2012-06-06 36 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(); 
    } 
}