2017-05-25 33 views
0

我首先提到我有一个带有几个项目的ListView。如何在mediaplayer播放时显示AlertDialog并使用它停止流式传输

我选择一个项目,我开始MediaPlayer的流媒体使用URL音频..

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



     } 
    }); 

我想,虽然同时MediaPlayer正在播放到显示AlertDialog关闭流这可能吗?

谢谢。

+0

那么问题是什么?无法看到你用Dialog –

+0

尝试过任何东西是的,因为我不知道如何去做:) –

回答

1

创建alertDialog和回调里面写来执行:

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

或者,如果你愿意,你可以创建自己的对话框布局(见https://stackoverflow.com/a/30388711/6726261

+0

好的,但是如果流式传输本身结束,对话框将不会关闭 –

+0

警报对话框将自动关闭(在只有当用户按“否”时才可以)。如果您想通过代码解除对话框,则需要在警报对话框元素上调用“取消” –

相关问题