且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android的,如何停止服务暂停的应用时播放音乐,

更新时间:2023-12-04 08:37:40

有关的活动控制背景音乐服务,您将需要使用自定义广播接收器时,应用程序的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);