2

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?

emreturka
  • 846
  • 3
  • 18
  • 44
  • Check this http://stackoverflow.com/questions/4327709/showing-progressdialog-while-a-service-is-being-started – Pavlos Aug 15 '13 at 23:00
  • Service is meant for a task in the background. Why would you want to show a progress dialog when you are doing something in background. – Gaurav Agarwal Aug 15 '13 at 23:12
  • Because of the loading. While stream is loading I want to show proggres dialog.If the stream is playing I will dismiss the progres dialog – emreturka Aug 16 '13 at 13:01
  • How you solved this issue?Now I have the same problem. – Ann Jan 18 '14 at 05:47

1 Answers1

0

It is advised to do that using Notification Service|Sending Notifications to the User by the official developer guide.

Also, if you need to play music in service, you need to do it in another thread.

Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.

However, if you insist to show a dialog and your music is played only after all is downloaded. I think you can use an dialog class which has an inner AsyncTask class. You can do the downloading task in the AsyncTask's doInBackground (Params... params) method and update the UI of the dialog by @Override protected void onProgressUpdate (Progress... values) of the AsyncTask.

Owen Zhao
  • 3,205
  • 1
  • 26
  • 43
  • I am streaming music in service.So how can i use AsyncTask and where can i use? – emreturka Aug 16 '13 at 11:25
  • Ok.Can I use Proggres dialog? – emreturka Aug 16 '13 at 20:47
  • You can't use Service and AsyncTask both. At least my suggestion is not to in this case. My suggestion is either you use a Service but try to use Notification or Toast to notify the user, or you use an AsyncTask in you Activity or Fragment and show a dialog or update any UI you want. – Owen Zhao Aug 16 '13 at 21:56
  • AsyncTask is useful because it has very useful methods that fit you situation here. You can use [doInBackground (Params... params)](http://developer.android.com/reference/android/os/AsyncTask.html#doInBackground(Params...)) to download you music steam in back ground and when the downloading has progress, you can call publishProgress (Progress... values), which will automatically invoke onProgressUpdate (Progress... values), the latter method is where you update your ProgressDialog's progress part to the UI. – Owen Zhao Aug 16 '13 at 22:04
  • I did not understand any thing from your suggestions.First of all If I use AsyncTask in my Activity that used this service,I can not control whether the stream is playing.How can I control whether the stream is playing or not in Activity that used this service. – emreturka Aug 17 '13 at 16:09
  • Have you ever seen my suggestion is with **if you insist to show a dialog and your music is played only after all is downloaded**? Also, AsyncTask is not the suggestion with **service**. It is a suggestion other than service. So each time I see what you say with **your service**, I am out of my temper! Also, you can always call [cancel (boolean mayInterruptIfRunning)](http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean)), so it is a solution to stop the cache if you don't want it any more. – Owen Zhao Aug 17 '13 at 23:17