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);
}
}