Android - playing mp3 from byte [] array

I have a byte [] array containing an mp3 file. Is there a way to play an mp3 file without creating a temporary file?

thank

Edited:

I've tried a temporary file-based approach:

                File tempMp3 = File.createTempFile("test", ".mp3", getCacheDir());

                //tempMp3.deleteOnExit();
                FileOutputStream fos = new FileOutputStream(tempMp3);
                fos.write(bytes);
                fos.close();

                mediaPlayer = MediaPlayer.create(getApplicationContext(),Uri.fromFile(tempMp3));
                mediaPlayer.start();

      

And I am still getting NullPointerException. I am writing a byte array (called bytes) to a file, I save its reference and use it. But still getting NPE. Do you know where the error is?

+3


source to share


5 answers


I assume that you are using MediaPlayer

, and until the last API can set the data in format byte[]

just like String

(path), FileDescriptor

or Uri

so I am afraid you will have to write a temporary one File

.



+2


source


Android provides a very low level API ( AudioTrack ) for writing raw PCM data directly to audio hardware.



+1


source


With tmpFile follow this post: Android - Play mp3 from byte []

If it's still npe check if your file (tempMp3) is not null and if Uri.fromFile returns uri and not null.

0


source


private void playByteArray(byte[] mp3SoundByteArray) {
    try {
        File Mytemp = File.createTempFile("TCL", "mp3", getCacheDir());
        Mytemp.deleteOnExit();
        FileOutputStream fos = new FileOutputStream(Mytemp);
        fos.write(mp3SoundByteArray);
        fos.close();

        MediaPlayer mediaPlayer = new MediaPlayer();

        FileInputStream MyFile = new FileInputStream(Mytemp);
        mediaPlayer.setDataSource(Mytemp.getFD());

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

      

0


source


I know this is old, but I have searched for an answer and haven't found any that really allows playing from a byte array without temporary files, and there is indeed a very simple way to create a custom MediaDataSource.

I am posting the code here for Xamarin Android, but it is very easy to port to Java, the question is if you can see a way to do it.

Please note that these methods were added in the android 6.0 SDK, if you are targeting lower Android versions you must use the correct compatibility support library.

public class AudioPlayer
{
    MediaPlayer currentPlayer;

    public void Play(byte[] AudioFile, bool Loop)
    {
        Stop();

        currentPlayer = new MediaPlayer();
        currentPlayer.Prepared += (sender, e) =>
        {
            currentPlayer.Start();
        };
        currentPlayer.Looping = Loop;
        currentPlayer.SetDataSource(new StreamMediaDataSource(new System.IO.MemoryStream(AudioFile)));
        currentPlayer.Prepare();
    }

    public void Stop()
    {
        if (currentPlayer == null)
            return;

        currentPlayer.Stop();
        currentPlayer.Dispose();
        currentPlayer = null;
    }
}

public class StreamMediaDataSource : MediaDataSource
{
    System.IO.Stream data;

    public StreamMediaDataSource(System.IO.Stream Data)
    {
        data = Data;
    }

    public override long Size
    {
        get
        {
            return data.Length;
        }
    }

    public override int ReadAt(long position, byte[] buffer, int offset, int size)
    {
        data.Seek(position, System.IO.SeekOrigin.Begin);                
        return data.Read(buffer, offset, size);
    }

    public override void Close()
    {
        if (data != null)
        {
            data.Dispose();
            data = null;
        }
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);

        if (data != null)
        {
            data.Dispose();
            data = null;
        }
    }
}

      

0


source







All Articles