2

如何將播放和暫停圖像按鈕看作單個圖像按鈕。我在此附上我的代碼。這些是使用的圖像。我將遊戲重命名爲開始。媒體播放器在Android中播放暫停

import android.app.Activity; 
import android.media.MediaPlayer; 
import android.net.Uri; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ProgressBar; 


public class AudioView extends Activity{ 

MediaPlayer mp; 

    Button p,pu,s,b,f,h,v,c,bu; 

    ProgressBar myProgressBar; 

     Uri uri=Uri.parse("http://player.trackitdown.net/preview/289245/preview_c-90-feat-red-monkey-yo-dj-original-mix-dos-or-die-traxx.mp3"); 

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

     Log.v("Start Of OnCreate","++"); 
     mp=MediaPlayer.create(this,uri); 


     final Button play = (Button) findViewById(R.id.play); 
     play.setBackgroundResource(R.drawable.start); 
     final Button pause = (Button) findViewById(R.id.pause); 
     pause.setBackgroundResource(R.drawable.pause); 
     final Button back = (Button) findViewById(R.id.back); 
     back.setBackgroundResource(R.drawable.backward); 
     final Button fwd = (Button) findViewById(R.id.fwd); 
     fwd.setBackgroundResource(R.drawable.forward); 

     p=(Button)findViewById(R.id.play); 
     pu=(Button)findViewById(R.id.pause); 
     pu.setVisibility(View.INVISIBLE); 
     //s=(Button)findViewById(R.id.stop); 
     b=(Button)findViewById(R.id.back); 
     f=(Button)findViewById(R.id.fwd); 

     Log.v("Button Objects","getting man"); 

     play.setOnClickListener(mListener); 
     pause.setOnClickListener(mListener); 
     back.setOnClickListener(mListener); 
     fwd.setOnClickListener(mListener); 


     myProgressBar=(ProgressBar)findViewById(R.id.progressbar_Horizontal); 

     myProgressBar.setProgress(0); 
     myProgressBar.setMax(mp.getDuration()); 


     Log.v("End Of OnCreate","--"); 




    } 
    private Runnable myThread = new Runnable(){ 

      public void run() { 
      // TODO Auto-generated method stub 
      while (mp.getCurrentPosition()<mp.getDuration()){ 
      try{ 
      //myHandle.sendMessage(myHandle.obtainMessage()); 
        myProgressBar.setProgress(mp.getCurrentPosition()); 
       //Thread.sleep(1000); 
      } 
      catch(Throwable t){ 
      } 
      } 
      } 

      }; 


    View.OnClickListener mListener = new View.OnClickListener(){ 

     public void onClick(View v) { 

      switch(v.getId()){ 

      case R.id.play: 

       try { 
        mp.prepare(); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 


       mp.start(); 
       p.setVisibility(View.INVISIBLE); 
       pu.setVisibility(View.VISIBLE); 
       pu.setClickable(true); 
       p.setClickable(false); 
        //seekBar.setProgress(mp.getCurrentPosition()); 
        new Thread(myThread).start(); 

       break; 
      case R.id.pause: 
       mp.pause(); 
       pu.setClickable(false); 
       p.setVisibility(View.VISIBLE); 
       pu.setVisibility(View.INVISIBLE); 
       p.setClickable(true); 
       break; 

      case R.id.back: 
       int dur = mp.getCurrentPosition(); 
       int pos = (dur>10000 ? dur-5000:0); 
       mp.seekTo(pos); 
       break; 
      case R.id.fwd: 
       int curpos = mp.getCurrentPosition(); 
       int dur2 = mp.getDuration(); 

       int pos2 = (curpos+5000>dur2 ? dur2: curpos+5000); 
       mp.seekTo(pos2); 
       break; 


      } 

     } 


    }; 

    } 

和XML是

<?xml version="1.0" encoding="UTF-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 

<LinearLayout android:orientation="vertical" 

    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom" 
    android:gravity="center" 
    > 

    <ImageView android:layout_width="176dp" 
       android:layout_height="208dp" 
       android:src="@drawable/audio_icon"/> 

     <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center"> 

     <Button 
     android:id="@+id/play" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

     <Button 
      android:id="@+id/pause" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="4dp" /> 

     <Button 
     android:id="@+id/back" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="4dp" 
     /> 
     <Button 
     android:id="@+id/fwd" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="4dp" 
     /> 

    </LinearLayout> 

    <ProgressBar 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:id="@+id/progressbar_Horizontal" 
    /> 

</LinearLayout> 
</LinearLayout> 
+0

很簡單,您使用相同的按鈕,只需在運行時替換其資源以在播放和暫停之間切換。 – Teovald

回答

2

更改按鈕,第一個說玩。將播放和暫停代碼合併爲一個。設置並檢查一個標誌,看看是否按下播放或暫停,並相應地更改文字。

所以你只需要一個按鍵,兩個布爾字段:現在

boolean play=true, pause=false; 

    final Button playPause = (Button) findViewById(R.id.play); 
      playPause.setBackgroundResource(R.drawable.start); 

    playPause.setOnClickListener(mListener); 

,在你的聽衆的代碼,這樣做:

case R.id.play: 

      if(play) 
      { 
       play=false; 
       pause=true; 

       //change image for button 
       playPause.setBackgroundResource(R.drawable.start); 

       try { 
        mp.prepare(); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 


       mp.start(); 
       p.setVisibility(View.INVISIBLE); 
       pu.setVisibility(View.VISIBLE); 
       pu.setClickable(true); 
       p.setClickable(false); 
        //seekBar.setProgress(mp.getCurrentPosition()); 
        new Thread(myThread).start(); 

       } 
      if(pause) 
      { 
       play=true; 
       pause=false; 

       //change image for button 
       playPause.setBackgroundResource(R.drawable.pause); 

       mp.pause(); 
       pu.setClickable(false); 
       p.setVisibility(View.VISIBLE); 
       pu.setVisibility(View.INVISIBLE); 
       p.setClickable(true); 
      } 
      break; 

相應變化在您的按鈕文本。

2

您可以使用下面的代碼:

if(!mp.isPlaying() || isCompleted) { 
    isCompleted = false; 
    mp.start(); 
} else { 
    mp.pause(); 
} 

添加變體:

private boolean isCompleted = true; 

添加監聽器:

mp.setOnCompletionListener(new OnCompletionListener(){ 

     @Override 
     public void onCompletion(MediaPlayer mp) { 
      // TODO Auto-generated method stub 
      isCompleted = true; 
     }} 
    );