[英]How to change the media player from a different activity
I was wondering if I can declare a media player variable in one activity and then pause or stop it in a separate activity. 我想知道是否可以在一个活动中声明一个媒体播放器变量,然后在另一个活动中暂停或停止它。 How would I go about this or is there another way?
我将如何处理?还是有其他方法? Thanks
谢谢
Either you can use static variable of MediaPlayer
in your activity, so that you can access your media player by using YourActivityName.mediaplayer.stop()
您可以在活动中使用
MediaPlayer
静态变量,以便可以通过使用YourActivityName.mediaplayer.stop()
访问媒体播放器。
or 要么
use a service
class 使用
service
类别
I prefer service
class 我更喜欢
service
舱
I am not a fan of static vars. 我不喜欢静态变量。 I'd prefer doing something like this
我更喜欢做这样的事情
Android Manifest Android清单
<activity name="Player" android:launchMode="singleTop"/>
This former ensures that you have only one instance of the activity running, and that all intents leading to starting that activity are delivered via its onNewIntent()
前者确保您只有一个正在运行的活动实例,并且所有导致启动该活动的意图都通过其
onNewIntent()
传递
class Player extends Activity{
public static final String ACTION_PLAY = "com....PLAY";
public static final String ACTION_PAUSE = "com...PAUSE";
public void onNewIntent(Intent intent){
if(intent.getAction().equals(ACTION_PLAY)){
//Play
}
else if(intent.getAction().equals(ACTION_PAUSE){
//Pause
}
}
}
And from the calling activity, you could invoke 从调用活动中,您可以调用
Intent playIntent = new Intent(this, Player.class);
playIntent.setAction(Player.ACTION_PLAY);
and 和
Intent pauseIntent = new Intent(this, Player.class);
pauseIntent.setAction(Player.ACTION_PAUSE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.