我开发了一个新闻Android应用程序,这个应用程序使用MediaPlayer类来流式传输来自SoundCloud的新闻播放曲目,一切正常。现在让我们说用户从应用程序内部按下播放按钮,音乐开始播放。现在用户按下硬件主页按钮,应用程序转到后台或用户按下睡眠按钮关闭屏幕。我设法让音乐在后台播放,但我希望用户能够从应用程序外部控制MediaPlayer(暂停,停止,下一步,上一步操作)。
我搜索了很多但没有找到任何东西,我怎么能这样做?
我没有放我的代码,因为我觉得它无关紧要请告诉我你是否想看代码。
提前致谢。
答案 0 :(得分:1)
我已经通过添加带图片/按钮的自定义通知来控制Android通知栏中的音频播放和频道,从而做了类似的事情。
我认为这就是你所追求的目标。
通知代码:
public Notification buildNotification()
{
Intent intent = new Intent(mContext, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
setContentView();
Notification.Builder notibuilder = new Notification.Builder(mContext);
notibuilder.setContentTitle(" ");
notibuilder.setContentText(" ");
notibuilder.setSmallIcon(R.drawable.ic_launcher);
notibuilder.setOngoing(true);
notibuilder.setAutoCancel(false);
notibuilder.setContentIntent(pIntent);
notibuilder.setContent(mContentView);
notibuilder.setTicker(null);
mNotification = notibuilder.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
return mNotification;
}
public void setContentView()
{
mContentView = new RemoteViews(mContext.getPackageName(), R.layout.custom_notification);
mContentView.setTextViewText(R.id.notificaiton_app_title, "");
String currentChannel = " ";
if ( mCurrentChannel != -1)
currentChannel = " " + (mCurrentChannel + 1);
mContentView.setTextViewText(R.id.notificaiton_app_channel, currentChannel);
Intent previousIntent = new Intent(mContext, NotificationButtonIntentService.class);
previousIntent.setAction(NotificationButtonIntentService.Action_Previous);
mContentView.setOnClickPendingIntent(R.id.notification_layout_previous, PendingIntent.getService(mContext, 0, previousIntent, PendingIntent.FLAG_CANCEL_CURRENT));
mContentView.setOnClickPendingIntent(R.id.notification_image_previous, PendingIntent.getService(mContext, 0, previousIntent, PendingIntent.FLAG_CANCEL_CURRENT));
Intent playIntent = new Intent(mContext, NotificationButtonIntentService.class);
if ( mCurrentChannel != -1)
{
playIntent.setAction(NotificationButtonIntentService.Action_Pause);
mContentView.setInt(R.id.notification_image_play_pause, "setBackgroundResource", R.drawable.ic_media_pause);
}
else
{
playIntent.setAction(NotificationButtonIntentService.Action_Play);
mContentView.setInt(R.id.notification_image_play_pause, "setBackgroundResource", R.drawable.ic_media_play);
}
mContentView.setOnClickPendingIntent(R.id.notification_layout_play_pause, PendingIntent.getService(mContext, 0, playIntent, PendingIntent.FLAG_CANCEL_CURRENT));
mContentView.setOnClickPendingIntent(R.id.notification_image_play_pause, PendingIntent.getService(mContext, 0, playIntent, PendingIntent.FLAG_CANCEL_CURRENT));
Intent nextIntent = new Intent(mContext, NotificationButtonIntentService.class);
nextIntent.setAction(NotificationButtonIntentService.Action_Next);
mContentView.setOnClickPendingIntent(R.id.notification_layout_next, PendingIntent.getService(mContext, 0, nextIntent, PendingIntent.FLAG_CANCEL_CURRENT));
mContentView.setOnClickPendingIntent(R.id.notification_image_next, PendingIntent.getService(mContext, 0, nextIntent, PendingIntent.FLAG_CANCEL_CURRENT));
Intent closeIntent = new Intent(mContext, NotificationButtonIntentService.class);
closeIntent.setAction(NotificationButtonIntentService.Action_Close);
mContentView.setOnClickPendingIntent(R.id.notification_layout_close, PendingIntent.getService(mContext, 0, closeIntent, PendingIntent.FLAG_CANCEL_CURRENT));
mContentView.setOnClickPendingIntent(R.id.notification_image_close, PendingIntent.getService(mContext, 0, closeIntent, PendingIntent.FLAG_CANCEL_CURRENT));
}
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true" >
<ImageView
android:id="@+id/notification_app_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="12dp"
android:src="@drawable/ic_stat_zipstreamer" />
<TextView
android:id="@+id/notificaiton_app_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TextView
android:id="@+id/notificaiton_app_channel"
style="@style/notification.channel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ems="2" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/notification_layout_previous"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<ImageButton
android:id="@+id/notification_image_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/ic_media_previous" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/notification_layout_play_pause"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<ImageButton
android:id="@+id/notification_image_play_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/ic_media_play" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/notification_layout_next"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<ImageButton
android:id="@+id/notification_image_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/ic_media_next" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/notification_layout_close"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<ImageButton
android:id="@+id/notification_image_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/ic_media_close" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
意图服务:
public class NotificationButtonIntentService extends IntentService
{
public static final String TAG = "NotificationButtonIntentService";
public static final String Action_Play = "play";
public static final String Action_Pause = "pause";
public static final String Action_Previous = "previous";
public static final String Action_Next = "next";
public static final String Action_Close = "close";
public NotificationButtonIntentService()
{
super("");
}
public NotificationButtonIntentService(String name)
{
super(name);
}
@Override
protected void onHandleIntent(Intent intent)
{
String action = intent.getAction();
String realAction = intent.getAction();
if (realAction != null)
{
if (realAction.equals(Action_Play))
{
EventBus.getDefault().post(new PlayControlEvent(PlayControlEvent.PlayAction.PLAY));
}
else if (realAction.equals(Action_Pause))
{
EventBus.getDefault().post(new PlayControlEvent(PlayControlEvent.PlayAction.PAUSE));
}
else if (realAction.equals(Action_Close))
{
EventBus.getDefault().post(new ShutdownEvent());
}
else if ( realAction.equals(Action_Next))
{
EventBus.getDefault().post(new PlayControlEvent(PlayControlEvent.PlayAction.NEXT_CHANNEL));
}
else if ( realAction.equals(Action_Previous))
{
EventBus.getDefault().post(new PlayControlEvent(PlayControlEvent.PlayAction.PREVIOUS_CHANNEL));
}
}
}
}
答案 1 :(得分:0)
您需要访问MediaPlayer对象。使其静态并创建静态暂停,启动等方法。像这样的东西:
public class myMediaPlayer{
static MediaPlayer mediaPlayer = null;
public static void start(final Context context, Uri media){
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(context, media);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MEDIA);
mediaPlayer.prepare();
mediaPlayer.start();
}
public static void stop() {
if(mediaPlayer!=null)
mediaPlayer.release();
}