Play Byte array in MediaPlayer - Android

I am trying to play an audio file with MediaPlayer. I want to play a byte array in MediaPlayer. How can i do this? I 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 it does not play audio. It just generates a lot of files on the 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 help is appreciated.

thank

+5
source share
3 answers

WAV . WAV , - . , , ( , , ..). WAV , .

MPEG-4, , . , , , , . , , - .

MediaPlayer Android - , , WAV, . WAV , AudioTrack class OpenSL ES . AudioTrack : http://audioprograming.wordpress.com/2012/10/18/a-simple-synth-in-android-step-by-step-guide-using-the-java-sdk/

+6

: :

 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

Try the following:

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

All Articles