我对通过媒体播放器播放音乐时出现在锁定屏幕上的内置媒体控件有疑问。
播放音乐时,控件始终存在,但是当我按下锁定屏幕上的暂停按钮并通过 mediaplayer.pause() 暂停媒体播放器然后关闭屏幕并再次打开时(再次查看锁定屏幕),媒体控件已经消失,尽管播放器只是暂停并没有停止。
使用同样使用媒体播放器的内置音乐播放器应用程序,这不会发生。
以下是相关的代码部分:
if (action.equals(Intent.ACTION_SCREEN_OFF)) {
if(userPresent)
int result = mAudioManager.requestAudioFocus(mAudioFocusListener,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
configMediaPlayer();
}
userPresent = false;
}
screenOn = false;
}
else if (action.equals(Intent.ACTION_USER_PRESENT)) {
stopMediaPlayer();
userPresent = true;
}
该代码位于广播接收器中。仅当屏幕关闭(部分唤醒锁被保持)或存在锁定屏幕时才播放音乐。
private void configMediaPlayer(){
if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mediaPlayer.setOnPreparedListener(this);
}
else
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(getApplicationContext(), Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.song));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
}
@Override
public void onPrepared(MediaPlayer mp) {
startMediaPlayer();
}
private void startMediaPlayer(){
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
private void stopMediaPlayer(){
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
暂停事件的函数:
public void pause(){
if(isPaused && !userPresent){
startMediaPlayer();
Intent i = new Intent("com.android.music.playstatechanged");
i.putExtra("position", Long.valueOf(123));
i.putExtra("playing", true);
sendStickyBroadcast(i);
isPaused = false;
}
else if (!isPaused && !userPresent){
mediaPlayer.pause();
isPaused = true;
Intent i = new Intent("com.android.music.playstatechanged");
i.putExtra("position", Long.valueOf(123));
i.putExtra("playing", false);
sendStickyBroadcast(i);
}
}
当按下按钮时,我通过 mediabuttonintentreceiver 接收暂停事件,然后执行 pause() 函数。因为我正在为姜饼开发应用程序,remotecontrolclient 不可用,并且在我发现的音乐播放器应用程序中标记播放状态更改的唯一相关内容是:
Intent i = new Intent(what);
i.putExtra("id", Long.valueOf(getAudioId()));
i.putExtra("artist", getArtistName());
i.putExtra("album",getAlbumName());
i.putExtra("track", getTrackName());
i.putExtra("playing", isPlaying());
sendStickyBroadcast(i);
暂停播放时音频焦点不会丢失,因为我在日志中没有收到来自 onaudiochangelistener 的消息:
private OnAudioFocusChangeListener mAudioFocusListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
String LOGTAG = "AUDIOFOCUSCHANGELISTENER";
switch (focusChange) {
case AudioManager.AUDIOFOCUS_LOSS:
Log.e(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS, turning FM off");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
Log.e(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Log.e(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT");
break;
case AudioManager.AUDIOFOCUS_GAIN:
Log.e(LOGTAG, "AudioFocus: received AUDIOFOCUS_GAIN");
break;
default:
Log.e(LOGTAG, "Unknown audio focus change code " + focusChange);
}
}
};
我缺少什么或无法正常工作?我希望有人可以帮助我,因为我已经搜索了论坛,谷歌搜索并且已经测试了好几天都没有成功
编辑:我忘了提,函数/代码在后台服务中运行。也许它是有帮助的。
编辑
好的,我终于把它缩小到一个更具体的点。仅当我与 Intent.ACTION_SCREEN_OFF 一起启动媒体播放器时才会发生这种情况。
如果我在该意图之前启动媒体播放器,例如在服务的 onCreate() 函数中,然后关闭屏幕,重新打开(锁定屏幕),按下暂停按钮,再次关闭,最后打开同样,媒体控件在锁定屏幕上可见并处于暂停状态。
但是如果我在收到 Intent.ACTION_SCREEN_OFF 后启动媒体播放器,就会出现上述问题。
当问题发生时,我在服务内部的 braodcastreceiver 类中启动媒体播放器是否与此有关?或者您是否应该在屏幕关闭后干脆不启动媒体播放器,尽管您有唤醒锁?