2012-06-06 37 views
5

我試圖用MediaPlayer播放音頻文件。我想在MediaPlayer中播放字節數組。我怎樣才能做到這一點?我查過this在MediaPlayer中播放字節數組 - Android

public void writeSamples(byte[] samples, int length) 
{ 
    // track.write(samples, 0, length); 
    File tempMp3; 
    try { 
    tempMp3 = File.createTempFile("kurchina", ".mp3"); 
     tempMp3.deleteOnExit(); 
     FileOutputStream fos = new FileOutputStream(tempMp3); 
     fos.write(samples); 
     fos.close(); 
     // Tried reusing instance of media player 
     // but that resulted in system crashes... 
     MediaPlayer mediaPlayer = new MediaPlayer(); 

     // Tried passing path directly, but kept getting 
     // "Prepare failed.: status=0x1" 
     // so using file descriptor instead 
     FileInputStream fis = new FileInputStream(tempMp3); 
     mediaPlayer.setDataSource(fis.getFD()); 
     mediaPlayer.prepare(); 
     mediaPlayer.start(); 
     } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 

但它沒有播放音頻。它只是在SD卡中生成許多文件。並給這個錯誤

06-06 11:02:59.191: E/MediaPlayer(1831): Unable to to create media player 
06-06 11:02:59.191: W/System.err(1831): java.io.IOException: setDataSourceFD failed.:  status=0x80000000 
06-06 11:02:59.201: W/System.err(1831): at android.media.MediaPlayer.setDataSource(Native Method) 
06-06 11:02:59.201: W/System.err(1831): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:749) 
06-06 11:02:59.201: W/System.err(1831): at org.vinuxproject.sonic.SonicTest$1.run(SonicTest.java:178) 
06-06 11:02:59.201: W/System.err(1831): at java.lang.Thread.run(Thread.java:1096) 

請幫助我。任何形式的幫助表示讚賞。

謝謝

+0

添加您的代碼片段在這裏,有人將能夠提供幫助。 –

+0

我建議嘗試setDataSource變體,除FD外還需要一個偏移量和長度。生成的mp3文件是否在音樂應用程序中播放?另外,deleteOnExit在Android/Dalvik中沒有用處。 – dagalpin

+0

沒有文件不在我的應用程序中播放。請幫助我 –

回答

6

根據您的意見,您正在使用WAV文件進行流式傳輸。在該文件的開始處傳統的WAV has a header並且文件的其餘部分是純數據。如果您不包含標題部分,則需要向播放器提供參數,以便能夠解釋數據(通道數,每秒採樣數,塊大小等)。結果,WAV文件本身不可流通,參數必須送入播放器。

MPEG-4另一方面已被指定爲能夠流式傳輸。它包含可以單獨播放的可識別段,只要數據塊包含一個標題,其後的數據就可以播放。除了良好的壓縮比外,這也是許多互聯網收音機使用它的原因之一。

MediaPlayer在Android中是一個高級別組件,並且無法訪問播放WAV塊所需的低級參數。如果你需要的源代碼是WAV,不能使用其他streamable formats,那麼你可以嘗試AudioTrack類或OpenSL ES進行更低級別的訪問。 對於AudioTrack,這是一個很好的教程:http://audioprograming.wordpress.com/2012/10/18/a-simple-synth-in-android-step-by-step-guide-using-the-java-sdk/

+0

的過載變體嗯... audioTrack聽起來像是要走的路。 – EGHDK

+0

如果我可以幫忙,請告訴我。 – allprog

+0

WordPress的鏈接是「死」,博客現在是私人的 –

0

寫後的文件字節: 你可以從這個功能發揮:

void playSound(int resid) { 
     MediaPlayer eSound = MediaPlayer.create(context, resid); 
     Resources res = context.getResources(); 
     AssetFileDescriptor afd = res.openRawResourceFd(resid); 
     eSound.reset(); 
     eSound.setAudioStreamType(AudioManager.STREAM_SYSTEM); 
     try { 
      eSound.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), 
        afd.getLength()); 
     } catch (IllegalArgumentException e) { 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     try { 
      eSound.prepare(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     eSound.start(); 
    } 

而且你可以從這裏獲取文件信息:

byte[] getFileInformation(String filepath) { 
     MediaPlayer eSound = MediaPlayer.create(context, Uri.parse(filepath)); 
     eSound.reset(); 
     eSound.setAudioStreamType(AudioManager.STREAM_SYSTEM); 
     try { 
      eSound.setDataSource(filepath); 
     } catch (IllegalArgumentException e) { 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     try { 
      eSound.prepare(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     int duration = eSound.getDuration()/1000; 

     int height = 480; 
     int width = 640; 
     height = eSound.getVideoHeight(); 
     width = eSound.getVideoWidth(); 

     eSound.release(); 
     File f = new File(filepath); 
     int size = (int) f.length(); 
     byte[] b = new byte[16]; 
     System.arraycopy(convertIntToByte(size), 0, b, 0, 4); 
     System.arraycopy(convertIntToByte(duration), 0, b, 4, 4); 
     System.arraycopy(convertIntToByte(width), 0, b, 8, 4); 
     System.arraycopy(convertIntToByte(height), 0, b, 12, 4); 
     return b; 
    } 
0

試試這個:

private void playMp3(byte[] mp3SoundByteArray) 
{ 
    try 
    { 

     File path=new File(getCacheDir()+"/musicfile.3gp"); 

     FileOutputStream fos = new FileOutputStream(path); 
     fos.write(mp3SoundByteArray); 
     fos.close(); 

     MediaPlayer mediaPlayer = new MediaPlayer(); 

     FileInputStream fis = new FileInputStream(path); 
     mediaPlayer.setDataSource(getCacheDir()+"/musicfile.3gp"); 

     mediaPlayer.prepare(); 
     mediaPlayer.start(); 
    } 
    catch (IOException ex) 
    { 
     String s = ex.toString(); 
     ex.printStackTrace(); 
    } 
}