audio of Video play not synchronise properly - android

I am working on customized video play in android. I am using following code for video play.
mp.reset();
mp.setDataSource(songsList.get(songIndex).get("songPath"));
mp.prepare();
mp.start();
videoview.setVideoPath(songsList.get(songIndex).get("songPath"));
videoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
//pDialog.dismiss();
videoview.start();
}
})
I am facing problem that sound repeats twice & so its mixing during video play. Can you please guide me what will be the issue. Kindly help.

You are starting the same file twice, try the following...
videoview.setVideoPath(songsList.get(songIndex).get("songPath"));
videoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
//pDialog.dismiss();
videoview.start();
}
})

Related

VideoView and MediaPlayer seekTo always starts from beginning

I have some problem with seeking video played in VideoView and resuming it.
Here's some part of my code:
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mp) {
Log.d("V", "onSeekComplete1 " + videoView.getCurrentPosition());
videoView.start();
Log.d("V", "onSeekComplete1 " + videoView.getCurrentPosition());
}
});
}
});
videoView.setVideoURI('some local uri');
At some point I'm pausing video using:
videoView.pause();
After button click I'm seeking video to certain time, eg. 10 seconds (10 000 ms).
#OnClick(R2.id.ivMove)
void clickMove() {
videoView.seekTo(10000);
}
After that in log I see:
onSeekComplete1 10000
onSeekComplete2 0
So after calling videoView.start() it is starting video from beginning!
But when I call start() without seeking video (eg. one button calls pause() and second calls start() then video is playing correctly - from the moment when it was paused, not from beginning.
Could you tell me what I'm doing wrong?
The problem was caused by MediaPlayer - it is seeking to closest keyframe of the video and it turns out that my testing video has keyframes every ~30 seconds.
I changed to ExoPlayer https://github.com/google/ExoPlayer - it can seek to exact time.
There is a new overload for the MediaPlayer seekTo method. It has a second int parameter, in which you can pass a SEEK_CLOSEST constant. According to Google, "This mode is used with seekTo(long, int) to move media position to a frame (not necessarily a key frame) associated with a data source that is located closest to or at the given time." In testing my own app, it solves the problem for API level >= 26. I'm not sure if there are any good options prior to API level 26.
Try videoView.resume() instead of start()
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mp) {
Log.d("V", "onSeekComplete1 " + videoView.getCurrentPosition());
videoView.resume();
Log.d("V", "onSeekComplete1 " + videoView.getCurrentPosition());
}
});
}
});
videoView.setVideoURI('some local uri');
Try this solution. It works like charm....
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mp) {
mp.start();
}
});
}
});

Playing sounds in Android in certain order

In an Android app I am using MediaPlayer to play sound files.
This is just for personal use and will not be published.
I have several references to the sound files:
MediaPlayer dooropen = MediaPlayer.create(MainActivity.this, R.raw.dooropen);
MediaPlayer doorclose = MediaPlayer.create(MainActivity.this, R.raw.doorclose);
//...
For example the length of the dooropen sound clip is 2 seconds so after I play it I sleep for 2.5 seconds and then play the doorclose sound clip, like so
dooropen.start();
try{ Thread.sleep(2500); }catch(InterruptedException e){ }
doorclose.start();
The issue I am having is some of my sound files are not playing in the order I have them in.
There does not seem to be any reason why certain sound files do not play, because if I play them at the top of my onCreate() procedure they all play, it is only when I try and play them in a certain order.
You should implement the setOnCompletionListener() of the mediaplayer to get a callback when playback has completed and then load another audio file that needs to start playing.
See MediaPlayer Documentation about the mediaplayer state.
Yes you can use MediaPlayer along with oncompletionListener or you may try reseting the mediaplayer after one audio is completed. example code here. You may also use session id to keep track of which file was playing and which to start now.
mPlayer = new MediaPlayer();
//set other attributes here
mPlayer.setAudioSessionId(1);
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//check which audio session was playing and set new datasource and session
mPlayer.reset();
//set other data source here and different session id
}
});
Hope it solves your problem.
If you want to play sound in order, try this:
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp2.start();
}
});
mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp3.start();
}
});
mp3.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp1.start();
}
});

android: do something after playing audio file

i try to stream and play an audio file in my application. i use this code :
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(url);
player.prepare();
player.start();
i want to know is it possible to detect when player finished playing ? i want to change a button color after player finished playing
Check out the setOnCompletionListener method:
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Log.d(TAG, "Done playing!");
}
});
You can actually set a ton of callbacks here. In addition to setOnCompletionListener, we have:
setOnBufferingUpdateListener
setOnErrorListener
setOnInfoListener
setOnPreparedListener
setOnSeekCompleteListener
setOnTimedMetaDataAvailableListener
setOnTimedTextListener
setOnVideoSizeChangedListener.

android repeat sound lag

I am working on a application in which i have to play a horn sound and i am having a sound clip of it. Now i want to play this sound file in a loop but i am getting a second lag once its repeat the same file. I am using media player class to play the sound.
Please let me know how i can remove this lag to play a sound file without a lag.
final MediaPlayer mp_horn1 = MediaPlayer.create(this, R.raw.horn_wf);
mp_start.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp_start.start();
}
});
Hey use this code for play sound in loop.
final MediaPlayer mp_horn1 = MediaPlayer.create(this, R.raw.horn_wf);
mp_start.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp_horn1..setLooping(true);
mp_start.start();
}
});
You have to use this - .setLooping(true);
Hope this will work. All the best.

Android play multiple mp4 files in succession

I am developing a DASH client for Android and am testing the playback multiple video segments in mp4 format on Android. Therefore I tried two approaches so far:
using VideoView:
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.setVisibility(View.VISIBLE);
videoView.start();
}
});
using TextureView:
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
try {
mMediaPlayer.setSurface(surface);
mMediaPlayer.reset();
mMediaPlayer.setDataSource(file.getAbsolutePath());
mMediaPlayer.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
}
});
Okay both work but have a little lag when switching to the next file. What can cause that? Am I doing something wrong and can improve the code? I read about using the MediaExtractor and MediaCodec APIs for better playback of multiple videos which were also improved for DASH compatibility with Android verisons 4.3 and 4.4 but I haven't understood yet how to use them to play video and audio on a surface. I can only find sources on how to us them in order to play video or audio.
I hope somebody can help me to play multiple video segments in succession seamlessly.

Categories

Resources