Note that there are some explanatory texts on larger screens.

plurals
  1. POAudio [MediaPlayer] is not working for my Piano Apps
    text
    copied!<p>Now, I try to built a piano application. The concept is to play a sound I must touch a button. Now, I have 7 button and for each button play a sound file. Here is the code.</p> <pre><code>package com.andikurnia.piano; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; public class IsaronActivity extends Activity { /** Called when the activity is first created. */ ImageView button1; ImageView button2; ImageView button3; ImageView button4; ImageView button5; ImageView button6; ImageView button7; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final MediaPlayer sound_6 = MediaPlayer.create(this, R.raw.saron_6); final MediaPlayer sound1 = MediaPlayer.create(this, R.raw.saron1); final MediaPlayer sound2 = MediaPlayer.create(this, R.raw.saron2); final MediaPlayer sound3 = MediaPlayer.create(this, R.raw.saron3); final MediaPlayer sound5 = MediaPlayer.create(this, R.raw.saron5); final MediaPlayer sound6 = MediaPlayer.create(this, R.raw.saron6); final MediaPlayer sound11 = MediaPlayer.create(this, R.raw.saron11); button1 = (ImageView) findViewById(R.id.one); button1.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent touchEvent) { // TODO Auto-generated method stub if(sound_6.isPlaying()){ sound_6.stop(); sound_6.seekTo(0); }else{ sound6.start(); } return true; } }); button2 = (ImageView) findViewById(R.id.two); button2.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View two, MotionEvent touchEvent) { // TODO Auto-generated method stub if(sound1.isPlaying()){ sound1.stop(); sound1.seekTo(0); }else{ sound1.start(); } return true; } }); button3 = (ImageView) findViewById(R.id.three); button3.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View three, MotionEvent touchEvent) { // TODO Auto-generated method stub if(sound2.isPlaying()){ sound2.stop(); sound2.seekTo(0); }else{ sound2.start(); } return true; } }); button4 = (ImageView) findViewById(R.id.four); button4.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View four, MotionEvent touchEvent) { // TODO Auto-generated method stub if(sound3.isPlaying()){ sound3.stop(); sound3.seekTo(0); }else{ sound3.start(); } return true; } }); button5 = (ImageView) findViewById(R.id.five); button5.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View five, MotionEvent touchEvent) { // TODO Auto-generated method stub if(sound5.isPlaying()){ sound5.stop(); sound5.seekTo(0); }else{ sound5.start(); } return true; } }); button6 = (ImageView) findViewById(R.id.six); button6.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View six, MotionEvent touchEvent) { // TODO Auto-generated method stub if(sound6.isPlaying()){ sound6.stop(); sound6.seekTo(0); }else{ sound6.start(); } return true; } }); button7 = (ImageView) findViewById(R.id.seven); button7.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View seven, MotionEvent touchEvent) { // TODO Auto-generated method stub if(sound11.isPlaying()){ sound11.stop(); sound11.seekTo(0); }else{ sound11.start(); } return true; } }); } } </code></pre> <p>The problem is when I touch a button (for example button1) the audio file called and play properly. But, when I move my finger to touch other buttons, The sound on that button just play half and when I back to button before, it can't play sound again. Anyone can help analyze my code..? :(</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload