OnClickListener停止播放

时间:2014-10-29 03:50:26

标签: android android-imageview android-mediaplayer onclicklistener

我试图在图像上播放点击声音(ImageView),听起来很完美,但经过一些点击后,没有任何声音,程序仍然运行没有问题。我尝试使用release()但不适合我。

公共类MainActivity扩展ActionBarActivity实现OnClickListener {

private MediaPlayer mp;

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.MnuOpc1:
            System.exit(0);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.main);
    View boton1 = findViewById(R.id.imageView1);
    boton1.setOnClickListener(this);
    View boton2 = findViewById(R.id.imageView4);
    boton2.setOnClickListener(this); 
    View boton3 = findViewById(R.id.imageView3);
    boton3.setOnClickListener(this); 
    View boton4 = findViewById(R.id.imageView2);
    boton4.setOnClickListener(this); 
    View boton5 = findViewById(R.id.imageView7);
    boton5.setOnClickListener(this); 
    View boton6 = findViewById(R.id.imageView9);
    boton6.setOnClickListener(this); 
    View boton7 = findViewById(R.id.imageView11);
    boton7.setOnClickListener(this); 
    View boton8 = findViewById(R.id.imageView13);
    boton8.setOnClickListener(this); 
    View boton9 = findViewById(R.id.imageView15);
    boton9.setOnClickListener(this); 
    View boton10 = findViewById(R.id.imageView17);
    boton10.setOnClickListener(this); 
    View boton11 = findViewById(R.id.imageView19);
    boton11.setOnClickListener(this); 
    View boton12 = findViewById(R.id.imageView21);
    boton12.setOnClickListener(this); 
}

public void onClick(View v) {



    if(v.getId()==findViewById(R.id.imageView1).getId())
    {


         mp = MediaPlayer.create(this,R.raw.sound1);  
         mp.start();


    }
    if(v.getId()==findViewById(R.id.imageView4).getId())
    {

        mp = MediaPlayer.create(this,R.raw.sound2);  
        mp.start();
    } 
    if(v.getId()==findViewById(R.id.imageView3).getId())
    {

        mp = MediaPlayer.create(this,R.raw.sound3);  
          mp.start();
    }
    if(v.getId()==findViewById(R.id.imageView2).getId())
    {

        mp = MediaPlayer.create(this,R.raw.sound4);  
          mp.start();
    }
    if(v.getId()==findViewById(R.id.imageView7).getId())
    {

        mp = MediaPlayer.create(this,R.raw.sound5);  
          mp.start();
    }
    if(v.getId()==findViewById(R.id.imageView9).getId())
    {

        mp = MediaPlayer.create(this,R.raw.sound6);  
          mp.start();
    }
    if(v.getId()==findViewById(R.id.imageView11).getId())
    {

        mp = MediaPlayer.create(this,R.raw.sound7);  
          mp.start();
    }
    if(v.getId()==findViewById(R.id.imageView13).getId())
    {

        mp = MediaPlayer.create(this,R.raw.sound8);  
          mp.start();
    }
    if(v.getId()==findViewById(R.id.imageView15).getId())
    {

        mp = MediaPlayer.create(this,R.raw.sound9);  
          mp.start();
    }
    if(v.getId()==findViewById(R.id.imageView17).getId())
    {

        mp = MediaPlayer.create(this,R.raw.sound10);  
          mp.start();
    }
    if(v.getId()==findViewById(R.id.imageView19).getId())
    {

        mp = MediaPlayer.create(this,R.raw.sound11);  
          mp.start();
    }
    if(v.getId()==findViewById(R.id.imageView21).getId())
    {

        mp = MediaPlayer.create(this,R.raw.sound12);  
          mp.start();
    }
}   

}

我认为我需要将release()放在某处或更改代码结构,但我现在不知道该怎么做。感谢。

0 个答案:

没有答案