Media Media Player service does not work after publishing

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(); // it takes so long, about a minute... } catch (IllegalArgumentException | SecurityException | IllegalStateException | IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } /** Called when MediaPlayer is ready */ public void onPrepared(MediaPlayer player) { mediaPlayer.start(); radio0.tabBridge.setStopView(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; } public void onDestroy() { if (mediaPlayer.isPlaying()) { mediaPlayer.stop(); mediaPlayer.release(); } wifiLock.release(); } public void onCompletion(MediaPlayer _mediaPlayer) { stopSelf(); } 

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" /> 
+6
source share
2 answers

There is no @Override annotation in onPrepared() . This is usually not a problem, but who knows.

+2
source

Well, I recommend that you use the EXOPlayer Library if you intend to use MediaPlayer to stream a file. The page also has a sample.

+1
source

All Articles