1

I am trying to make an android app that would stream audio from a webserver using http, streaming itself works with VLC and HTML "audio" tag but when trying to stream it using MediaPlayer the file just gets downloaded as a whole, not streamed.

When streamed through VLC/HTML, http response codes are 206(partial data), streaming through MediaPlayer returns 200 every time. File served on the server is in wav format(also tried m4a and mp4)

Code taking care of streaming(basically taken straight from the docs):

String url = "http://localhost:5000/song";
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioAttributes(
    new AudioAttributes.Builder()
        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
        .setUsage(AudioAttributes.USAGE_MEDIA)
        .build()
);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
mediaPlayer.start();

1 Answers1

1

In your first line, change localhost to the ip address where flask is running

String url = "http://192.168.***.***:5000/song";

And run flask with the host as unspecified address (i.e. 0.0.0.0)

app.run(debug=True, host="0.0.0.0", port=5000)

Not sure why this worked, i was just messing around with different host names

Some other tips for people googling this:

  • Add Internet permission <uses-permission android:name="android.permission.INTERNET" /> in manifest
  • OP is using a route "/song" which is better solution esp if files are in folder other than static. If your files are in the static folder, you don't need a route and can reference their url directly String url = "http://192.168.***.***:5000/static/mysong.mp3
billy bud
  • 103
  • 2
  • 13