由于某些原因,repeat和shuffle按钮没有任何作用。
上一个和下一个按钮工作得很好。
在我的应用程序中,我通过广播与我的服务进行通信。
mediaPlayer
是我的服务类中的一个公共静态,我正在其他活动中导入它。
public static MediaPlayer mediaPlayer = new MediaPlayer();
(服务中等级(
活动
这是按钮的代码
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mBtnPlayPause.setImageResource(R.drawable.ic_action_pause_white);
tvSongListSize.setText((songIndex + 1) + "/" + songList.size());
updateProgressBar();
}
}
mBtnShuffle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isShuffle){
isShuffle = false;
Toast.makeText(getContext(), "Shuffle is off", Toast.LENGTH_SHORT ).show();
mBtnShuffle.setImageResource(R.drawable.ic_action_shuffle);
}else{
isShuffle = true;
Toast.makeText(getContext(), "Shuffle is on", Toast.LENGTH_SHORT).show();
mBtnShuffle.setImageResource(R.drawable.ic_shuffle_on);
isRepeat = false;
mBtnRepeat.setImageResource(R.drawable.ic_action_repeat);
}
}
});
mBtnRepeat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isRepeat){
isRepeat = false;
Toast.makeText(getContext(), "Repeat is off", Toast.LENGTH_SHORT).show();
mBtnRepeat.setImageResource(R.drawable.ic_action_repeat);
}else{
isRepeat = true;
Toast.makeText(getContext(), "Repeat is on", Toast.LENGTH_SHORT).show();
mBtnRepeat.setImageResource(R.drawable.ic_repeat_on);
isShuffle = false;
mBtnShuffle.setImageResource(R.drawable.ic_action_shuffle);
}
}
});
mBtnPlayPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
mBtnPlayPause.setImageResource(R.drawable.ic_action_play);
} else {
mediaPlayer.start();
mBtnPlayPause.setImageResource(R.drawable.ic_action_pause_white);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
mBtnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nextSong();
}
});
mBtnPrev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
prevSong();
}
});
onCompletionListener中的代码
@Override
public void onCompletion(MediaPlayer mp) {
if (isRepeat){
//Store current songIndex in mSharedPreferences
StorageUtil storageUtil = new StorageUtil(getContext());
storageUtil.storeSongIndex(songIndex);
//Send media with BroadcastReceiver
Intent broadCastReceiverIntent = new Intent(Constants.ACTIONS.BROADCAST_PlAY_NEW_SONG);
if (getActivity() != null) {
getActivity().sendBroadcast(broadCastReceiverIntent);
}
}else if(isShuffle){
Random random = new Random();
songIndex = random.nextInt((songList.size() - 1) + 1);
tvSongListSize.setText((songIndex + 1) + "/" + songList.size());
//Store random songIndex in mSharedPreferences
StorageUtil storageUtil = new StorageUtil(getContext());
storageUtil.storeSongIndex(songIndex);
//Send media with BroadcastReceiver
Intent broadCastReceiverIntent = new Intent(Constants.ACTIONS.BROADCAST_PlAY_NEW_SONG);
if (getActivity() != null) {
getActivity().sendBroadcast(broadCastReceiverIntent);
}
}else if (songIndex < songList.size()-1){
mediaPlayer.reset();
nextSong();
tvSongListSize.setText((songIndex + 1) + "/" + songList.size());
}else{
songIndex = 0;
tvSongListSize.setText((1) + "/" + songList.size());
//Store random songIndex in mSharedPreferences
StorageUtil storageUtil = new StorageUtil(getContext());
storageUtil.storeSongIndex(songIndex);
//Send media with BroadcastReceiver
Intent broadCastReceiverIntent = new Intent(Constants.ACTIONS.BROADCAST_PlAY_NEW_SONG);
if (getActivity() != null) {
getActivity().sendBroadcast(broadCastReceiverIntent);
}
}
}
广播接收机
private BroadcastReceiver NewSongBroadCastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
songList = new StorageUtil(getApplicationContext()).getSongs();
songIndex = new StorageUtil(getApplicationContext()).loadSongIndex();
if (songIndex != -1 && songIndex < songList.size()){
activeSong = songList.get(songIndex);
}else{
stopSelf();
}
stopSong();
mediaPlayer.reset();
if (mMediaSessionManager == null) {
try {
initMediaSession();
initMediaPlayer();
} catch (RemoteException e) {
e.printStackTrace();
stopSelf();
}
}
initMediaPlayer();
updateMetaData();
NotificationBuilder(PlaybackStatus.PLAYING);
}
};
按钮mBtnShuffle
和mBtnRepeat
不会触发onCompletion
。它们只是用于设置isShuffle和isRepeat标志的两个普通按钮onCompletion
在歌曲完成后被触发
因此,如果您希望在点击这两个按钮时发生一些事情,您应该在它们的侦听器中放入一些代码。