I am created an Android app thta needs to have a fullscreen seemless video loop playing in the background. By 'in the background' I mean that there will be buttons on top of the video.
I've read these threadw already
playback video full screen
Integrating video file in android app as app background
but I'm still confused about the following
1 Is the mediaplayer needed for video playback?
2 Will using OnCompletionListener create a 'seamless' loop or will there be a 'hiccup' as the video loops?
Use the setOnPreparedListener to tell the MediaPlayer to loop and start
videoview.setOnPreparedListener(new OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
mp.start();
}
});
This is seemless on some devices, but can cause a frame or two of stutter on others :/
Related
I have an array of audio files and I want to play 3 audio files one after the other, so as the gap in between them is not noticeable.
I am trying it using onCompletion listener but unable to do.
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
// TODO Auto-generated method stub
if ((image100==1)&&(image10==0)&&(image1==1))
{
mediaplayer = MediaPlayer.create(getApplicationContext(), sounds[0]);
mediaplayer.start();
mediaplayer = MediaPlayer.create(getApplicationContext(), sounds[12]);
mediaplayer.start();
mediaplayer = MediaPlayer.create(getApplicationContext(), sounds[3]);
mediaplayer.start();
}
}
You may want to try using the AudioTrack class to play your sounds. With it, you can get very close and even overlapping sounds. To use it, you create separate threads for each sound.
Here's the AudioTrack reference page and here's an easy to follow blog entry where the author implements a piano with arbitrary length and simultaneous sounds. In it, he loops very short duration sounds, but you could easily adapt that to longer, single-play sounds.
If these 3 audio files are short sounds, like game sound effects or piano tones, you can use SoundPool for playing them.
Here is a link to the sample code:
Game Sound effects in Android
App I'm developing contains many short (1-2 sec) videos.
The videos are displayed in one activity. User can either replay video (possibly while video is beeing played) or change actual video.
Part of code changing video:
String videoPath = getVideoPath();
videoView.setVideoPath(videoPath);
videoView.start();
Those 3 lines already causes app to load new video and play it.
Problem starts after video is completed. From this point loading new video causes many problems (Like sometimes for half a movie only sound is played while screen is black blank). There are similar problems with replaying video (which I end up with calling 3 lanes from above).
It seems like android after completing movie releases resources or something like this (and that's why I am settings same path, when I want to replay video).
Ideally I would want video to simply pause and seekTo to beggining of movie after finished playing (but I cannot do this in OnCompletedListener, since it already changed state to stopped...).
Can I somehow achieve this? (By this I mean -> after completed video pauses and seekTo to beginning)
I already tried all combinations of pausing vidoes, suspending them, setting OnPreparedListener, setting OnCompletedListener.
Thx!
Try something like
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer arg0) {
mVideoView.start();
}
});
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.reset();
mVideoView.setVideoPath(file.getAbsolutePath());
mVideoView.start();
}
});
stop the playback, change video path, start video
videoView.stopPlayback();
videoView.setVideoPath(newVideoPath);
videoView.start();
I refactored my code in a following way: Everytime I need new video I reload whole activity. It works most of times, but now instead of video blackness at the beginning of playing I sometimes get "Cannot open media file" error.
Has it something to do with android resource managment? I release mediaPlayer in onCompletionListener.
Has anyone had such problem with playing many videos from external storage?
You can do something like this:
videoView.setOnCompletionListener(MediaPlayer.OnCompletionListener { mp ->
mp.seekTo(0);//go to second 0
mp.start()// start again
})
I discover in this link:
https://developer.android.com/reference/android/media/MediaPlayer
Do this.
videoView.setOnPreparedListener { mp ->
mp.isLooping = true
)
In my android application I only play a looping sound with MediaPlayer. I'm trying to loop fluid, eliminating the time between the end of the track and the beginning of the next reading.
I tried with the function setLooping but there is a time between sounds.
I tried with setOnCompletionListener function like this:
MP.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
MP.start();
}
});
However, there are still a few seconds between sounds.
Apparently it is not possible with MediaPlayer; there's an open bug asking for gapless playback that has been open since 2009.
Android: MediaPlayer gapless or seamless Video Playing also has several non-answers on the topic.
In iPhone, we can use AVQueuePlayer, AVAsset, and AVPlayerItem if we want to play a list of movies sequentially. It is very convenient over MPMediaPlayer and MPMediaPlayerController in iPhone. From, apple documentation:
AVQueuePlayer is a subclass of AVPlayer you use to play a number
of items in sequence.
So, my question is, is there anything like that in android which we can use instead of MediaPlayer and VideoView.
In Android, there is no automatic way of playing sounds in sequence (at least not a "built-in" one). If you need to start playback of the second sound when the first one finishes, you can use method
public void setOnCompletionListener (MediaPlayer.OnCompletionListener listener)
of the MediaPlayer to start the second playback when the first one is finished. Something like this:
MediaPlayer player = MediaPlayer.create(context, uriOfFirstSound);
player.setOnCompletionListener(new OnCompletionListener(
public void onCompletion(MediaPlayer mp) {
mp.setDataSource(context, uriOfNextSound);
mp.prepare();
mp.start();
}
));
player.prepare();
player.start();
Naturally, if you have more than two sounds, you can do this in a loop.
I am Playing the video in loop using set Looping(true) option and i will stop the media player after a particular event happened. It is working fine. But i want to know the number of times that my video got played in the loop.
MediaPlayer doesn't provide that information, you need to make it loop yourself and count how many times it has restarted. To do this, something in your app will need to extend OnCompletionListener and do something like
int count = 0;
public void onCompletion(MediaPlayer mediaPlayer) {
count++;
mediaplayer.seekTo(0);
mediaplayer.start();
}
And you need to set mediaPlayer.setLooping(false)