Reputation: 2053
I am having a problem where once the user clicks on a sound to play from my ListView, then while that sound is playing they click on another sound, the 2 sounds they clicked play at the same time.
I would like to have the sound that was currently playing, finish, then start the new sound that they most recently clicked.
If someone could help me, that would be much appreciated!
CODE:
ListView BoardList = (ListView) findViewById(R.id.BoardList);
String List[] = {
"Audio1", "Audio2", "Audio3", "Audio4", "Audio5"
, "Audio6", "Audio7", "Audio8", "Audio9"
, "Audio10", "Audio11", "Audio12" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.listcustomize, R.id.textItem, List);
BoardList.setAdapter(adapter);
BoardList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
MediaPlayer mPlayer = null;
if (position == 0) {
mPlayer = MediaPlayer.create(HodgeMain.this,
R.raw.Audio1);
mPlayer.start();
}
if (position == 1) {
mPlayer = MediaPlayer.create(HodgeMain.this,
R.raw.Audio2);
mPlayer.start();
}
if (position == 2) {
mPlayer = MediaPlayer.create(HodgeMain.this, R.raw.Audio3);
mPlayer.start();
}
if (position == 3) {
mPlayer = MediaPlayer.create(HodgeMain.this,
R.raw.Audio4);
mPlayer.start();
}
if (position == 4) {
mPlayer = MediaPlayer.create(HodgeMain.this,
R.raw.Audio5);
mPlayer.start();
}
if (position == 5) {
mPlayer = MediaPlayer.create(HodgeMain.this,
R.raw.Audio6);
mPlayer.start();
}
if (position == 6) {
mPlayer = MediaPlayer.create(HodgeMain.this,
R.raw.Audio7);
mPlayer.start();
}
if (position == 7) {
mPlayer = MediaPlayer
.create(HodgeMain.this, R.raw.Audio8);
mPlayer.start();
}
if (position == 8) {
mPlayer = MediaPlayer.create(HodgeMain.this,
R.raw.Audio9);
mPlayer.start();
}
if (position == 9) {
mPlayer = MediaPlayer.create(HodgeMain.this,
R.raw.Audio10);
mPlayer.start();
}
if (position == 10) {
mPlayer = MediaPlayer.create(HodgeMain.this,
R.raw.Audio11);
mPlayer.start();
}
if (position == 11) {
mPlayer = MediaPlayer.create(HodgeMain.this,
R.raw.Audio12);
mPlayer.start();
}
Upvotes: 6
Views: 23660
Reputation: 14199
Media player playing multiple files at the same time
Try
Declare MediaPlayer mPlayer;
common for all
Like
BoardList.setAdapter(adapter);
MediaPlayer mPlayer;
then use mPlayer.release();
if (position == 0) {
if(mPlayer!=null)
{
mPlayer.release();
mPlayer=null;
}
mPlayer = MediaPlayer.create(HodgeMain.this,
R.raw.Audio1);
mPlayer.start();
}
.
.
.
.
if(position==N)
{
if(mPlayer!=null)
{
mPlayer.release();
mPlayer=null;
}
mPlayer = MediaPlayer.create(HodgeMain.this,
R.raw.AudioN);
mPlayer.start();
}
About release():
Releases resources
associated with this MediaPlayer object
.
It is considered good practice to call this method when you're done using the MediaPlayer
.
In particular, whenever an Activity of an application is paused (its onPause() method is called), or stopped (its onStop() method is called)
, this method should be invoked to release the MediaPlayer object
, unless the application has a special need to keep the object around.
PS. i have tried with release()
which is working fine !
Example: which is working for me
try {
if (position == 1) {
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
mPlayer = MediaPlayer.create(MainActivity.this, R.raw.all);
mPlayer.start();
}
if (position == 2) {
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
mPlayer = MediaPlayer.create(MainActivity.this, R.raw.all2);
mPlayer.start();
}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Upvotes: 6
Reputation: 6834
The problem is the static method MediaPlayer.create(); is actually returning a new instance of MediaPlayer for you.
The reason multiple are playing is because you're not stopping the previously playing audio file (as others suggested).
Call this method IMMEDIATELY BEFORE calling MediaPlayer.create():
private void killSounds(){
try{
mPlayer.stop();
mPlayer.release();
}
catch(Exception e) {
//Eat it and do nothing because it's just going to be an NPE when mPlayer is null, which doesn't matter because we're handling it
}
}
After you've stopped the previous MediaPlayer, you should be able to create the new one without the sound overlap.
Upvotes: 0
Reputation: 11608
as already mentioned, you need to instantiate the MediaPlayer
class using new
to access its object anywhere in your class and not statically like you are doing it now. I'm pretty sure consulting this document will help you understand more about the MediaPlayer
: http://developer.android.com/reference/android/media/MediaPlayer.html
Upvotes: 0
Reputation: 2181
Before it's start consider doing the following:
if(mPlayer.isPlaying()) mPlayer.stop();
mPlayer.start();
Implement it inside your if
statements and see if it works for you!
But for this to work you have to define your MediaPlayer
object in a wider Scope and not declaring everytime a new MediaPlayer
object.
Upvotes: 0