android: 播放音频文件后做一些事情

android: do something after playing audio file

我尝试在我的应用程序中流式传输和播放音频文件。我使用此代码:

MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(url);
player.prepare();
player.start();

我想知道是否可以检测玩家何时结束游戏?我想在玩家玩完后更改按钮颜色

查看setOnCompletionListener方法:

player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        Log.d(TAG, "Done playing!");
    }
});

您实际上可以在此处设置大量回调。除了setOnCompletionListener,我们还有:

  • setOnBufferingUpdateListener
  • setOnErrorListener
  • setOnInfoListener
  • setOnPreparedListener
  • setOnSeekCompleteListener
  • setOnTimedMetaDataAvailableListener
  • setOnTimedTextListener
  • setOnVideoSizeChangedListener.