2016-10-30 202 views
0

在這個程序中,我們可以播放15個帶有按鈕的MP3文件。 我模擬了「youwave for android」中的程序,它的作品很好。 當我測試它在聯想ideatab a3000平板電腦和xperia z1手機somtimes當我們從一個按鈕切換到另一個它不播放聲音,它不會播放,如果我們再次按下按鈕。 ,我們應該按下其他按鈕並回到沒有播放聲音播放聲音的按鈕。 和我檢查了這些按鈕的編程。 還有另一個問題,有時當我退出程序聲音不會停止播放。 和我最近添加了延遲,並沒有在此之前不好。語音不播放在聲音播放安卓程序

,這是程序:

package fifa1.com.w3; 

import android.app.Activity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.os.SystemClock; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 




public class Fifa1Activity2 extends Activity { 


    Button  btn0; 
    Button  btn1; 
    Button  btn2; 
    Button  btn3; 
    Button  btn4; 
    Button  btn5; 
    Button  btn6; 
    Button  btn7; 
    Button  btn8; 
    Button  btn9; 
    Button  btn10; 
    Button  btn11; 
    Button  btn12; 
    Button  btn13; 
    Button  btn14; 
    Button  btn15; 
    Button  btn16; 
    Button  btn17; 


    ImageView imageView1; 


    MediaPlayer md = new MediaPlayer(); 




    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main2); 


     btn0 = (Button) findViewById(R.id.btn0); 
     btn1 = (Button) findViewById(R.id.btn1); 
     btn2 = (Button) findViewById(R.id.btn2); 
     btn3 = (Button) findViewById(R.id.btn3); 
     btn4 = (Button) findViewById(R.id.btn4); 
     btn5 = (Button) findViewById(R.id.btn5); 
     btn6 = (Button) findViewById(R.id.btn6); 
     btn7 = (Button) findViewById(R.id.btn7); 
     btn8 = (Button) findViewById(R.id.btn8); 
     btn9 = (Button) findViewById(R.id.btn9); 
     btn10 = (Button) findViewById(R.id.btn10); 
     btn11 = (Button) findViewById(R.id.btn11); 
     btn12 = (Button) findViewById(R.id.btn12); 
     btn13 = (Button) findViewById(R.id.btn13); 
     btn14 = (Button) findViewById(R.id.btn14); 
     btn15 = (Button) findViewById(R.id.btn15); 
     btn16 = (Button) findViewById(R.id.btn16); 
     btn17 = (Button) findViewById(R.id.btn17); 


     imageView1 = (ImageView) findViewById(R.id.imageView1); 


     btn0.setOnClickListener(new OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       md.stop(); 
       imageView1.setImageResource(R.drawable.fifa2000); 
       md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2000); 
       md.start(); 


      } 
     }); 


     btn1.setOnClickListener(new OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       md.stop(); 
       SystemClock.sleep(100); 
       imageView1.setImageResource(R.drawable.fifa2001); 
       md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2001); 
       md.start(); 
      } 
     }); 


     btn2.setOnClickListener(new OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       md.stop(); 
       SystemClock.sleep(100); 
       imageView1.setImageResource(R.drawable.fifa2002); 
       md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2002); 
       md.start(); 
      } 
     }); 


     btn3.setOnClickListener(new OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       md.stop(); 
       SystemClock.sleep(100); 
       imageView1.setImageResource(R.drawable.fifa2003); 
       md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2003); 
       md.start(); 
      } 
     }); 


     btn4.setOnClickListener(new OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       md.stop(); 
       SystemClock.sleep(100); 
       imageView1.setImageResource(R.drawable.fifa2004); 
       md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2004); 
       md.start(); 
      } 
     }); 


     btn5.setOnClickListener(new OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       md.stop(); 
       SystemClock.sleep(100); 
       imageView1.setImageResource(R.drawable.fifa2005); 
       md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2005); 
       md.start(); 
      } 
     }); 


     btn6.setOnClickListener(new OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       md.stop(); 
       SystemClock.sleep(100); 
       imageView1.setImageResource(R.drawable.fifa2006); 
       md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2006); 
       md.start(); 
      } 
     }); 


     btn7.setOnClickListener(new OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       md.stop(); 
       SystemClock.sleep(100); 
       imageView1.setImageResource(R.drawable.fifa2007); 
       md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2007); 
       md.start(); 
      } 
     }); 


     btn8.setOnClickListener(new OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       md.stop(); 
       SystemClock.sleep(100); 
       imageView1.setImageResource(R.drawable.fifa2008); 
       md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2008); 
       md.start(); 
      } 
     }); 


     btn9.setOnClickListener(new OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       md.stop(); 
       SystemClock.sleep(100); 
       imageView1.setImageResource(R.drawable.fifa2009); 
       md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2009); 
       md.start(); 
      } 
     }); 


     btn10.setOnClickListener(new OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       md.stop(); 
       SystemClock.sleep(100); 
       imageView1.setImageResource(R.drawable.fifa2010); 
       md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2010); 
       md.start(); 
      } 
     }); 


     btn11.setOnClickListener(new OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       md.stop(); 
       SystemClock.sleep(100); 
       imageView1.setImageResource(R.drawable.fifa2011); 
       md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2011); 
       md.start(); 
      } 
     }); 


     btn12.setOnClickListener(new OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       md.stop(); 
       SystemClock.sleep(100); 
       imageView1.setImageResource(R.drawable.fifa2012); 
       md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2012); 
       md.start(); 
      } 
     }); 


     btn13.setOnClickListener(new OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       md.stop(); 
       SystemClock.sleep(100); 
       imageView1.setImageResource(R.drawable.fifa2013); 
       md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2013); 
       md.start(); 
      } 
     }); 


     btn14.setOnClickListener(new OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       md.stop(); 
       SystemClock.sleep(100); 
       imageView1.setImageResource(R.drawable.fifa2014); 
       md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2014); 
       md.start(); 
      } 
     }); 


     btn15.setOnClickListener(new OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       md.stop(); 
       SystemClock.sleep(100); 
       imageView1.setImageResource(R.drawable.fifa2015); 
       md = MediaPlayer.create(Fifa1Activity2.this, R.raw.fifa2015); 
       md.start(); 
      } 
     }); 


     btn16.setOnClickListener(new OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       md.stop(); 
       SystemClock.sleep(100); 
       finish(); 
      } 
     }); 


     btn17.setOnClickListener(new OnClickListener() { 


      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       Intent intent = new Intent(Fifa1Activity2.this, about_us.class); 
       startActivity(intent); 
       finish(); 
      } 
     }); 


    } 
} 

XML文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingTop="20dp" > 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" > 


     <Button 
      android:id="@+id/btn0" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="FIFA2000"/> 


     <Button 
      android:id="@+id/btn1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="FIFA2001"/> 


     <Button 
      android:id="@+id/btn2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="FIFA2002"/> 


     <Button 
      android:id="@+id/btn3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="FIFA2003"/> 
    </LinearLayout> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" > 


     <Button 
      android:id="@+id/btn4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="FIFA2004" /> 


     <Button 
      android:id="@+id/btn5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="FIFA2005" /> 


     <Button 
      android:id="@+id/btn6" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="FIFA2006" /> 


     <Button 
      android:id="@+id/btn7" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="FIFA2007" /> 
    </LinearLayout> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" > 


     <Button 
      android:id="@+id/btn8" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="FIFA2008" /> 


     <Button 
      android:id="@+id/btn9" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="FIFA2009" /> 


     <Button 
      android:id="@+id/btn10" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="FIFA2010" /> 


     <Button 
      android:id="@+id/btn11" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="FIFA2011" /> 
    </LinearLayout> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" > 


     <Button 
      android:id="@+id/btn12" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="FIFA2012" /> 


     <Button 
      android:id="@+id/btn13" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="FIFA2013" /> 


     <Button 
      android:id="@+id/btn14" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="FIFA2014" /> 


     <Button 
      android:id="@+id/btn15" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="FIFA2015" /> 


    </LinearLayout> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" android:gravity="center"> 


     <Button 
      android:id="@+id/btn17" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="دربــــاره مــا" /> 


     <Button 
      android:id="@+id/btn16" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="خــــــــــــروج" /> 


    </LinearLayout> 


    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:layout_gravity="center" android:paddingTop="20dp"/> 


</LinearLayout> 

回答

0

有兩件事情可以做

  • 確保你釋放你的媒體播放器,只要您使用它完成使用release()
  • 使s URE您停止媒體播放器時,你就大功告成了使用,用前player.start()

stop()

  • 呼叫player.prepare()看看生命週期瞭解的方法更好,以及:

    enter image description here