-1

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;
    }    
swati
  • 23
  • 1
  • 5

1 Answers1

0

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).

IncrediApp
  • 10,303
  • 2
  • 33
  • 24