gpt4 book ai didi

java - Android:从应用程序外部控制 MediaPlayer 实例(对象)?

转载 作者:行者123 更新时间:2023-12-01 11:46:42 25 4
gpt4 key购买 nike

我开发了一个新闻 Android 应用程序,该应用程序使用 MediaPlayer 类从 SoundCloud 流式传输新闻广播轨道,一切正常。现在假设用户从应用程序内部按下播放按钮,音乐开始播放。现在,用户按下硬件主页按钮,应用程序进入后台,或者用户按下 sleep 按钮关闭屏幕。我已经设法让音乐在后台播放,但我希望用户能够从应用程序外部控制 MediaPlayer(暂停、停止、下一个、上一个操作)。

我搜索了很多,但没有找到任何东西,我该怎么做?

我没有放置我的代码,因为我认为它不相关,如果您想查看代码,请告诉我。

提前致谢。

最佳答案

我通过添加带有图像/按钮的自定义通知来控制 Android 通知栏的音频播放和 channel ,从而完成了类似的操作。

认为这就是你所追求的。

通知代码:

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));
}
}
}
}

enter image description here

关于java - Android:从应用程序外部控制 MediaPlayer 实例(对象)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29085553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com