Streaming audio from a url in Android using MediaPlayer?

I am trying to transfer mp3 via http using the Android built in MediaPlayer class. The documentation will tell me that this should be as simple as:

MediaPlayer mp = new MediaPlayer(); mp.setDataSource(URL_OF_FILE); mp.prepare(); mp.start(); 

However, I repeatedly get the following. I also tried different urls. Please do not tell me that streaming does not work on mp3.

 E/PlayerDriver( 31): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported W/PlayerDriver( 31): PVMFInfoErrorHandlingComplete E/MediaPlayer( 198): error (1, -4) E/MediaPlayer( 198): start called in state 0 E/MediaPlayer( 198): error (-38, 0) E/MediaPlayer( 198): Error (1,-4) E/MediaPlayer( 198): Error (-38,0) 

Any help is much appreciated, thanks S

+79
android mp3 android-mediaplayer audio-streaming
Dec 27 '09 at 11:40
source share
7 answers

A simple media player with a streaming example. For the xml part, you will need one button with the button id1 and two images in your folder named button_pause and button_play and please remember to add the permission on the Internet to the manifest.

 public class MainActivity extends Activity { private Button btn; /** * help to toggle between play and pause. */ private boolean playPause; private MediaPlayer mediaPlayer; /** * remain false till media is not completed, inside OnCompletionListener make it true. */ private boolean intialStage = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.button1); mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); btn.setOnClickListener(pausePlay); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } private OnClickListener pausePlay = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // TODO Auto-generated method stub if (!playPause) { btn.setBackgroundResource(R.drawable.button_pause); if (intialStage) new Player() .execute("http://www.virginmegastore.me/Library/Music/CD_001214/Tracks/Track1.mp3"); else { if (!mediaPlayer.isPlaying()) mediaPlayer.start(); } playPause = true; } else { btn.setBackgroundResource(R.drawable.button_play); if (mediaPlayer.isPlaying()) mediaPlayer.pause(); playPause = false; } } }; /** * preparing mediaplayer will take sometime to buffer the content so prepare it inside the background thread and starting it on UI thread. * @author piyush * */ class Player extends AsyncTask<String, Void, Boolean> { private ProgressDialog progress; @Override protected Boolean doInBackground(String... params) { // TODO Auto-generated method stub Boolean prepared; try { mediaPlayer.setDataSource(params[0]); mediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub intialStage = true; playPause=false; btn.setBackgroundResource(R.drawable.button_play); mediaPlayer.stop(); mediaPlayer.reset(); } }); mediaPlayer.prepare(); prepared = true; } catch (IllegalArgumentException e) { // TODO Auto-generated catch block Log.d("IllegarArgument", e.getMessage()); prepared = false; e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block prepared = false; e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block prepared = false; e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block prepared = false; e.printStackTrace(); } return prepared; } @Override protected void onPostExecute(Boolean result) { // TODO Auto-generated method stub super.onPostExecute(result); if (progress.isShowing()) { progress.cancel(); } Log.d("Prepared", "//" + result); mediaPlayer.start(); intialStage = false; } public Player() { progress = new ProgressDialog(MainActivity.this); } @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); this.progress.setMessage("Buffering..."); this.progress.show(); } } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); if (mediaPlayer != null) { mediaPlayer.reset(); mediaPlayer.release(); mediaPlayer = null; } } 
+68
Dec 06 '12 at 9:45
source share

Android MediaPlayer does not support streaming MP3s up to 2.2. In older versions of the OS, it seems that only the 3GP stream is native. You can try the pocketjourney code, although it is old (there is a new version here ), and I had problems with its stickiness - it stuttered whenever it refilled the buffer.

The NPR News app for Android is open source and uses a local proxy server to handle MP3 streaming in OS versions up to 2.2. You can see the corresponding code in lines 199-216 (r94) here: http://code.google.com/p/npr-android-app/source/browse/Npr/src/org/npr/android/news/PlaybackService .java? r = 7cf2352b5c3c0fbcdc18a5a8c67d836577e7e8e3

And this is the StreamProxy class: http://code.google.com/p/npr-android-app/source/browse/Npr/src/org/npr/android/news/StreamProxy.java?r=e4984187f45c39a54ea6c88f71197762dbe10e72

The NPR application also still receives an β€œerror (-38, 0)” sometimes during streaming. This could be a thread problem or a network switch problem. Check the error tracker for updates .

+35
Aug 25 '10 at 16:11
source share

I assume you are trying to play .pls directly or something like that.

try it:

1: code

 mediaPlayer = MediaPlayer.create(this, Uri.parse("http://vprbbc.streamguys.net:80/vprbbc24.mp3")); mediaPlayer.start(); 

2: .pls file

This URL is from the BBC as an example. It was a .pls file which I uploaded to linux with

 wget http://foo.bar/file.pls 

and then I opened vim (use your favorite editor;), and I saw the real URLs inside this file. Unfortunately, not all of the .pls are plain text.

I read that 1.6 does not support streaming mp3 via http, but I just tested the code above with Android 1.6 and 2.2 and had no problems.

Good luck

+9
15 Oct '11 at 10:05
source share

I had the same error as yours, and it turned out that nothing happened to the code. The problem was that the web server was sending the wrong Content-Type header.

Try a proxy server or something similar to see what type of content is being transmitted by the web server.

+2
Mar 24 '10 at 21:18
source share

Use

  mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaplayer.prepareAsync(); mediaplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mediaplayer.start(); } }); 
+2
Apr 03 '17 at 12:27
source share

There is no call to mp.start with OnPreparedListener to avoid the zero state of i log.

+1
Apr 05 2018-10-10T00:
source share

Looking at my projects:

+1
Oct 29 '14 at 6:10
source share



All Articles