我被这最后一段代码困住了。 我想要按下按钮时播放闹钟。 警报在MUSIC_STREAM上播放,因为它只能通过耳机/听筒播放,所以没有其他人受到干扰。如何使用Android(Java)在后台停止播放所有音乐?
public void playAlarm(){
try {
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(this, alert);
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
originalVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(false);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, alarmVolume, 0);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, originalVolume, 0);
}
});
} catch(Exception e) {
}
}
^这段代码在警报触发时运行。奇迹般有效。播放默认的闹铃声并重复播放。当我杀了应用程序,声音停止 - 就像我想要的。
public void killApp(){
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
finish();
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
^This works too too。
不工作的部分是这样的:
private static void sendMediaButton(Context context, int keyCode) {
KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
Intent intentPause = new Intent(Intent.ACTION_MEDIA_BUTTON);
intentPause.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
context.sendOrderedBroadcast(intentPause, null);
keyEvent = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
intentPause = new Intent(Intent.ACTION_MEDIA_BUTTON);
intentPause.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
context.sendOrderedBroadcast(intentPause, null);
}
^它暂停音乐后级或Player.fm(和其他musicplayers),让我报警的发挥,但它也重新开始歌曲在我的默认音乐应用程序(PowerAmp) - 音量非常低,但我的警报和歌曲都可以忍受。
当我使用Player.fm播放音乐时,播放器也会暂停,警报将播放,但我的默认音乐应用程序会再次播放。
我试着在网络上搜索,所有这些代码:
private static void sendMediaButton2(final Context context, int keyCode){
Intent mediaEvent = new Intent(Intent.ACTION_MEDIA_BUTTON);
KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY);
mediaEvent.putExtra(Intent.EXTRA_KEY_EVENT, event);
context.sendBroadcast(mediaEvent);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
Intent mediaEvent = new Intent(Intent.ACTION_MEDIA_BUTTON);
KeyEvent event = new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_MEDIA_PLAY);
mediaEvent.putExtra(Intent.EXTRA_KEY_EVENT, event);
context.sendBroadcast(mediaEvent);
}
}, 100);
}
^不工作
public void stopMusicPlaying5(){
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
String SERVICECMD = "com.android.music.musicservicecommand";
String CMDNAME = "command";
String CMDSTOP = "stop";
if(mAudioManager.isMusicActive()) {
Intent i = new Intent(SERVICECMD);
i.putExtra(CMDNAME , CMDSTOP);
MapsActivity.this.sendBroadcast(i);
}
}
^不工作
public void stopMusicPlaying(){
KeyEvent ke = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE);
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
// construct a PendingIntent for the media button and unregister it
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
//PendingIntent pi = PendingIntent.getBroadcast(AppContext.getContext(),
// 0/*requestCode, ignored*/, mediaButtonIntent, 0/*flags*/);
intent.putExtra(Intent.EXTRA_KEY_EVENT, ke);
//sendKeyEvent(pi, AppContext.getContext(), intent);
ke = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PAUSE);
intent.putExtra(Intent.EXTRA_KEY_EVENT, ke);
//sendKeyEvent(pi, AppContext.getContext(), intent);
// android.intent.action.MEDIA_BUTTON
}
^不工作
public void stopMusicPlaying1(){
long eventtime = SystemClock.uptimeMillis();
Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
sendOrderedBroadcast(downIntent, null);
}
^不工作
public void stopMusicPlaying4(){
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (audioManager != null){
audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE);
audioManager.dispatchMediaKeyEvent(event);
}
}
public void stopMusicPlaying2(){
long eventtime = SystemClock.uptimeMillis();
Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_STOP, 0);
upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
sendOrderedBroadcast(upIntent, null);
}
^而且,奇怪的是,没有工作。
_“当我杀应用” _你为什么这样做?停止'MediaPlayer'(也许完成当前的Activity)似乎更合适。至于回放问题,通常的方法是暂时为您的应用程序请求音频焦点。 – Michael
@迈克尔可能是一个更好的主意。感谢您的输入。 –