I have a simple listview with the name of some songs. When you click on one of the childs, the song plays. However, I noticed that when you accidentally tap it again while its playing, it start to play again causing the sounds to overlap. How can I set it up so that when you click on one song and then click on another song, the first song stop and the second song begins. How can I modify my code to accomplish this. Any help would be appreciated.
public class TwelveGrammar extends ActionBarActivity {
private ListView lv;
private MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.twelve_grm);
lv = (ListView) findViewById(R.id.list);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> (
this, android.R.layout.simple_list_item_1, Exp_list);
lv.setAdapter(arrayAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int pos,
long id) {
if (mp.isPlaying()) {
mp.stop();
}
else {
if (pos==0) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1200);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==1) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1201);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==2) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1202);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==3) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1203);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==4) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1204);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==5) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1205);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==6) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1206);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==7) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1207);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==8) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1208);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==9) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1209);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==10) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1210);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==11) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1211);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==12) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1212);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==13) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1213);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==14) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1214);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==15) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1215);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
}
I think you should have one boolean like boolean isAnotherSongPlaying and if it's true the previous song should be stopped and the new one should start. And I believe that the best way to handle mediaplayer is a Service. I'm not yet too familiarized with that but I have implemented a Service for background music and its control depending on user's actions and it works quite well.
MyService.class
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
public class MyService extends Service implements OnCompletionListener {
MediaPlayer mediaPlayer;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
mediaPlayer = MediaPlayer.create(this, R.raw.music);// raw/s.mp3
mediaPlayer.setLooping(true);
mediaPlayer.setOnCompletionListener(this);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
}
public void onCompletion(MediaPlayer _mediaPlayer) {
stopSelf();
}
}
in my main activity I have one button which starts and stops the service depending whether the music is playing or not
Intent play;
play = new Intent(getApplicationContext(), MyService.class);
...
sound.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
if(isPlaying){
stopService(play);
isPlaying = false;
sound.setText("Yes");
} else {
startService(play);
isPlaying = true;
sound.setText("No");
}
}
});
Search it also on google you'll find some good tutorials. Hope I helped you.
Related
Being a novice coder, trying to code an app that plays a sound on buttonclick in a fragment; successfully did it but I want to add more small pieces of sounds to play one after another. How can I add those sounds(sound2, sound3, sound4, etc) after that 'R.raw.sound1'? Please guide..
buttonPlayAudioVar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp != null) {
if(mp.isPlaying()){
mp.stop();
mp.release();
}
}
mp = MediaPlayer.create(getActivity().getApplicationContext(), R.raw.sound1);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
mp = null;
}
});
}
});
//you can add all the songs in a array
ArrayList<String> songsArray = new ArrayList<>();
int position = 0;
buttonPlayAudioVar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer(songsArray, position);
}
});
private void mediaPlayer(ArrayList<String> songsArray) {
if (mp != null) {
if (mp.isPlaying()) {
mp.stop();
mp.release();
}
}
if (position > songsArray.size() - 1) {
return;
}
try {
mp.setDataSource(songsArray.get(position));
position++;
mp.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
mp = null;
mediaPlayer(songsArray, position);
}
});
}
i am making a phone dialer app that play the sound when you press on button the app is working great but after a while i get a random crash because on prepared listener i do not know what is happening and this is the error
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setOnPreparedListener(android.media.MediaPlayer$OnPreparedListener)' on a null object reference
at com.alper.pola.andoid.phonedailer.MainActivity$1.onTouch(MainActivity.java:258)
and this is how i cast the media player
if (mLastButton == one) {
MediaPlayer mediaPlayer ;
mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.one);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp1.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp1.release();
}
});
}
if (mLastButton == two) {
MediaPlayer mediaPlayer ;
mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.two);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp2.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp2.release();
}
});
}
if (mLastButton == three) {
MediaPlayer mediaPlayer ;
mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.three);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp3.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp3.release();
}
});
}
if (mLastButton == four) {
MediaPlayer mediaPlayer ;
mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.four);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp4.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp4.release();
}
});
}
the app is working fine and the same button when i click on it is working but after while the button stop working and i get this crash with every button it start with working fine but after i type a few numbers the app crash
You are using a very inefficient way while dealing with MediaPlayer. Still I'll help you a workaround with this code. Use mediaPlayer.prepare() to initiate the call. Checkout this code:
if (mLastButton == one) {
mp1 = MediaPlayer.create(MainActivity.this,R.raw.one);
mp1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp1.start();
});
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp1.release();
}
});
mp1.prepare();
}
Do similarly for others. There are much better way to do this but as with your code, this is shortest.
this is the method am using..and will call whenever required...in my case i want to control that audio by one button whether it may toggle or button...
click to start and stop in one button
private void playSound() {
if (isFlashLightOn) {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off);
} else {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_on);
}
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});mp.start(); playing=true;
}
An edited version of your code
private void playSound() {
if (isFlashLightOn) {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off);
} else {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_on);
}
mp.prepareAsync(); //prepares the MediaPlayer
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
//MediaPlayer instance prepared
mp.start(); //play the content
playing=true;//update the flag
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release(); //playback finished
playing=false; //update the flag
}
});
}
more info on MediaPlayer here
Onclick pause:
public void pause(View view) {
Toast.makeText(getApplicationContext(),
"Pausando..", Toast.LENGTH_SHORT)
.show();
if (mp != null && mp.isPlaying()) {
mp.pause();
}
}//pause
Onclik stop-
public void stop(View view) {
Toast.makeText(getApplicationContext(),
"Stop", Toast.LENGTH_SHORT)
.show();
if (mp != null) {
mp.stop();
}//if
}//stop
Anyone know how to check to see if MediaPlayer is being played. In a test, before I added the if statement, the audio played fine, but now I get a force closed. What I want is that when someone clicks another button, the current audio stops and the new audio plays. I thought that would be easy, but it seems not. I thought I did the code correctly. Is my code correct? What am I missing? Any help would be appreciated.
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int pos,
long id) {
if (mp.isPlaying()) {
mp.stop();
mp.release();
}
else {
if (pos==0) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2200);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==1) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2201);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==2) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2202);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==3) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2203);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==4) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2204);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==5) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2205);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==6) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2206);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==7) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2207);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==8) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2208);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==9) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2209);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==10) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2210);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==11) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2211);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==12) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2212);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==13) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2213);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==14) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2214);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==15) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2215);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==16) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2216);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
}
}
});
Check for null before calling isPlaying() and set it to null after all release calls, so the release doesn't get called twice.
if (mp != null && mp.isPlaying()) {
mp.stop();
mp.release();
mp = null;
}
Am using mediaplayer to play tick sound in timer.But it continues to play after timer is over,Even after i called stop for media player. I played sound as seen below in ontick() in timer. In onfinish() called mp.Stop(),But it not working.
mp = MediaPlayer.create(getBaseContext(), sound);
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
});
mp.setLooping(false);
// mp.setVolume(0.1, 0.1);
mp.start();
}
coding for my timer .
final class MyCounter extends CountDownTimer {
public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
timer.cancel();
MediaPlayer.OnCompletionListener onCompletion = new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
if (mp.isPlaying()) {
mp.release();
mp.stop();
mp.reset();
}
}
};
mp.setOnCompletionListener(onCompletion);
i++;
submitAnswer();
final GameEngine ge = GameEngine.getInstance();
timeer.setText("Timer Completed.");
if (ge.getCurrentUnAllocatedAmount() > 0 && i <= 8) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(GameActivity.this,
ScoreActivity.class);
intent.putExtra("level", i);
ge.setCurrentLevel(i);
startActivity(intent);
}
}, 2200);
} else if (ge.getCurrentUnAllocatedAmount() <= 0 || i > 8) {
Intent intent = new Intent(GameActivity.this,
ScoreActivity.class);
intent.putExtra("level", i);
startActivity(intent);
}
}
public void onTick(long millisUntilFinished) {
remTime = (millisUntilFinished / 1000);
timeer.setText("Time:" + (millisUntilFinished / 1000) + "");
mp = MediaPlayer.create(getBaseContext(), R.raw.beep7);
if (remTime > 0) {
mp.start();
mp.release();
}
if (remTime < 10) {
final Handler handler = new Handler();
handler.post(new Runnable() {
public void run() {
if (timeer.getVisibility() == View.VISIBLE) {
timeer.setVisibility(View.INVISIBLE);
} else {
timeer.setVisibility(View.VISIBLE);
}
timeer.setTextColor(Color.RED);
}
});
}
}
}
Use this code:
if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
}
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.stop();
});
try this:
mp.setOnCompletionListener(onCompletion);
private MediaPlayer.OnCompletionListener onCompletion = new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if(mp.isPlaying())
{
mp.stop();
mp.reset();
}
}
};
I think you are not calling the release() method on the onCompletionListener() itself.
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
});
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if (mp.isPlaying()) {
mp.stop();
}
mp.release();
mp = null;
}
});
Check condition when timer is stop.
if(mPlayer.isPlaying())
{
mPlayer.stop();
}