2014-12-30 99 views
0

有了這段代碼,我可以在後臺運行歌曲,點擊'yes'按鈕並用'no'停止它,但它只能運行一次。如果我再次單擊「是」按鈕(第二次),則應用程序停止。所以需要幫助,我希望我的活動能夠在每次單擊時使用這些警報對話框的按鈕來運行和停止音樂。Android媒體播放器第二次不工作

public class Music extends Activity { 

final Context context = this; 
MediaPlayer song; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.menu); 

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
      context); 

    alertDialogBuilder 
      .setMessage("Turn ON the music?") 
      .setCancelable(false) 
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 

        song.setAudioStreamType(AudioManager.STREAM_MUSIC); 
        try { 
         song = MediaPlayer.create(Music.this, R.raw.fing); 
        } catch (IllegalArgumentException e) { 
         Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
        } catch (SecurityException e) { 
         Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
        } catch (IllegalStateException e) { 
         Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
        } 
        try { 
         song.prepare(); 
        } catch (IllegalStateException e) { 
         Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
        } catch (IOException e) { 
         Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
        } 
        song.start(); 
       } 


       // dialog.cancel(); 
      } 


       ) 


       .setNegativeButton("No",new DialogInterface.OnClickListener() { 
        public void onClick (DialogInterface dialog,int id){ 
         // if this button is clicked, just close 
         // the dialog box and do nothing 
         if(song!=null && song.isPlaying()){ 
          song.stop(); 
          dialog.cancel(); 

         } 
        }}); 

       // create alert dialog 
       AlertDialog alertDialog = alertDialogBuilder.create(); 

       // show it 
       alertDialog.show();}} 
+0

試着在'onCreate'中設置音頻'MediaPlayer歌曲',在'PositiveButton'中設置'song.start();' – waki

+0

@Waki我首先試過,它只能工作一次;如果我點擊'yes'_first time_歌曲播放,如果我點擊'no'_first time_它會停止,當我再次點擊'是'時 - 應用程序停止。所以我使用了try/catch,但它沒有起作用。 – Journey

+0

你不理解我,'setAudioStreamType','MediaPlayer.create'和'prepare'也放在'onCreate'中,在'PositiveButton'' onClick'只是'song.start();' – waki

回答

1

您所有的媒體播放器的初始化(如創建,setAudioStreamType,準備),例如你可以在onCreate方法不是的onClick對話框。在onClick中只需調用stop和play方法。

喜歡這個

public class Music extends Activity { 

    final Context context = this; 
    MediaPlayer song; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.menu); 

    song.setAudioStreamType(AudioManager.STREAM_MUSIC); 
         try { 
          song = MediaPlayer.create(Music.this, R.raw.fing); 
         } catch (IllegalArgumentException e) { 
          Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
         } catch (SecurityException e) { 
          Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
         } catch (IllegalStateException e) { 
          Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
         } 
         try { 
          song.prepare(); 
         } catch (IllegalStateException e) { 
          Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
         } catch (IOException e) { 
          Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
         } 

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
      context); 

    alertDialogBuilder 
      .setMessage("Turn ON the music?") 
      .setCancelable(false) 
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        song.start(); 
       } 


       // dialog.cancel(); 
      } 


       ) 


       .setNegativeButton("No",new DialogInterface.OnClickListener() { 
        public void onClick (DialogInterface dialog,int id){ 
         // if this button is clicked, just close 
         // the dialog box and do nothing 
         if(song!=null && song.isPlaying()){ 
          song.stop(); 
          dialog.cancel(); 

         } 
        }}); 

       // create alert dialog 
       AlertDialog alertDialog = alertDialogBuilder.create(); 

       // show it 
       alertDialog.show(); 
    } 
} 
+0

不介意朋友,但沒有一個工作。我想我需要使用線程來解決這個問題。 – Journey

1

使用此

.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
song.reset(); // this makes sure that the song object was not assigned before (it recreates the MediaPlayer) 
        song.setAudioStreamType(AudioManager.STREAM_MUSIC); 
        try { 
         song = MediaPlayer.create(Music.this, R.raw.fing); 
        } catch (IllegalArgumentException e) { 
         Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
        } catch (SecurityException e) { 
         Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
        } catch (IllegalStateException e) { 
         Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
        } 
        try { 
         song.prepare(); 
        } catch (IllegalStateException e) { 
         Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
        } catch (IOException e) { 
         Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
        } 
        song.start(); 
       } 


      // dialog.cancel(); 
     } 


      ) 

.setNegativeButton("No",new DialogInterface.OnClickListener() { 
       public void onClick (DialogInterface dialog,int id){ 
        // if this button is clicked, just close 
        // the dialog box and do nothing 
        if(song!=null && song.isPlaying()){ 
         song.stop(); 
song.release(); // This releases the song object so you can initiate another MediaPlayer object if you want. 
         dialog.cancel(); 

        } 
       }}); 

這將重置MediaPlayer對象,以確保你有一個新鮮的,如果音樂停止播放它釋放的MediaPlayer(這也釋放RAM存儲器)

0

儘量撥打電話

if (song !=null){ 
song.release()} 

song = MediaPlayer.create(Music.this, R.raw.fing); 

之前,你能告訴你的日誌?

0

我解決了調用setOnPreparedListener中的song.start()的問題,任何調用在此方法外部啓動都會產生錯誤。