Android的,如何停止服务暂停的应用时播放音乐, [英] Android, How to stop Service playing music when pausing app ,

查看:232
本文介绍了Android的,如何停止服务暂停的应用时播放音乐,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个简单的Andr​​oid应用程序,它有两个活动和一个IntentService。我IntentService播放音乐在每个活动(这就是我想要的),但如果我离开应用程序的音乐仍然扮演(例如:如果我preSS家庭按钮,它会带我到桌面上,把我的活动中的onPause状态但是从该应用程序的音乐仍在播放)。任何帮助将是AP preciated ....

code以下

主要活动

 公共类MainActivity延伸活动{@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    意图serviceIntent =新意图(这一点,BackgroundMusic.class);
    startService(serviceIntent);}
公共无效ShipPick(查看视图){    意图activityIntent =新意图(这一点,ShipChoiceActivity.class);
    startActivity(activityIntent);}
}

背景音乐服务

 公共类背景音乐扩展IntentService {MediaPlayer的熔点; 公共背景音乐(){
      超级(背景音乐);
  }
 处理器HN =新的处理程序();
 私有类PlayMusic实现Runnable {       公共无效PLayMusic(){       }       公共无效的run(){
           熔点= MediaPlayer.create(getApplicationContext(),R.raw.musicfile);
        mp.start();
       }
 } @覆盖
 保护无效onHandleIntent(意向意图){    HN.post(新PlayMusic()); } 公共无效的onPause(){        mp.pause();
        } 公共无效onResume(){     mp.start();
     } 保护无效的onStop(){     mp.stop();
     熔点=无效;
     }
 }

第二项活动

 公共类ShipChoiceActivity延伸活动{保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_ship_choice);
}}

清单

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=com.example.starwars
安卓版code =1
机器人:=的versionName1.0><用途-SDK
    安卓的minSdkVersion =8
    机器人:targetSdkVersion =18/><应用
    机器人:allowBackup =真
    机器人:可调试=真
    机器人:图标=@绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme>
     <服务机器人:名字=背景音乐/>    <活动
        机器人:名字=com.example.ship.MainActivity
        机器人:标签=@字符串/ APP_NAME>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MAIN/>            <类机器人:名字=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
    <活动机器人:标签=@字符串/ APP_NAME机器人:名字=ShipChoiceActivity/>
< /用途>< /清单>


解决方案

有关的活动控制背景音乐服务,您将需要使用自定义广播接收器时,应用程序的onPause状态将与服务进行通信。

注册广播接收器在背景音乐:

 公共类MusicServiceBroadCast扩展广播接收器{     @覆盖
       公共无效的onReceive(上下文的背景下,意图ARG1){                    如果(小于匹配的作用>){
                      如果行动是暂停,然后调用暂停的MediaPlayer
                    }其他{
                      如果行动的游戏...
                    }
        }    }
 @覆盖
 保护无效onHandleIntent(意向意图){
    // ...注册广播接收器
    registerReceiver(新MusicServiceBroadCast(),新的IntentFilter(
     com.xx.PAUSE_MUSIC_ACTION));
    HN.post(新PlayMusic()); }

从活动的onPause 发送广播接收器:

 意向意图=新的Intent();
 intent.setAction(com.xx.PAUSE_MUSIC_ACTION);
 sendBroadcast(意向);

I am building a simple android application and it has two Activities and a IntentService. My IntentService plays music over every activity(thats what I want) but if I leave the app the music still plays (example: if I press the home button it will take me to the desktop, putting my activity in the onpause state but the music from that app is still playing). Any help would be appreciated....

code below

Main Activity

public class MainActivity extends Activity  {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent serviceIntent= new Intent(this, BackgroundMusic.class);
    startService(serviceIntent);



}




public void ShipPick(View view){

    Intent activityIntent= new Intent(this, ShipChoiceActivity.class);
    startActivity(activityIntent);

}


}

Background Music Service

public class BackgroundMusic extends IntentService {

MediaPlayer mp;

 public BackgroundMusic() {
      super("BackgroundMusic");
  }


 Handler HN = new Handler(); 


 private class PlayMusic implements Runnable {



       public void PLayMusic(){

       }

       public void run(){
           mp = MediaPlayer.create(getApplicationContext(), R.raw.musicfile);
        mp.start();
       }
 }

 @Override
 protected void onHandleIntent(Intent intent) {

    HN.post(new PlayMusic()); 

 }

 public void onPause() {

        mp.pause();
        }

 public void onResume() {

     mp.start();
     }

 protected void onStop() {

     mp.stop();
     mp = null;
     }


 }

second activity

public class ShipChoiceActivity extends Activity {



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ship_choice);


}

}

manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.starwars"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:debuggable="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
     <service android:name=".BackgroundMusic" />

    <activity
        android:name="com.example.ship.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:label="@string/app_name" android:name="ShipChoiceActivity"/>
</application>

</manifest>

解决方案

For controlling BackgroundMusic Service from Activity you will need to use custom BroadcastReceiver to communicate with service when application is going in onpause state.

Register BroadcastReceiver In BackgroundMusic :

    public class MusicServiceBroadCast extends BroadcastReceiver {

     @Override
       public void onReceive(Context context, Intent arg1) {

                    if(<Match for action>){
                      if Action is for pause then call pause for MediaPlayer
                    }else{
                      if Action for Play ...
                    }
        }

    } 
 @Override
 protected void onHandleIntent(Intent intent) {
    //... Register BroadcastReceiver
    registerReceiver(new MusicServiceBroadCast(), new IntentFilter(
     "com.xx.PAUSE_MUSIC_ACTION"));
    HN.post(new PlayMusic()); 

 }

Send BroadcastReceiver from Activity onPause :

 Intent intent = new Intent();
 intent.setAction("com.xx.PAUSE_MUSIC_ACTION");
 sendBroadcast(intent);

这篇关于Android的,如何停止服务暂停的应用时播放音乐,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆