0

I start by mentioning that I have a ListView with a few items.

I select an Item and I start MediaPlayer streaming an audio from URL using..

String url = "https://url/"+ MyFile + ".mp3";
    mPlayer = new MediaPlayer();
    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mPlayer.setDataSource(url);
    } catch (IllegalArgumentException e) {
        Toast.makeText(this, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        mPlayer.prepare();
    } catch (IllegalStateException e) {
        Toast.makeText(this, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        Toast.makeText(this, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
    }
    mPlayer.start();
    mPlayer.setOnCompletionListener(new OnCompletionListener() {
        public void onCompletion(MediaPlayer mPlayer) {
            mPlayer.release();



        }
    });

I want though while MediaPlayer is playing to display an AlertDialog to turn off streaming is it possible?

Thank you.

Maria Georgali
  • 629
  • 1
  • 9
  • 22

1 Answers1

2

Create a alertDialog and write inside the callback to execute:

AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setMessage("Stop the player?");
builder1.setCancelable(true);

builder1.setPositiveButton(
    "Yes",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            mPlayer.release();
            dialog.cancel();
        }
    });

builder1.setNegativeButton(
    "No",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    }).show();

Or if you want you can create your own dialog layout (see https://stackoverflow.com/a/30388711/6726261)

Maurizio Ricci
  • 462
  • 1
  • 4
  • 11
  • Yes ok but the dialog will not close if streaming ends by itself it will stay there – Maria Georgali May 25 '17 at 14:23
  • The alert dialog will close itself (in the example above) only if the user press "no". If you want to dismiss the dialog via code you need to call 'cancel' on a alert dialog element – Maurizio Ricci May 25 '17 at 20:04