2014/01/17

AMR and WAV files Playback in Android, IOS and Browser


上篇 Android and IOS recording file 已介紹過如何錄製 AMR 音訊檔,接著就來介紹如何在 Android IOS and Browser 上播放 AMR 音訊檔

Android 播放 AMR 音訊檔


//使用系統播放器開啟音訊檔並播放
private void openFile(File aFile) {  
    Intent intent = new Intent();  
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
    intent.setAction(android.content.Intent.ACTION_VIEW);  
    String type = getMIMEType(aFile);  
    intent.setDataAndType(Uri.fromFile(aFile), type);  
    startActivity(intent);  
} 

//取得檔案類型
private String getMIMEType(File aFile) {  
    String end = aFile.getName().substring(aFile.getName().lastIndexOf(".") + 1, aFile.getName().length()).toLowerCase();  
    String type = "";  
    if (end.equals("mp3") || end.equals("aac") || end.equals("wav")  
            || end.equals("amr") || end.equals("mpeg") || end.equals("mp4")) {  
        type = "audio";  
    } else if (end.equals("jpg") || end.equals("gif") || end.equals("png")  
            || end.equals("jpeg")) {  
        type = "image";  
    } else {  
        type = "*";  
    }  
    type += "/*";  
    return type;  
}   

IOS 播放 WAV 音訊檔

ios 自 4.3 開始不支援 AMR 音訊格式,故播放前必須先轉檔,這裡示範轉成 WAV
AMR to WAV

//WAV轉AMR格式
int result = [VoiceConverter wavToAmr:self.wavFullName amrSavePath:amrFullName];
WAV 音訊播放

self.wavFullName = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"recorder.wav"];
    NSURL *tempUrl = [NSURL fileURLWithPath:self.wavFullName];
    NSLog(@"play tempUrl=%@",tempUrl);
    NSError *err = nil;
    //初始化播放器
    self.audioPlayer = [[AVAudioPlayer alloc]init];
    self.audioPlayer = [self.audioPlayer initWithContentsOfURL:tempUrl error:&err];
    if(err!=nil){
        NSLog(@"play err=%@",err);
    }
    //開始播放
    [self.audioPlayer play];
PS. 關於 Apple 官網說的支援 3gp 格式,我測試結果無法播放,播放器閃一下就消失了,但 mp4 測試播放正常,如果有朋友實現 IOS 3gp 檔案格式播放,也請不吝賜教,提供一下心得,謝謝! 

Browser 播放 AMR 音訊檔

這裡使用 quicktime 套件與 script 語法進行播放(若電腦未安裝 quicktime 系統會提示下載並安裝)

<script language="JavaScript" type="text/javascript">
function playAMR(filePath, width, height){
var pv='';
pv += '<object width="'+width+'" height="'+height+'" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab">';
pv += '<param name="src" value="'+filePath+'">';
pv += '<param name="controller" value="true">';
pv += '<param name="type" value="video/quicktime">';
pv += '<param name="autoplay" value="true">';
pv += '<param name="target" value="myself">';
pv += '<param name="bgcolor" value="black">';
pv += '<param name="pluginspage" value="http://www.apple.com/quicktime/download/index.html">';
pv += '<embed src="'+filePath+'" width="'+width+'" height="'+height+'" controller="true" align="middle" bgcolor="black" target="myself" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/index.html"></embed>';
pv += '</object>';
document.write(pv);
}
</script>

References

沒有留言:

張貼留言