I have written an app that streaming a rtsp link.I have stream the url in my custom Service class.I want to show a proggresdialog while url is loading in the other words before start the music.Here is my codes;
public class MyMediaPlayerService extends Service implements OnCompletionListener{
private String path = "rtsp://someURL";
MediaPlayer mediaPlayer;
private ProgressDialog pd;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(this);
}
public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
}
public void surfaceDestroyed(SurfaceHolder surfaceholder) {
}
public void surfaceCreated(SurfaceHolder holder) {
}
public void onCompletion(MediaPlayer _mediaPlayer) {
stopSelf();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!mediaPlayer.isPlaying()) {
pd = new ProgressDialog(getApplicationContext());
pd.setMessage("Loading...");
pd.setCancelable(false);
pd.show();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(path);
mediaPlayer.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
mediaPlayer
.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
pd.dismiss();
mp.start();
}
});
}
return START_NOT_STICKY;
}
public void onDestroy() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
}
}
An also I am getting this errors on the log;
08-15 22:42:15.384: E/AndroidRuntime(1090): FATAL EXCEPTION: main
08-15 22:42:15.384: E/AndroidRuntime(1090): java.lang.RuntimeException: Unable to start service com.applogist.servis.MyMediaPlayerService@42417340 with Intent { cmp=com.applogist.standartfm/com.applogist.servis.MyMediaPlayerService }: android.view.WindowManager$BadTokenException: Unable to add window --
token null is not for an application
How to show a proggres dialog now?