Reputation: 126
I'm building an audio streaming app where the users should be able to listen to their songs while the device is locked (as any other music app).
Some users are reporting a problem where the playback is being stopped after 10 to 15 minutes on WIFI. On all cases the device has been locked manually or entered the sleep mode. I'm able to reproduce this issue on a Pixel 2 running Android 8.1 and on the Emulator running 8.0. Some users using Nokia running 8.1 and LG running 7.0 are also experiencing the same issue. This problem is not happening when listening using mobile network (4g).
The steps to reproduce this problem are the following. - Start to stream a song; - Put the app in background; - Lock the device and wait until the playback stops;
Debugging the app I can see that I'm getting "java.net.SocketTimeoutException: failed to connect to [address]/[IP] (port 80) from /[local IP] (port 38149)". I'm assuming that the wifi is being disconnected (but still enabled) for some reason after the device entered the sleep mode and I'm unable to perform the HTTP requests that I need before playing each track. The HTTP requests are being triggered by the MediaPlayer completion listener and run on a different thread. I'm not using JobScheduler or AlarmManager.
Everything is running on a foreground service that shows a persistent notification to the users (media controls).
I'm also setting a Wifi Lock and a Wake Lock when the service is created. Also checking if the lock is being held before playing each track just to make sure.
WifiManager wifiManager = ((WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE));
if (wifiManager != null) {
wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "myapp.com.wifilock");
wifiLock.acquire();
}
PowerManager powerManager = ((PowerManager) getSystemService(Context.POWER_SERVICE));
if (powerManager != null) {
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "myapp.com.wake_lock");
wakeLock.acquire();
}
I also tried to set the MediaPlayer wake mode, but nothing changed.
mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK)
I was able to reproduce this on the emulator by using ADB.
$ adb shell dumpsys battery unplug
$ adb shell dumpsys deviceidle step
Also used "dumpsys" to check WIFI status
$ adb shell dumpsys wifi
Wi-Fi is enabled
Stay-awake conditions: 1
mInIdleMode true
mScanPending false
WifiController:
total records=11
rec[0]: time=04-13 14:00:54.690 processed=ApStaDisabledState org=ApStaDisabledState dest=<null> what=155656(0x26008)
rec[1]: time=04-13 14:00:55.198 processed=ApStaDisabledState org=ApStaDisabledState dest=<null> what=155659(0x2600b)
rec[2]: time=04-13 14:00:55.198 processed=ApStaDisabledState org=ApStaDisabledState dest=DeviceActiveState what=155656(0x26008)
rec[3]: time=04-13 14:00:57.520 processed=DefaultState org=DeviceActiveState dest=<null> what=155652(0x26004)
rec[4]: time=04-13 14:00:57.558 processed=DefaultState org=DeviceActiveState dest=<null> what=155652(0x26004)
rec[5]: time=04-13 14:00:51.045 processed=DeviceActiveState org=DeviceActiveState dest=<null> what=155660(0x2600c)
rec[6]: time=04-13 14:01:02.743 processed=DeviceActiveState org=DeviceActiveState dest=<null> what=155660(0x2600c)
rec[7]: time=04-13 14:01:44.845 processed=DefaultState org=DeviceActiveState dest=<null> what=155654(0x26006)
rec[8]: time=04-13 14:03:03.801 processed=DefaultState org=DeviceActiveState dest=<null> what=155652(0x26004)
rec[9]: time=04-13 14:03:16.009 processed=DefaultState org=DeviceActiveState dest=<null> what=155650(0x26002)
rec[10]: time=04-13 14:03:16.029 processed=DefaultState org=DeviceActiveState dest=<null> what=155651(0x26003)
curState=DeviceActiveState
mScreenOff true
mDeviceIdle false
mPluggedType 0
mIdleMillis 900000
mSleepPolicy 2
mPersistWifiState 1
mAirplaneModeOn false
mNotificationEnabled true
mNotificationRepeatTime 0
mNotificationShown false
mNumScansSinceNetworkStateChange 0
mEnableTrafficStatsPoll false
mTrafficStatsPollToken 10
mTxPkts 5031
mRxPkts 10191
mDataActivity 0
Locks held:
Locks acquired: 0 full, 1 full high perf, 0 scan
Locks released: 0 full, 0 full high perf, 0 scan
Locks held:
WifiLock{myapp.com.wifilock type=3 uid=10100}
mMulticastEnabled 1
mMulticastDisabled 1
Multicast Locks held:
As we can see, the Wifi Lock is still active. I also confirmed that the Wake Lock was also active. "mSleepPolicy 2" should keep the Wifi alive even when the device entered the sleep mode, if I'm not wrong.
I don't wan't to whitelist the app on the battery optimization panel. I can see that other streaming apps, like Spotify, are optimized and have no problems streaming while the device is locked.
Am I missing something?
Upvotes: 0
Views: 1123