0

I'm having a problem with my app when I try to stream musics on background and it only occurs on some devices.

When the app plays any music on foreground it works without problems, but in some devices, when I press the power button, the stream immediately loses its quality (looks like when I'm on a low speed internet connection). When I turn on the screen the stream gets better again.

I've already tried WakeLocks but it didn't work.


Edit 1: This is how I used the wake locks:

OnCreate of my activity:

//Setting the wakelock
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");

and than I do this when the music starts:

wl.acquire();

and this when the stream stops:

if(wl.isHeld()){
    wl.release();
}

Edit 2:

Tried this as well:

mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);

Edit 3:

Tried with WiFi Locks too:

//Setting the proper lockMode depending on the android version:
int wifiLockMode = WifiManager.WIFI_MODE_FULL;

int sdkVersion = Build.VERSION.SDK_INT;

//WIFI_MODE_FULL_HIGH_PERF was added on Android 3.1 so 
//I need to implement this to make sure the wifi will execute on its full power(even if it consumes more battery)
if (sdkVersion >= Build.VERSION_CODES.HONEYCOMB_MR1) {
wifiLockMode = WifiManager.WIFI_MODE_FULL_HIGH_PERF;
}

//Setting the WifiLock
WiFiManager wm = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 
WifiLock mWiFiLock = wm.createWifiLock(wifiLockMode, "MyFlag");
mWiFiLock.acquire();

//Releasing the WifiLock
if(mWiFiLock.isHeld()){
    mWiFiLock.release();
}

With the WifiLock it seems to be a little better(or I'm getting used to the interrupted sound)

Any ideas?

jMelnik
  • 1,055
  • 2
  • 15
  • 26
  • Can you provide the source code of the way you are using the WakeLock mechanism ? – devMatt Oct 08 '12 at 13:48
  • are you using a service - normally that is the way to go if you want to run something in the background ... I think ;-) – dorjeduck Oct 08 '12 at 14:04
  • Is your app requesting the `WAKE_LOCK` permission? Also, if you're using MediaPlayer, it has a built-in mechanism for acquiring the WakeLock for you: http://stackoverflow.com/questions/6433185/using-wakelock-to-keep-a-stream-playing – acj Oct 08 '12 at 14:39
  • Still the same with the `setWakeMode()` but I'll take another look at the `MediaPlayer` documentation that was on that link and see if I can find my flaw – jMelnik Oct 08 '12 at 16:14
  • Are you seeing any `WakeLock`-related errors in LogCat? If the lock is failing, you should see some useful messages in there. – acj Oct 08 '12 at 17:14
  • No messages at all...just the streaming getting interrupted like if it is over a bad wifi signal – jMelnik Oct 08 '12 at 19:54

2 Answers2

0

I had the same quality issues with Nexus 4 using android 4.3 and discovered that the problem was solved if I turned off the "wifi optimization": Wi-Fi-> Advanced -> Wi-Fi optimization.

Seems consistent with this other threads:

https://code.google.com/p/android/issues/detail?id=42272#c319 http://forum.xda-developers.com/showthread.php?t=2072930&page=55

If this is true, wifi lock is not preventing the wifi optimization. I also tested that the wake lock is not required to stream quality music with screen turned off, at least with Nexus 4.

ollbap
  • 96
  • 5
0

I think that you cannot do something really because I think that each OEM rewrite it's "battery optimization" rules that will try to preserve battery when the screen is OFF.

The best thing to do is to follow Android Standard about MediaPlayer's wakelock system and cross your finger :)

StErMi
  • 5,389
  • 5
  • 48
  • 71