-1

此代码不起作用..任何人都可以帮助我。当我运行此代码时,会出现错误提示强制关闭应用程序。我想在
每次点击时开始一个新的声音。在这里,我使用 图像作为我的按钮

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;
    }    
4

1 回答 1

0

首先,您必须检查您的 logcat 是否存在异常,以便您更容易找到问题的根源。现在解决您的问题:您正在停止未播放的媒体播放器...在停止媒体播放器之前添加检查:

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

并调用:

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

(虽然我更喜欢使用数组进行这种编码,因为它会使代码更具可读性和更易于维护 - 使用循环)。

于 2012-08-21T17:20:58.840 回答