I am trying to play 3 audios width Android but not at the same time, my code is the next one:
for (Word w : wordsList){
String path = Parameters.PATH_AUDIO + "/" + w.getAudioPath();
Uri audioPath = Uri.parse(path);
MediaPlayer audio1 = MediaPlayer.create(context, audioPath);
audio1.start();
}
But all audios start to play at least at the same time. I need something like audio1.wait() or audio1.sleep or someone like this.
You might want to consider using setOnCompletionListener:
audio1.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer player) {
//do something when audio is finished
}});
Here's an example (didn't have the chance to try it though):
Decalre these in the class level:
int index = 0;
MediaPlayer audio1;
and then:
try {
audio1 = MediaPlayer.create(this, Uri.parse(yourList.get(index)));
audio1.start();
index++;
audio1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer player) {
if (index < yourList.size()) {
audio1.reset();
audio1 = MediaPlayer.create(MainActivity.this,
Uri.parse(yourList.get(index)));
audio1.start();
index++;
}
else
{
//release it
}
}
});
} 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();
}
Related
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 a problem when playing an mp3 in android, is something like a delay or a lag, ex:
if I have to reproduce the following: "Hello, how are you?", it only plays "how are you?" or says very low the "hello".
It happens in a ViewSonic V220 its a 22" tablet, in most of other devices, it works fine, but is in that one that seems to fail.
Its weird, becouse other apps(like youtube or media player) works fine.
This is my code, maybe i am doing something wrong:
public class SoundManager implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener {
private Parent mParent;
private MediaPlayer mediaPlayer;
String[] mp3_array;
int counter = 0;
public SoundManager(Parent parent) {
mParent = parent;
}
public void playSound(String[] url) throws IllegalArgumentException,
IllegalStateException, IOException {
mp3_array = url;
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset();
}
mediaPlayer.setDataSource(url[0]);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.prepareAsync();
}
public void stopMediaPlayer() {
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
mp3_array = null;
counter = 0;
}
}
#Override
public void onCompletion(MediaPlayer mp) {
try {
Integer c = counter;
if (mp3_array != null && counter + 1 < mp3_array.length) {
mp.reset();
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(this);
counter += 1;
mp.setDataSource(mp3_array[counter]);
mediaPlayer.prepareAsync();
} else {
if (mParent != null)
mParent.invokeJs("playSoundEnded()");
mp.release();
mp = null;
mp3_array = null;
counter = 0;
}
} 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();
}
}
#Override
public void onPrepared(MediaPlayer mp) {
if (mParent != null)
mParent.invokeJs("playSoundStarted()");
mp.start();
}
}
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
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();
In my application, i have to record sound with music. When i am clicking on record button , music starts and recording will starting too. User will sing the song and this song will record with music .
In my code song is not recording with music. it records only sound of user. Kindly check my code and suggest me.
recorder = new MediaRecorder();
record_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
mPlayer = MediaPlayer.create(RecordVoiceActivity.this, R.raw.intro);
mPlayer.start();
long dtMili = System.currentTimeMillis();
primarySongName=""+dtMili;
// Date dt = new Date(dtMili);
Toast.makeText(RecordVoiceActivity.this,
"Recording starting", Toast.LENGTH_LONG).show();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
currentDateTimeString = DateFormat.getDateInstance()
.format(new Date());
Log.d("##################################", "" + dtMili);
recorder.setOutputFile("/sdcard/Recordedsong_" + dtMili + ".3gp");
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
stop_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
// TODO Auto-generated method stub
Toast.makeText(RecordVoiceActivity.this,
"Recording stopped", Toast.LENGTH_LONG).show();
mPlayer.stop();
recorder.stop();
recorder.release();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
replay_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
// TODO Auto-generated method stub
Log.d("############################", currentDateTimeString);
Uri path = Uri.parse(Environment.getExternalStorageDirectory()
+ "/" + "Recordedsong_" + primarySongName
+ ".3gp");
MediaPlayer player = MediaPlayer.create(
RecordVoiceActivity.this, path);
player.setLooping(false);
player.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
In this code , M recording the song and saving in my Sd card. Then replaying from Sdcard .
But song is recording without music which i am playing.