2017-05-25 73 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

警報對話框將自動關閉(在只有當用戶按「否」時纔可以)。如果您想通過代碼解除對話框,則需要在警報對話框元素上調用「取消」 –

相關問題