0

I have an Intent which starts a MusicService which connects to the internet and plays a stream:

final Intent i = new Intent(MusicService.ACTION_URL);
Uri uri = Uri.parse("http://31.3.242.244:8058");

i.setData(uri);
startService(i);

progressDialog = ProgressDialog.show(fmActivity.this, "", "connecting...");
progressDialog.setCancelable(true);

When the connection is established I have a Handler close the dialog box:

static Handler closeHandler = new Handler() {
    public void handleMessage(Message msg) {
        if (progressDialog !=null) {
            progressDialog.dismiss();
        }
    }
};    
fmActivity.closeHandler.handleMessage(null); //from the MusicService

This all works great unless the connection hangs or the server is slow to connect. I need to be able to cancel the Intent from trying to start the MusicService with the back button. Checking for internet connection won't help because there is always a connection...the user needs to be able to cancel the call to connect because if the connection to the server fails for whatever reason it prevents the user from being able to load a different stream. I have searched all over and can't find out how to do this. Thanks.

makvalti
  • 87
  • 1
  • 9
midiwriter
  • 426
  • 5
  • 12
  • If there is a bad connection , why doesn't your app just stop the music service for that particular stream? Doesn't seem right for it to hang there and have the user decide if its a bad connection or not. Once an intent is started, its started. You can stop the service however. – Finding Nemo 2 is happening. Sep 05 '12 at 18:19
  • you can handle socket exception after the request /connection is made.. which ease the cancel operation – Chet Sep 05 '12 at 18:20
  • "If there is a bad connection , why doesn't your app just stop the music service for that particular stream?" Yes that would be the same as having the user cancel it, but this is where I don't know how to proceed... – midiwriter Sep 05 '12 at 18:37
  • After you start the music service, have the service check whether it is buffering/cant recieve enough data. Properly testing that could be a bit tricky, but then have your service call `stopSelf()` to cancel that particular stream. – Finding Nemo 2 is happening. Sep 05 '12 at 18:47
  • @gjj thank you for the answer...although I only started learning java 1 month ago and I'm not sure if I can do the check buffer thing...I will try though and thanks for helping. – midiwriter Sep 05 '12 at 19:05
  • Ya like you said just checking to see if there is an active internet connection isnt really useful, but with MediaPlayer: http://developer.android.com/guide/topics/media/mediaplayer.html There is a constant, MEDIA_INFO_BUFFERING_START that is defined in the MediaPlayer class that can signal if the audio source if having to buffer. Take a look at it and start a new question if you get stuck. – Finding Nemo 2 is happening. Sep 05 '12 at 19:11

1 Answers1

0

You can stop the music service before dismissing the ProgressDialog inside your cancel's OnClickListener.

For instance:

    progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int which) {
                stopService(i); // Stop the music service here
                dialog.dismiss();
            }
});
Thiago M Rocha
  • 1,416
  • 1
  • 20
  • 26
  • Thanks but this doesn't stop the MusicService it just closes the dialog. – midiwriter Sep 05 '12 at 18:38
  • I'm sorry. I forgot to add the most important line! I've edited my answer. Check the new version and see if helps you. – Thiago M Rocha Sep 06 '12 at 12:17
  • Wow you are a genius! I can't believe how simple that was and how much time I have spent on this. There are posts all over about not being able to stop the MediaPlayer while it's in the prepare state, such as this one: [link](http://stackoverflow.com/questions/4971914/safe-to-reset-a-mediaplayer-in-preparing-state) and this: [link](http://stackoverflow.com/questions/4180670/android-mediaplayer-is-preparing-too-long) Thanks for your help. – midiwriter Sep 06 '12 at 14:21
  • Thanks man! Feel free to ask anything about the Android's `MusicService` – Thiago M Rocha Sep 06 '12 at 18:15