因此,我开发了一个使用exoplayer库来流音乐URL的android应用程序。但是,在创建后台服务以在后台启用音乐流后,它不起作用,但是当我尝试将其作为后台(MainActivity)时,相同的代码也可以工作。
Android清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.sparrowpaul.musicservice">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".BackgroundService"/>
</application>
MainActivity.Java
package com.sparrowpaul.musicservice;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
公共类MainActivity扩展了AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this, BackgroundService.class);
startService(intent);
}
}
BackgroundService.java
公共类BackgroundService扩展Service实现ExoPlayer.EventListener {
private Handler mainHandler;
private RenderersFactory renderersFactory;
private BandwidthMeter bandwidthMeter;
private LoadControl loadControl;
private DataSource.Factory dataSourceFactory;
private ExtractorsFactory extractorsFactory;
private MediaSource mediaSource;
private TrackSelection.Factory trackSelectionFactory;
private SimpleExoPlayer player;
private final String streamUrl = "http://bbcwssc.ic.llnwd.net/stream/bbcwssc_mp1_ws-einws";
private TrackSelector trackSelector;
@Override
public void onCreate() {
super.onCreate();
startPlayer();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startPlayer();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
public void startPlayer(){
renderersFactory = new DefaultRenderersFactory(getApplicationContext());
bandwidthMeter = new DefaultBandwidthMeter();
trackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(trackSelectionFactory);
loadControl = new DefaultLoadControl();
player = ExoPlayerFactory.newSimpleInstance(this, renderersFactory, trackSelector, loadControl);
player.addListener(this);
dataSourceFactory = new DefaultDataSourceFactory(getApplicationContext(), "ExoplayerDemo");
extractorsFactory = new DefaultExtractorsFactory();
mainHandler = new Handler();
mediaSource = new ExtractorMediaSource(Uri.parse(streamUrl),
dataSourceFactory,
extractorsFactory,
mainHandler,
null);
player.prepare(mediaSource);
}
}
您需要以foreground service的身份启动服务并为其提供通知。 ExoPlayer的ui模块带有PlayerNotificationManager which helps you with the notification。
大约需要start your service as as a foreground service in your activity。然后,一旦您的服务创建了通知,您就需要调用startForeground(notificationId,notification)。如果您不作为前台服务启动,则系统将在短时间内停止您的服务。另请参见:https://developer.android.com/guide/components/services#Foreground
下面的示例给出了有关如何使用ExoPlayers通知管理器的想法。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (player == null) {
startPlayer();
playerNotificationManager = new PlayerNotificationManager(
this, CHANNEL_ID, NOTIFICATION_ID,
createMediaDescriptionAdapter(),
new PlayerNotificationManager.NotificationListener() {
@Override
public void onNotificationPosted(int notificationId, Notification notification, boolean ongoing) {
startForeground(notificationId, notification);
}
@Override
public void onNotificationCancelled(int notificationId, boolean dismissedByUser) {
if (dismissedByUser) {
// Do what the app wants to do when dismissed by the user,
// like calling stopForeground(true); or stopSelf();
}
}
});
playerNotificationManager.setPlayer(player);
}
return START_STICKY;
}