在我的android应用程序中启动新声音之前,请停止prevous声音



此代码不起作用。。有人能帮我吗。当我运行此代码时,会出现一个错误,说强制关闭应用程序。我想用
开始新的声音每次点击。这里我使用图像作为我的按钮

package com.kle;
import java.io.IOException;
import com.kle.Kid.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
//import android.widget.Button;
import android.widget.ImageView;

public class Thirteen extends Activity implements OnClickListener{
int flag=1;
//Button b1;
MediaPlayer mp1,mp2,mp3,mp4,mp5,mp6,mp7,mp8,mp9,mp10;

ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.thirteen);
    iv=(ImageView) findViewById(R.id.imageView1);
    ImageView image1=(ImageView) findViewById(R.id.imageView1);
    ImageView image2=(ImageView) findViewById(R.id.imageView2);
    ImageView image3=(ImageView) findViewById(R.id.imageView3);
    ImageView image4=(ImageView) findViewById(R.id.imageView4);
    ImageView image5=(ImageView) findViewById(R.id.imageView5);
    ImageView image6=(ImageView) findViewById(R.id.imageView6);
    ImageView image7=(ImageView) findViewById(R.id.imageView7);
    ImageView image8=(ImageView) findViewById(R.id.imageView8);
    ImageView image9=(ImageView) findViewById(R.id.imageView9);
    ImageView image10=(ImageView) findViewById(R.id.imageView10);
    //b1= (Button) findViewById(R.id.button1);
    mp1= MediaPlayer.create(Thirteen.this, R.raw.one);
    mp2= MediaPlayer.create(Thirteen.this, R.raw.two);
    mp3= MediaPlayer.create(Thirteen.this, R.raw.three);
    mp4= MediaPlayer.create(Thirteen.this, R.raw.four);
    mp5= MediaPlayer.create(Thirteen.this, R.raw.five);
    mp6= MediaPlayer.create(Thirteen.this, R.raw.six);
    mp7= MediaPlayer.create(Thirteen.this, R.raw.seven);
    mp8= MediaPlayer.create(Thirteen.this, R.raw.eight);
    mp9= MediaPlayer.create(Thirteen.this, R.raw.nine);
    mp10= MediaPlayer.create(Thirteen.this, R.raw.ten);

    image1.setOnClickListener(this);
    image2.setOnClickListener(this);
    image3.setOnClickListener(this);
    image4.setOnClickListener(this);
    image5.setOnClickListener(this);
    image6.setOnClickListener(this);
    image7.setOnClickListener(this);
    image8.setOnClickListener(this);
    image9.setOnClickListener(this);
    image10.setOnClickListener(this);
    //b1.setOnClickListener(this);



}
public void onClick(View v) {
    // TODO Auto-generated method stub

    switch(v.getId()){
    case R.id.imageView1: {
        iv.setImageResource(R.drawable.kidone);
        mp2.stop();
        mp3.stop();
        mp4.stop();
        mp5.stop();
        mp6.stop();
        mp7.stop();
        mp8.stop();
        mp9.stop();
        mp10.stop();
        try {
            mp1.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mp1.seekTo(0);
        mp1.start();

        break;
    }
    case R.id.imageView2: {
        iv.setImageResource(R.drawable.kidtwo);
        mp1.stop();
        mp3.stop();
        mp4.stop();
        mp5.stop();
        mp6.stop();
        mp7.stop();
        mp8.stop();
        mp9.stop();
        mp10.stop();
        try {
            mp2.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mp2.seekTo(0);
        mp2.start();


        break;
    }
    case R.id.imageView3: {
        iv.setImageResource(R.drawable.kidthree);
        mp2.stop();
        mp1.stop();
        mp4.stop();
        mp5.stop();
        mp6.stop();
        mp7.stop();
        mp8.stop();
        mp9.stop();
        mp10.stop();
        try {
            mp3.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mp3.seekTo(0);
        mp3.start();

        break;
    }    

首先,您必须检查您的logcat是否有异常,这样您就更容易找到问题的根源。现在解决你的问题:你正在停止一个没有播放的媒体播放器。。。在停止媒体播放器之前添加检查:

private void stopPlaying(MediaPlayer mp)
{
     if (mp != null && mp.isPlaying())
     {
          mp.stop();
     }
}

然后呼叫:

stopPlaying(mp1);
stopPlaying(mp2);
stopPlaying(mp3);
...

(尽管我更喜欢使用数组进行这种编码,因为使用循环会使代码更可读,更容易维护)。

最新更新