11

I have a strange problem. I use the prepareAsync method with the MediaPlayer, but the listener that I declared just never gets fired. I try to stream a live .mp3 feed from the Internet (radio station). I use an inline method for the listener, but I also tried implementing the interface without any success. Here's a portion of my code :

In the member section :

String url = "http://<my_url>.mp3";
MediaPlayer mediaPlayer = new MediaPlayer();

In Activity onCreate() :

ToggleButton playButton = (ToggleButton) findViewById(R.id.playToggleButton);
playButton.setOnClickListener(this);
playButton.clearFocus();
playButton.setClickable(false);

mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
    public void onPrepared(MediaPlayer mp) {
        ToggleButton playButton = (ToggleButton) findViewById(R.id.playToggleButton);
        playButton.setClickable(true);
        mp.start();
    }
});

preparePlayer();

Then, here's the preparePlayer() method :

private void preparePlayer() {
    if (mediaPlayer == null) {
        mediaPlayer = new MediaPlayer();
    }
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mediaPlayer.setDataSource(url);
        mediaPlayer.prepareAsync();

    } catch (IllegalArgumentException e) {
        Toast.makeText(
                MyStreamActivity.this,
                getResources().getString(R.string.erreurIllegalArgument),
                Toast.LENGTH_LONG).show();
        e.printStackTrace();
    } catch (IllegalStateException e) {
        Toast.makeText(
                MyStreamActivity.this,
                getResources().getString(R.string.erreurIllegalState),
                Toast.LENGTH_LONG).show();
        e.printStackTrace();
    } catch (IOException e) {
        Toast.makeText(
                MyStreamActivity.this,
                getResources().getString(R.string.erreurIO),
                Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}

And (just for the record), the onDetroy() where I do the cleaning :

private void releaseMediaPlayer() {
    if (mediaPlayer != null) {
        if(mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
        }
        mediaPlayer.release();
        mediaPlayer = null;
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    releaseMediaPlayer();
}
Ziem
  • 6,579
  • 8
  • 53
  • 86
Patrice Cote
  • 3,572
  • 12
  • 43
  • 72
  • does the log say anything about what happens or are there errors? Do you have the permission to internets? – zapl Apr 24 '12 at 22:57

2 Answers2

11

You are setting the OnPreparedListener in your Activity's create method and later on, possibly, creating a new MediaPlayer in your preparePlayer() method. Any newly created MediaPlayer's wont have the prepareListener set. You should also add an errorlistener to find out whats going on.

You may want to move the setOnPreparedListener call inside the

  if (mediaPlayer == null) {
    mediaPlayer = new MediaPlayer();
}

as well as setting a setOnErrorListener like so:

if (mediaPlayer == null) {
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnErrorListener(....);
    mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
        public void onPrepared(MediaPlayer mp) {
            ToggleButton playButton = (ToggleButton) findViewById(R.id.playToggleButton);
            playButton.setClickable(true);
            mp.start();
        }
    });
}

Please change e.printStackTrace(); to

Log.e("tag", e.getMessage(), e);  

Hope this helps, let us know if you have any log data as this will also be useful.

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
petey
  • 16,914
  • 6
  • 65
  • 97
2

Turned out, I forgot to mention the "@Override" on top of my inner method. Like this :

mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        ToggleButton playButton = (ToggleButton) findViewById(R.id.playToggleButton);
        playButton.setClickable(true);
        mp.start();
    }
});

Now everything works fine. Well... almost, since it takes forever to buffer...

Patrice Cote
  • 3,572
  • 12
  • 43
  • 72
  • 1
    quite old, but I'd still like to point out that *the @Override statement doesn't produce code* and therefore can't possibly be a solution to this or other code-related issues. See http://stackoverflow.com/a/8545586/2177061 for reference. – Rick77 Sep 28 '14 at 22:44
  • That probably isn't the cause of this problem. I'm now having the exact same problem and setting "Override" doesn't help. While the example application also didn't set it anyways. – xji Nov 07 '14 at 07:15
  • 1
    `@Override` is totally optional in Java, whether Android or Swing or Java EE. Thus this cannot never be your problem – FindOut_Quran Oct 01 '15 at 17:04