2017-10-19 123 views
0

我试图播放音乐与后台服务。首先,我对播放和暂停音乐toggle buttonMainActivity,我创建 BackgroundSoundService只是播放音乐的所有活动不能在后台播放:播放音乐与后台服务

public class BackgroundSoundService extends Service { 

private static final String TAG = "BackgroundSoundService"; 
MediaPlayer player; 

public IBinder onBind(Intent arg0) { 
    Log.i(TAG, "onBind()"); 
    return null; 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    player = MediaPlayer.create(this, R.raw.vaporv2); 
    player.setLooping(true); // Set looping 
    player.setVolume(100,100); 
    Log.i(TAG, "onCreate() , service started..."); 

} 
public int onStartCommand(Intent intent, int flags, int startId) { 
    player.start(); 
    return Service.START_STICKY; 
} 

public IBinder onUnBind(Intent arg0) { 
    Log.i(TAG, "onUnBind()"); 
    return null; 
} 
public void onStop() { 
    Log.i(TAG, "onStop()"); 
} 
public void onPause() { 
    if(player!=null && player.isPlaying()){ 
     player.pause(); 
    } 
    Log.i(TAG, "onPause()"); 
} 
@Override 
public void onDestroy() { 
    player.stop(); 
    player.release(); 
    Log.i(TAG, "onCreate() , service stopped..."); 
} 

@Override 
public void onLowMemory() { 
    Log.i(TAG, "onLowMemory()"); 
} 
    } 

MainActivity

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    MusicButton = (ToggleButton) findViewById(R.id.toggleButton); 

    MusicButton.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if (MusicButton.isChecked()) { 
       //mediaPlayer.pause(); 
       Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class); 
       startService(myService); 
      } else { 
       //mediaPlayer.start(); 
       Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class); 
       stopService(myService); 
      } 
     } 
    }); 

当我暂停音乐并再次播放时,会出现问题。音乐从一开始就开始,当我希望音乐从停止播放的地方继续。我的第二个问题是我不想在后台播放音乐,我想在应用程序处于后台时停止播放音乐,请提供任何帮助。

+0

查看此[链接](https://stackoverflow.com/questions/27685889/how-to-stop-service-of-music-background)停止音乐时,它在后台。 – Thracian

回答

3

时,我不知道你的onPause()onStop()是为了做。但是,当您首次使用Context.startService()启动服务时,将调用该服务的onCreate()方法,然后调用onStartCommand(),稍后再次调用startService()时,只会调用onStartCommand()。因此,无论出于何种原因,如果您想在服务中播放声音并在相同的服务中暂停播放,则需要提供指定您要执行的操作的服务Action

所以在你的活动时,要告诉服务打了声:

String action = "PLAY"; 
Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class); 
myService.setAction(action); 
startService(myService); 

,并暂停了声:

String action = "PAUSE"; 
Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class); 
myService.setAction(action); 
startService(myService); 

,并在您的服务onStartCommand()方法:

if (intent.getAction().equals("PLAY")) { 
    // resume the sound 
} 
if (intent.getAction().equals("PAUSE")) { 
    // pause the sound 
} 

当你真的需要停止服务的含义Destroy服务,请致电context.stopService(),然后onDestroy()方法被调用,服务真的被破坏。

+0

非常感谢它的工作,但音乐仍然在后台播放我把这段代码'意图myService =新的意图(MainActivity.this,BackgroundSoundService.class);在我的'onDestroy()'主要活动中使用getApplicationContext()。startService()' –

+0

请音乐在后台播放 –

+1

@ S.Queroane我不确定你的问题到底是什么。你想在你的服务被破坏时停止音乐吗? – szamani20

0

为了防止在后台覆盖玩活动暂停,然后暂停您服务那里,恢复你离开保存偏好的当前位置在哪里,然后检索它需要

相关问题