android開發:把一個byte數群組轉換成wav音頻檔案,並且播放

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   os   ar   java   for   

============問題描述============


如題,byte數群組轉換成wav音頻檔案,並且播放,下面代碼能產生data/data/com.example.playwav/cache/temp.wav
但是在播放的時候報異常。
我把代碼和Log貼在下面了。
我分析, 原因應該是wav檔案格式的編解碼問題,不能這麼隨隨便便把任意的一個byte數組就轉化為了wav
希望瞭解wav編解碼開發的童鞋給點解決辦法

byte[] a = { 52, 51, 48, 28, 58, 64, 98,-127, 56, 52, 51, 48, 28, 58, 64, 98,64, 98, 13, 2, 56, -4, 62, -116, 0, -59, 39, -116, 0, -84,64, 98, 13, 2, 56, -4, 62, -116, 0, -59, 39, -116, 0, -84,64, 98, 13, 2, 56, -4, 62, -116, 0, -59, 39, -116, 0, -84,64, 98, 13, 2, 56, -4, 62, -116, 0, -59, 39, -116, 0, -84,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -65 };playWav(a);public void playWav(byte[] byteWav) {String path = this.getCacheDir().getParent() + File.separator;try {File file = new File(path);if (!file.exists()) {file.mkdirs();}File tempWav = new File(file, "temp.wav");if (!tempWav.exists())tempWav.createNewFile();FileOutputStream fos = new FileOutputStream(tempWav);fos.write(byteWav);fos.flush();fos.close();MediaPlayer mediaPlayer = new MediaPlayer();FileInputStream fis = new FileInputStream(tempWav);mediaPlayer.setDataSource(fis.getFD());mediaPlayer.prepare();mediaPlayer.start();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}

報異常:10-17 11:16:42.726: E/MediaPlayer(20944): Unable to to create media player
10-17 11:16:42.726: D/String(20944): string = java.io.IOException: setDataSourceFD failed.: status=0x80000000
10-17 11:16:42.726: W/System.err(20944): java.io.IOException: setDataSourceFD failed.: status=0x80000000
10-17 11:16:42.726: W/System.err(20944): at android.media.MediaPlayer.setDataSource(Native Method)
10-17 11:16:42.726: W/System.err(20944): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:780)
10-17 11:16:42.726: W/System.err(20944): at com.example.playwav.MainActivity.playTempWav(MainActivity.java:56)
10-17 11:16:42.726: W/System.err(20944): at com.example.playwav.MainActivity$1.onClick(MainActivity.java:39)
10-17 11:16:42.736: W/System.err(20944): at android.view.View.performClick(View.java:2486)
10-17 11:16:42.736: W/System.err(20944): at android.view.View$PerformClick.run(View.java:9130)
10-17 11:16:42.736: W/System.err(20944): at android.os.Handler.handleCallback(Handler.java:587)
10-17 11:16:42.736: W/System.err(20944): at android.os.Handler.dispatchMessage(Handler.java:92)
10-17 11:16:42.736: W/System.err(20944): at android.os.Looper.loop(Looper.java:130)
10-17 11:16:42.736: W/System.err(20944): at android.app.ActivityThread.main(ActivityThread.java:3703)
10-17 11:16:42.736: W/System.err(20944): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 11:16:42.736: W/System.err(20944): at java.lang.reflect.Method.invoke(Method.java:507)
10-17 11:16:42.736: W/System.err(20944): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
10-17 11:16:42.736: W/System.err(20944): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
10-17 11:16:42.736: W/System.err(20944): at dalvik.system.NativeStart.main(Native Method)

============解決方案1============



我做的一個項目的一部分,是個網路攝影機的播發,就是從網路收到被adpcm編碼過的音頻包,然後我用adpcm的codec解碼,直接交由AudioTrack來進行播放
簡單的流程就是   經過adpcm編碼的 byte[]  解碼成byte,然後由AudioTrack進行播放。
希望對你有所啟發。理論上來說,最後的聲音播放,就是位元據,進入音頻I/O裝置,然後調用硬體發出聲音。
AudioTrack你可以去看下。

android開發:把一個byte數群組轉換成wav音頻檔案,並且播放

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.