0

我使用警报管理器来停止所有在 android 中播放的音乐。但我不知道如何做到这一点。在主要活动源代码中

 Intent intent = new Intent(AlarmActivity.this, OneShotAlarm.class);
        PendingIntent sender = PendingIntent.getBroadcast(AlarmActivity.this,
                0, intent, 0);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 5);

        // Schedule the alarm!
        AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

和 Alarm 扩展 BroadcastReceiver

public class OneShotAlarm extends BroadcastReceiver

{

@Override
public void onReceive(Context context, Intent intent)
{  
// What can I do for stop music of device is running
    Toast.makeText(context, R.string.one_shot_received, Toast.LENGTH_SHORT).show();
}

}

4

1 回答 1

0

您可以查看:AudioManager

像这样的东西应该工作:

AudioManager aManager = (AudioManager)getSystemService(AUDIO_SERVICE);
aManager.setStreamMute(AudioManager.STREAM_MUSIC, true);

编辑:有一个功能可以“音频焦点”:AudioManager

public int requestAudioFocus (AudioManager.OnAudioFocusChangeListener l, int streamType, int durationHint)

样本

AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);
...

// Request audio focus for playback
int result = am.requestAudioFocus(afChangeListener,
                                 // Use the music stream.
                                 AudioManager.STREAM_MUSIC,
                                 // Request permanent focus.
                                 AudioManager.AUDIOFOCUS_GAIN);

if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
    am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
    // Start playback.
}
于 2013-07-06T05:01:26.903 回答