音频线控制 MediaPlayer 服务
Audio cable controlling MediaPlayer Service
我编写了一个普通的MediaPlayer,比如APP。现在,我有一副带内置遥控器的耳机,可以控制音量、暂停和播放歌曲。
我的想法是用这个来控制我的 MediaPlayer 服务。但是我在网上找不到任何关于我的问题的信息。有人可以给我一些提示吗?
我不知道为什么我的问题被否决了,但我会为面临同样问题的其他人回答。
public class MediaService extends Service implements
MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener,
MediaPlayer.OnCompletionListener {
private IntentFilter intentFilterMediaButton = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
private MediaButtonIntentReceiver mMediaButtonReceiver = new MediaButtonIntentReceiver();
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
registerReceiver(this.mMediaButtonReceiver, this.intentFilterMediaButton);
player.start();
}
public static class MediaButtonIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
return;
}
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
return;
}
int action = event.getAction();
if (action == KeyEvent.ACTION_DOWN) {
switch (action){
case 0: // Button down
// do something
Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show();
break;
}
}
abortBroadcast();
}
}
主要调用这个 activity.onCreate()
AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE);
if (manager != null) {
manager.registerMediaButtonEventReceiver(new ComponentName(getPackageName(), MediaService.MediaButtonIntentReceiver.class.getName()));
}
并将其放入清单
<receiver android:name="MediaService$MediaButtonIntentReceiver" android:enabled="true">
<intent-filter android:priority="1000000000">
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
我编写了一个普通的MediaPlayer,比如APP。现在,我有一副带内置遥控器的耳机,可以控制音量、暂停和播放歌曲。 我的想法是用这个来控制我的 MediaPlayer 服务。但是我在网上找不到任何关于我的问题的信息。有人可以给我一些提示吗?
我不知道为什么我的问题被否决了,但我会为面临同样问题的其他人回答。
public class MediaService extends Service implements
MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener,
MediaPlayer.OnCompletionListener {
private IntentFilter intentFilterMediaButton = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
private MediaButtonIntentReceiver mMediaButtonReceiver = new MediaButtonIntentReceiver();
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
registerReceiver(this.mMediaButtonReceiver, this.intentFilterMediaButton);
player.start();
}
public static class MediaButtonIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
return;
}
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
return;
}
int action = event.getAction();
if (action == KeyEvent.ACTION_DOWN) {
switch (action){
case 0: // Button down
// do something
Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show();
break;
}
}
abortBroadcast();
}
}
主要调用这个 activity.onCreate()
AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE); if (manager != null) { manager.registerMediaButtonEventReceiver(new ComponentName(getPackageName(), MediaService.MediaButtonIntentReceiver.class.getName())); }
并将其放入清单
<receiver android:name="MediaService$MediaButtonIntentReceiver" android:enabled="true">
<intent-filter android:priority="1000000000">
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>