声音不是播放声音播放android程序

时间:2016-10-30 20:27:10

标签: android audio voice playing

在这个程序中我们可以用按钮播放15个mp3文件。 我在" youwave for android"中模拟程序。它的作品很好。 当我在lenovo ideatab a3000平板电脑和xperia z1手机上进行测试时,当我们从一个按钮切换到另一个按钮时它没有播放声音,如果我们再次按下按钮它就不会播放。 我们应该按下其他按钮并返回没有播放声音播放声音的按钮。 我检查了那些按钮的编程。 还有另一个问题,有时当我退出程序时声音不会停止播放。 我最近增加了延迟,并且在此之前并没有引起轩然大波。

这是程序:

    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>

1 个答案:

答案 0 :(得分:0)

你可以做的几件事

  • 确保在使用release()
  • 完成使用时释放媒体播放器
  • 确保在使用stop()
  • 完成后再停止使用媒体播放器
  • player.prepare()
  • 之前致电player.start()

了解生命周期以更好地理解方法:

enter image description here