I am using the media planner service to stream mp3 from url. After reading all the documentation and dozens of posts, the following is my last code. This code works correctly on all emulators, but it only works on some devices and, strange behavior, some of them with the same OS (for example, kat kit), works in some and others, but not ... Errors were debugged and looking to the logarithm, it seems that prepareAsync () does the right thing, but never throws onPrepared (). After the publication of the application, test devices operating during debugging also do not play music!
public class backgroundAudio extends Service implements MediaPlayer.OnPreparedListener { MediaPlayer mediaPlayer; WifiLock wifiLock; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { mediaPlayer = null; String url = "http://www.myserver.com/mystream.mp3"; mediaPlayer = new MediaPlayer(); mediaPlayer.setOnPreparedListener(this); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK); wifiLock = ((WifiManager) getSystemService(Context.WIFI_SERVICE)).createWifiLock(WifiManager.WIFI_MODE_FULL, "mylock"); wifiLock.acquire(); try { mediaPlayer.setDataSource(url); mediaPlayer.prepareAsync();
manifest manifest permissions
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/> <uses-permission android:name="android.permission.WAKE_LOCK" />
Jaume source share