Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Byte array in MediaPlayer - Android

Tags:

android

I am trying to play an audio file with the MediaPlayer. I want to play byte array in MediaPlayer. How can I do this? I've checked this

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

But its not playing audio. It is just generating many files in SD Card. And giving this error:

 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)

Please help me. Any kind of help is appreciated.

Thanks

like image 756
Krishna Suthar Avatar asked Jun 06 '12 05:06

Krishna Suthar


1 Answers

Based on your comments, you are using a WAV file for streaming. Conventional WAV has a header at the beginning of the file and the rest of the file is pure data. If you don't include the header part, then the parameters from it need to be provided to the player the be able to interpret the data (number of channels, samples per second, block size, etc.) As a result, a WAV file alone is not streamable, the parameters must be fed into the player.

MPEG-4 on the other hand has been specified to be capable of streaming. It contains well identifiable segments that are possible to be played alone, so as long as the data chunk contains a header, the data after it can be playable. In addition to it's good compression ratio, this is one of the reasons why many internet radios use it.

MediaPlayer in Android is a high level component and the low level parameters that would be required for playing a WAV chunk are not accessible. If you need the source to be WAV and cannot use other streamable formats, then you can try the AudioTrack class or OpenSL ES for even lower level access. For AudioTrack this is a very good tutorial: http://audioprograming.wordpress.com/2012/10/18/a-simple-synth-in-android-step-by-step-guide-using-the-java-sdk/

like image 92
allprog Avatar answered Nov 15 '22 15:11

allprog