I'm working on a radio streaming app and the audio always stops while on background, after a few minutes.
Manifest:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Service class:
public class StreamService extends Service implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnInfoListener {
private static final String STREAM_URL = "http://bbcwssc.ic.llnwd.net/stream/bbcwssc_mp1_ws-einws";
MediaPlayer mediaPlayer = null;
private WifiManager.WifiLock mWifiLock;
public int onStartCommand(Intent intent, int flags, int startId) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(STREAM_URL);
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnInfoListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setLooping(true);
if (mWifiLock == null) {
mWifiLock = ((WifiManager) Objects.requireNonNull(getApplicationContext().getSystemService(Context.WIFI_SERVICE)))
.createWifiLock(WifiManager.WIFI_MODE_FULL, "mediaplayerlock");
}
mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mediaPlayer.prepareAsync();
return START_STICKY;
}
public void onPrepared(MediaPlayer player) {
Log.e(TAG, "Service onPrepared");
mediaPlayer.start();
}
On the MainActivity I start the stream:
Intent i = new Intent(this, StreamService.class);
i.setAction("play");
startService(i);
After a few minutes with the device locked it gives this error and stops the playback:
W/MediaPlayerNative: info/warning (703, 0)
W/MediaPlayerNative: info/warning (701, 0)
703 and 701 are related to buffering and playing the audio, but it halts everything for good, every single time, even with good wifi connection. If you keep the app open everything it works forever, but if you close the app in a few minutes this error shows up. Then you have to reopen the app, stop and restart the stream.
There is a warning about the stream, but everything on the code seems correct:
W/MediaPlayer: Use of stream types is deprecated for operations other than volume control
W/MediaPlayer: See the documentation of setAudioStreamType() for what to use instead with android.media.AudioAttributes to qualify your playback use case
Am I missing something? Any ideas?
Thanks!
EDIT:
Found out the onInfo gets called when when the stream starts and when I get 703 error:
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
Log.e(TAG, "Service onInfo");
return false;
}
When your app goes to the background, it will stop shortly after. To continue playing the audio, you'll have to start the service as a foreground service.
Manifest:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
Activity:
startForegroundService(i)
Service:
onCreate() {
super.onCreate()
startForeground(ID, createNotification())
}