3

I am currently developing a Streaming Radio app for one of clients, The app is almost done and working fine. (Let's say work fine when I test it). But the problem is it is not working on my clients phone at all. He has a Galaxy note 5 and he is in USA, (I am in sri lanka). I tested the app with many phones, (Galaxy note edge, HTC one M8, Galaxy s5, Galaxy s4, Oneplus one, Galaxy note 3, Huawei honor 3c, Huawei ascend). It is working perfect on all those phones. But it is not working when he is testing it. He said it took more than 30 mins to start the paltyer (OMG). I called Media Player via foreground Service, here is my code of media player Service.

public class RadioService extends Service implements MediaPlayer.OnBufferingUpdateListener,MediaPlayer.OnErrorListener {

public static final String ACTION_PLAY = "com.example.action.PLAY";
static MediaPlayer mMediaPlayer = null;
public static ChannelDetails channelDetails;
public static int playState =0;
public static boolean isRunning;

public void initMedeaPlayer(){
    Log.d("RadioService", "initMediaPlayer");

    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.reset();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getAction().equals(ACTION_PLAY) && intent.getExtras().getSerializable("channelDetails")!=null) {
        Log.d("RadioService", "OnStartCommand");

      try{
          channelDetails = (ChannelDetails)intent.getExtras().getSerializable("channelDetails");
          initMedeaPlayer();
          playMusic(null);
          foregroundService();
          isRunning = true;

      }catch(Exception e){
          e.printStackTrace();
      }
    }
    return super.onStartCommand(intent, flags, startId);
}

public static void playMusic(ChannelDetails cn) {
   try{
       if(cn!=null){
           channelDetails = cn;
           mMediaPlayer.reset();
       }else{

       }
       mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
       mMediaPlayer.setDataSource(channelDetails.getUrl());
       mMediaPlayer.prepareAsync(); 
       mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
           @Override
           public void onPrepared(MediaPlayer mp) {
               playState = 1;
               Log.d("RadioService", "OnPrepared");

               mp.start();
           }
       });
   }catch(Exception e){
       e.printStackTrace();
   }
}

public static void pauseMusic() {
    try{
       mMediaPlayer.pause();
        playState=2;

    }catch(Exception e){
        e.printStackTrace();
    }
}

public static void simplePlayMusic() {
    try{
        mMediaPlayer.start();
        playState=1;

    }catch(Exception e){
        e.printStackTrace();
    }
}

@Override
public void onCreate() {
    super.onCreate();
    Log.d("RadioService", "OnCreate");

}

@Override
public void onDestroy() {
    super.onDestroy();
    mMediaPlayer.stop();
    mMediaPlayer.release();
    mMediaPlayer=null;
    isRunning = false;
    Log.d("RadioService", "Destroyed");
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {

}

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
    return false;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}


public void foregroundService(){
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), MainPage.class), PendingIntent.FLAG_UPDATE_CURRENT);

    Notification notification = new Notification.Builder(getApplicationContext())
            .setTicker("Radio.lk")
            .setContentTitle(channelDetails.getChannelName())
            .setContentText("Online Streaming")
            .setSmallIcon(R.mipmap.ic_launcher)
            .addAction(R.mipmap.play, "Play", pi)
            .addAction(R.mipmap.pause, "Pause", pi)
            .addAction(R.mipmap.stop, "Stop", pi)
            .setContentIntent(pi).getNotification();
    notification.flags |= Notification.FLAG_ONGOING_EVENT;
    NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    nm.notify(550,notification);
    startForeground(550, notification);
}

}
  • Media player is a very buggy class. There are issues when it buffers a lot of data before playing. Also prepareAsync is not reliable. my suggestion is to shift the logic to a separate thread so that you can use prepare and not rely on setOnPreparedListener. – Dexter Nov 22 '15 at 09:27
  • Thanks man,You mean that i have to do the buffering by myself other than `mMediaPlayer.prepareAsync();` right? – Chathuranga Rathnayake Nov 22 '15 at 19:08
  • I'm having the same exact problem. I'll let you know if I come up with a solution. Just commenting so you know you're not alone. – AutonomousApps Nov 22 '15 at 22:36
  • Possible solution (though somewhat unsatisfactory to me): http://stackoverflow.com/questions/32379922/android-mediaplayer-not-playing-on-galaxy-s6-with-android-5-1-1 – AutonomousApps Nov 22 '15 at 23:00
  • @AutonomousApps Thank god that i am not alone, Please let me know if there is any solution for that. And i will try the ExoPlayer as well. Only thing i don't have Note 5 device to test it on my own, that's why its so hard to solve the problem. thanks again. – Chathuranga Rathnayake Nov 23 '15 at 02:54

1 Answers1

-1

Try clearing out the cache on the app.....also try doing a quick restart on your phone if there might be an overheating issue. These are just a couple of quick tips, but visit fliptroniks.com for some more advanced tips and fixes for this issue