要实现Android Exo Player在用户离开后继续播放的功能,可以使用Service来管理Exo Player的播放状态。以下是一个示例代码,演示如何在用户离开应用后,通过Service继续播放音频。
AudioPlayerService
。public class AudioPlayerService extends Service {
private SimpleExoPlayer player;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
player = new SimpleExoPlayer.Builder(getApplicationContext()).build();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null && intent.getAction() != null) {
if (intent.getAction().equals(Constants.ACTION_PLAY)) {
String audioUrl = intent.getStringExtra(Constants.EXTRA_AUDIO_URL);
playAudio(audioUrl);
} else if (intent.getAction().equals(Constants.ACTION_PAUSE)) {
pauseAudio();
} else if (intent.getAction().equals(Constants.ACTION_RESUME)) {
resumeAudio();
} else if (intent.getAction().equals(Constants.ACTION_STOP)) {
stopAudio();
}
}
return START_STICKY;
}
private void playAudio(String audioUrl) {
Uri uri = Uri.parse(audioUrl);
MediaItem mediaItem = MediaItem.fromUri(uri);
player.setMediaItem(mediaItem);
player.prepare();
player.play();
}
private void pauseAudio() {
player.pause();
}
private void resumeAudio() {
player.play();
}
private void stopAudio() {
player.stop();
stopSelf(); // 停止Service
}
@Override
public void onDestroy() {
super.onDestroy();
player.release();
}
}
public class MainActivity extends AppCompatActivity {
private Intent serviceIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serviceIntent = new Intent(this, AudioPlayerService.class);
serviceIntent.setAction(Constants.ACTION_PLAY);
serviceIntent.putExtra(Constants.EXTRA_AUDIO_URL, "音频URL");
// 启动Service
startService(serviceIntent);
// 控制播放
Button playButton = findViewById(R.id.play_button);
Button pauseButton = findViewById(R.id.pause_button);
Button resumeButton = findViewById(R.id.resume_button);
Button stopButton = findViewById(R.id.stop_button);
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
serviceIntent.setAction(Constants.ACTION_PLAY);
startService(serviceIntent);
}
});
pauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
serviceIntent.setAction(Constants.ACTION_PAUSE);
startService(serviceIntent);
}
});
resumeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
serviceIntent.setAction(Constants.ACTION_RESUME);
startService(serviceIntent);
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
serviceIntent.setAction(Constants.ACTION_STOP);
startService(serviceIntent);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
// 停止Service
stopService(serviceIntent);
}
}
public class Constants {
public static final String ACTION_PLAY = "com.example.app.ACTION_PLAY";
public static final String ACTION_PAUSE = "com.example.app.ACTION_PAUSE";
public static final String ACTION_RESUME = "com.example.app.ACTION_RESUME";
public static final String ACTION_STOP = "com.example.app.ACTION_STOP";
public static final String EXTRA_AUDIO_URL = "com.example.app.EXTRA_AUDIO_URL";
}
这样,当用户离开应用时,Service仍然在后台运行,Exo Player会继续播放音频。用户可以通过发送不同的Action来控制播放状态。