Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

audio - Stop the prevoius sound before starting a new sound in my android app

This code is not working.. Can anyone help me out. when i had run this code is gave an error saying force close application.I want to start a new sound with
each click. here i am using images as my buttons.

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;
    }    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all you must check your logcat for the exceptions so that it would be easier for you to find the source of the problem. Now for your problem: you're stopping a media player which isn't playing... Add a check before stopping the media player:

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

And call:

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

(Although I would prefer using an array for this kind of coding as it would make the code far more readable and easier to maintain - using loops).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...