2013-10-06 32 views
0

我有一個包含三個不同按鈕的頁面PLAY |暫停|停。 PLAY |停止工作正常,但我無法在特定情況下暫停。我想暫停播放,按暫停按鈕。然後再次按下Play按鈕從保存的實例恢復它。Android - 需要暫停播放特定實例的媒體文件

下面是從Sound.java代碼文件

public class Sound extends Activity { 

    MediaPlayer mp = null; 
    String play = "Play!";  
    String stop = "Stop!"; 
    String pause = "Pause";  
    int length; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sound); 

     final ImageButton play = (ImageButton) findViewById(R.id.idplay); 
     play.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       managerOfSound("play"); 
       Toast toast_play = Toast.makeText(getBaseContext(), 
         "Playing First", Toast.LENGTH_SHORT); 
       toast_play.show(); 

      } // END onClick() 
     }); 

final ImageButton pause = (ImageButton) findViewById(R.id.idpause); 
     pause.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       managerOfSound("pause"); 
       Toast toast_pause = Toast.makeText(getBaseContext(), 
"Pausing Morning Bhajan-Aarati", Toast.LENGTH_SHORT); 
       toast_pause.show(); 

      } // END onClick() 
     }); 


     final ImageButton stop = (ImageButton) findViewById(R.id.idstop); 
     stop.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (mp != null) { 
        mp.stop(); 
        mp.reset(); 
        Toast toast_stop = Toast.makeText(getBaseContext(), 
          "Stopping First", 
          Toast.LENGTH_SHORT); 
        toast_stop.show(); 
       } 
      } // END onClick() 
     }); 


    /** Manager of Sounds **/ 
    protected void managerOfSound(String theText) { 
if (mp != null){ 
     mp.reset(); 
     mp.release(); 
     } 
     if (theText == "play") 
      mp = MediaPlayer.create(this, R.raw.goodbye); 
     else if (theText == "pause" && mp.isPlaying()) 
mp.pause(); 
      length = mp.getCurrentPosition(); 
      mp.seekTo(length); 
      mp.start(); 
    } 

這裏是我的sound.xml文件,

<RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <ImageButton 
      android:id="@+id/idplay" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="65dp" 
      android:layout_marginTop="20dp" 
      android:background="@drawable/image_play" /> 
     <!-- android:text="@string/idplay" /> --> 

     <ImageButton 
      android:id="@+id/idpause" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dp" 
      android:layout_toLeftOf="@+id/idstop" 
      android:background="@drawable/image_pause" /> 

     <ImageButton 
      android:id="@+id/idstop" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="20dp" 
      android:layout_marginTop="20dp" 
      android:layout_toRightOf="@+id/idplay" 
      android:background="@drawable/image_stop" /> 

    </RelativeLayout> 

您的幫助表示讚賞.. !!

回答

0

我覺得你的問題是這一行:

if (theText == "play") 
    mp = MediaPlayer.create(this, R.raw.goodbye); 
else if (theText == "pause" && mp.isPlaying()) 
    mp.pause(); 
    length = mp.getCurrentPosition(); 
    mp.seekTo(length); 
    mp.start();´ 

您通過點擊暫停按鈕設置的長度,但你會如果子句再次直接啓動它這一點。

您應該檢查是否被點擊播放按鈕,如果媒體被打befor,像

if(length > 0) 

,然後開始在指定的位置的球員。

編輯: 另外,您不應該使用運算符==來比較字符串。你應該更好地利用string1.equals(string2)

希望它可以幫助

最良好的祝願

+0

感謝您的答覆。我已經更新在代碼中string1.equals(字符串2)。但暫停活動,請你能澄清我應該在哪裏進行修改以及在哪裏檢查(長度> 0).. –

+0

嗨大家好, 對此有任何更新.. –

+0

我不知道你是否已經完成檢查if之前在MediaPlayer課程中播放過medai。但是,如果不是的話,你應該開發一種機制,在暫停播放指定的位置上播放媒體文件。 – ZeusNet

相關問題