AndroId MediaPlayer prepareAsync方法

时间:2012-04-24 22:52:32

标签: android asynchronous android-mediaplayer prepare

我有一个奇怪的问题。我使用带有MediaPlayer的prepareAsync方法,但我声明的侦听器永远不会被触发。我尝试从互联网(广播电台)流式传输实时.mp3 Feed。我为侦听器使用内联方法,但我也尝试实现接口而没有任何成功。这是我代码的一部分:

在会员部分:

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

活动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();

然后,这是preparePlayer()方法:

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();
    }
}

(仅供记录),onDetroy()我在哪里进行清洁:

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

@Override
protected void onDestroy() {
    super.onDestroy();
    releaseMediaPlayer();
}

2 个答案:

答案 0 :(得分:11)

您正在OnPreparedListener的创建方法中设置Activity,之后可能会在MediaPlayer方法中创建新的preparePlayer()。任何新创建的MediaPlayer都不会设置prepareListener。您还应该添加errorlistener以了解最新情况。

您可能希望在

中移动setOnPreparedListener来电
  if (mediaPlayer == null) {
    mediaPlayer = new MediaPlayer();
}

以及如此设置setOnErrorListener

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();
        }
    });
}

请将e.printStackTrace();更改为

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

希望这有帮助,如果您有任何日志数据,请告诉我们,因为这也很有用。

答案 1 :(得分:1)

原来,我忘记在我的内部方法之上提到“@Override”。像这样:

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

现在一切正常。嗯......差不多,因为需要永远缓冲...