0

I have been trying to play audio through a string/byte in android using MediaPlayer but could achieve the result.The code i am using is

`
    String ret = "";
    try {
        InputStream inputStream = this.openFileInput(Name);

        if ( inputStream != null ) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ( (receiveString = bufferedReader.readLine()) != null ) {
                stringBuilder.append(receiveString);
            }

            inputStream.close();
            ret = stringBuilder.toString();

            String url = "data:audio/mp3;base64,"+ret;
            MediaPlayer mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource(url);
            mediaPlayer.prepare();
            mediaPlayer.start();

        }

    }
    catch (FileNotFoundException e) {
        Toast.makeText(this, "File not found", Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        Toast.makeText(this,String.valueOf(e), Toast.LENGTH_LONG).show();
    }
    catch (Exception e)
    {
        Toast.makeText(this,String.valueOf(e),Toast.LENGTH_LONG).show();
    }`

The file is the Base64 encoding of the song that is saved from the server. The exception i am geting is java.io.ioexception prepare failed status=0x800000000

Can I play the audio using string/byte using MediaPlayer? If not, is there any solution through which I could play the same. Please help.

[EDIT] Android - Playing mp3 from byte[] This accepted answer works for me.But is there any other way to play the audio without creating a temporary file?

farmillion123
  • 179
  • 1
  • 12

1 Answers1

0

You are calling mediaPlayer.setDataSource(url) method. Here url (String) must be a valid internet url. You are trying to pass data in the form of String here.

Second, you haven't called

mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

method before calling prepare() method. This method is a must calling method in order for the target stream type to become effective.

You can make a MediaPlayer working in three ways. Have a look of them in below example:

    MediaPlayer mediaPlayer = new MediaPlayer();
    File file = new File("");// if you have a file object representing your audio file, you can start from here.
    Uri uri = Uri.fromFile(file);// if you have a valid uri representing your audio file (may be obtained from a content provider or any other means etc), you can start from here
    try {
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDataSource(this,uri);
        mediaPlayer.prepare(); // or mediaPlayer.prepareAsync();
        mediaPlayer.start();
    } catch (IOException e) {
        e.printStackTrace();
    }

If you have a valid internet UrlString then:

    MediaPlayer mediaPlayer = new MediaPlayer();
    String urlString = "http://...";// like this
    try {
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDataSource(urlString);
        mediaPlayer.prepare(); // or mediaPlayer.prepareAsync();
        mediaPlayer.start();
    } catch (IOException e) {
        e.printStackTrace();
    }

You will not need to entrap in Input/Output streams and all.

Then, try along this code:

byte[] byteArray = // your byteArray
    try {
        File file = File.createTempFile("prefix","suffix");//creates temporary file
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(byteArray);//your bytes written in that temporary file
    } catch (IOException e) {
        e.printStackTrace();
    }

From here you can initialise you mediaPlayer and use this temporary file.

Manishoaham
  • 601
  • 1
  • 5
  • 14
  • Well, I have a variable(both string & byte[]) whose value is the encoded data of the audio.I need to play the audio using this variable. – farmillion123 Jul 29 '17 at 06:23