我尝试创建使用媒体按钮操作的通知,但如何发送和接收这些按钮?这是我到目前为止所拥有的......
if(mediaSession==null) {
mediaSession = new MediaSession(this, "Boom");
}
//Album art
ImageView img = (ImageView)findViewById(R.id.img_AlbumArt1);
Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
PendingIntent mPauseIntent = PendingIntent.getBroadcast(this, 0,
new Intent(ACTION_PAUSE).setPackage(this.getPackageName()),
PendingIntent.FLAG_CANCEL_CURRENT);
Notification.Builder mBuilder =
new Notification.Builder(this)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_media_play)
.setLargeIcon(bitmap)
.setContentTitle(playingTitle)
.setContentText(playingArtist)
.setStyle(new Notification.MediaStyle()
.setMediaSession(mediaSession.getSessionToken())
.setShowActionsInCompactView(0,1,2)
)
// Add media control buttons that invoke intents in your media service
.addAction(R.drawable.ic_media_rew, "Previous", null) //#0
.addAction(R.drawable.ic_media_pause, "Play/Pause", null) //#1
.addAction(R.drawable.ic_media_ff, "Next", null) //#2 nextPendingIntent
;
// Creates an explicit intent for an Activity in your app
Intent intent = new Intent(this, Music.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_MUSIC);
// Creates an explicit intent for an Activity in your app
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,PendingIntent.FLAG_CANCEL_CURRENT,intent,0);
mBuilder.setContentIntent(pendingIntent);
NotificationManager NotiMan=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
NotiMan.notify(notification, mBuilder.build());
Android docs建议pendingIntent
暂时设置null
,但我还没有弄清楚如何为此目的制作PendingIntent
广播接收器:
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final CharSequence action = intent.getAction();
if(action==ACTION_PAUSE) {
//mTransportControls.pause();
Log.i("ON RECEIVE", "ACTION_PAUSE");
}
}
};
在onCreate:
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_PAUSE);
this.registerReceiver(mMessageReceiver, filter);
答案 0 :(得分:1)
-(BOOL)microphoneInput:(BOOL)enable;
{
UInt32 enableInput = (enable)? 1 : 0;
OSStatus status = AudioUnitSetProperty(
ioUnit,//our I/O unit
kAudioOutputUnitProperty_EnableIO, //property we are changing
kAudioUnitScope_Input,
kInputBus, //#define kInputBus 1
&enableInput,
sizeof (enableInput)
);
CheckStatus(status, @"Unable to enable/disable input");
return (status == noErr);
}
需要指向PendingIntent
或可以对指定请求执行操作的服务。例如,对于BroadcastReceiver
,您将有一个看起来像
BroadcastReceiver
接下来,注册action / pendingIntent
public static final String ACTION_PAUSE = "com.pkg.name.ACTION_PAUSE";
mPauseIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE,
new Intent(ACTION_PAUSE).setPackage(pkg),
PendingIntent.FLAG_CANCEL_CURRENT);
最后,当触发pendingIntent时,您将处理该操作。在这里,您可以选择使用 .addAction(R.drawable.ic_media_pause, mContext.getString(R.string.label_previous), mPendingIntent);
来播放/暂停操作。
MediaController.TransportControls
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
switch (action) {
case ACTION_PAUSE:
mTransportControls.pause();
break;
...
}
应该被实例化并设置为对播放/暂停+其他操作起作用。
MediaSession
最后扩展mSession = new MediaSession(this, "Service");
mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
并实现MediaSession.MediaSessionCallback
..这实际上会对播放起作用。