I want to repeat playing audio. so if first clicking button audio play, and second clicking same button repeat same audio without stopping. How can I do this? Here's what I've tried:
if (mp != null){
mp.stop();
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.seekTo(0);
mp = null;
}
else {
mp = MediaPlayer.create(Takbiratul_Ihram.this, R.raw.takbir_cowok);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener(){
public void onCompletion (MediaPlayer arg0){
mp = null;
}
});
mediaPlayer.setLooping(true);
More information here here
Related
I am using MediaPlayer to playback audio file of .WAV format. When I debug the app I'm able to play file but when I run the app file is not playing.
There is a similar issue regarding this but no solution is provided.
Below is the code which i am using
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(mContext, fileUri);// uri of the audio file
mMediaPlayer.prepare();
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.start();// to start the playback of the audio file
This code is working only in debug mode but not in normal mode.
Thanks
This is what I am saying:
btn_next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mp.reset();
mp.setDataSource(url.toString());
mp.setOnErrorListener(this);
mp.setOnPreparedListener(this);
mp.setOnCompletionListener(this);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
Log.e(TAG, "SecurityException");
} catch (IllegalStateException e) {
Log.e(TAG, "IllegalStateException");
} catch (IOException e) {
Log.e(TAG, "IOException");
}
mp.prepareAsync();
}
});
Now, override method onPrepared().
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
}
Comment below if you face any issue in this.
Now i develop a game application and as all games on google play, they have a toggle button to switch on/off the sound.
Is there any technique i can use to disable all sounds in the game with one step or i have to disable every sound separately ??
I use MediaPlayer for playing sounds
private int playSound(int file) {
int duration = 1000;
sound = new MediaPlayer();
AssetFileDescriptor fd = getResources().openRawResourceFd(file);
try {
sound.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
sound.prepare();
sound.start();
sound.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// Do the work after completion of audio
Toast.makeText(GameActivity.this, "good", Toast.LENGTH_SHORT).show();
mp.release();
}
});
duration = sound.getDuration();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return duration;
}
try this
media.setVolume(0,0);
I have added two buttons to my app, one to play and one to start. As soon as i start the app the songs starts playing automatically. when i press on stops it stops but when i press on play it doesnt starts again
To stop I have used mediaplayer.stop() which is working fine
To start I have used
mediaplayer.start()
which is not working
According to flow diagram its given we need to use
prepare()
and then
OnPreparedListener.onPrepared()
and then
start()
I don't know how to use these functions. Please help me
Don't call stop() for stopping the mediaplayer use reset() instead. I dont have good reason for that. But, it'll work for sure.
// for stopping it call below statement
if(mediaPlayer.isPlaying())
mediaPlayer.reset();
//for playing it again
mediaPlayer.prepare();
mediaPlayer.start();
For starting media player again use following code.
if (mediaplayer != null) {
mediaplayer.start();
}
and one more thing, instead of using
mediaplayer.stop()
use
mediaplayer.pause();
so it will pause the current song instead of stop.
Just check the condition whether it is null or not.
Try following code on Play Button
mp.reset();
mp.setDataSource(song path);
mp.prepare();
mp.start();
mp.setDataSource(song path); not necessary if you playing same sound again
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.singleitemview);
btnsound=(Button)findViewById(R.id.play_sound);
btnstopsound=(Button)findViewById(R.id.stop_sound);
mp=new MediaPlayer();
btnsound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
mp.setDataSource(Sound);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
mp.setLooping(true);
}
});
btnstopsound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.setLooping(false);
mp.stop();
}
});
}
I am making an app that uses sound effects. I would like to know how i can detect when a sound has finished playing and trigger an event .
i tried this :
player = new MediaPlayer();
if (player != null) {
player.reset();
player.release();
}
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
sonar.setBackgroundResource(R.drawable.sonar_off);
stopPlay();
}
});
if(state==1){
player = MediaPlayer.create(this, R.raw.sonar_slow);
}else if(state==2){
player = MediaPlayer.create(this, R.raw.sonar_medium);
}else if(state==3){
player = MediaPlayer.create(this, R.raw.sonar_fast);
}
try{
player.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
player.start();
You can set an OnCompleteListener() to the player object. The code for it is:
mPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp)
{
"your code comes here"
}
});
Here the mPlayer is the object of the MediaPlayer which is currently running.
There are multiple ways to play sounds, assuming you use the MediaPlayer class you could register a MediaPlayer.OnCompletionListener
I've just spent about 2 hours trying to get this to work. I've never had much luck with getting videos to play with MediaPlayer. What am I doing wrong here? It plays just audio the first time through, no video. And then on the second time I get IllegalStateException's when I try to re-setDataSource. I've tried not calling stop() and release() in onCompletion I've tried calling reset() before playing. I just get different StateExceptions (0, 64, 128) I've run out of ideas.
private void playVideo() {
mMediaPlaying = true;
sv.setVisibility(View.VISIBLE); //surfaceview
try {
if(mp.isPlaying()) {
mp.stop();
mp.reset();
}
mp.setDisplay(sh); //surfaceholder
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(this);
mp.setDataSource(getBaseContext(), Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test));
mp.prepare();
//mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void onPrepared(MediaPlayer mp) {
Log.d("", "mp prepared");
mp.start();
}
#Override
public void onCompletion(MediaPlayer arg0) {
sv.setVisibility(View.GONE);
mp.stop();
mp.release();
mMediaPlaying = false;
Log.d("", "Done playing media");
}
You can use VideoView if you want to play video.
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/VideoViewDemo.html
And you can use MediaController to control the video such as play,pause,forward and rewind.
Check this link for example.
http://android-coding.blogspot.in/2011/03/using-videoview-to-play-mp4-from-sdcard.html
mp.reset() must be call before setDataSource,
have a look at the life cycle of MediaPlayer
http://developer.android.com/reference/android/media/MediaPlayer.html
Ok, I basically copy/pasted this here and it seems to be working... Except the first time it plays there is still no video... But I got rid of the errors :)
try this:
private void playVideo() {
mMediaPlaying = true;
sv.setVisibility(View.VISIBLE); //surfaceview
try {
if(mp != null) {
mp.stop();
mp.release();
}
mp = new MediaPlayer();
mp.setDisplay(sh); //surfaceholder
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(this);
mp.setDataSource(getBaseContext(), Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test));
mp.prepare();
mp.start();