I want to play preroll ad (Just like you tube) before playing any video live streaming in my app. Right now i am able to play video using URL through streaming but not able to find any way to play preroll please some one help me to achieve this goal.
Just provide a ArrayList of videoUrls for your media player. Use the OnCompletionListener interface and play the next (none-preroll) video when the preroll has been completed. From what Ive understood you can't feed the Android media player a list of urls, you have to setDataSource for each video.
//Starting the mediaplayer
private void playVideo() {
mediaPlayer.setDataSource(videoUrlList.get(playlistPosition));
mediaPlayer.prepareAsync();
}
#Override
public void onCompletion(MediaPlayer mp) {
//Look if the list has more videos
if (playlistPosition < videoUrlList.size() - 1) {
playlistPosition++;
if(mediaPlayer != null)
mediaPlayer.reset();
playVideo();
}else{
//If no more videos, quit MoviePlayer Activity
finish();
}
}
Related
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.
I want to play video in a VideoView. which one is correct?
video.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
video.start();
}
});
or simply:
video.start();
Both method is right to play video but setOnPreparedListener is useful when you want to show ProgressBar when media is loading for play from SDCARD,webserver or streaming urls.
and if video.start(); called without setOnPreparedListener some delay occur during loading file then only black screen appear to user until video start.
can anyone teach me how to make videos play one after the other
I need the full code
I have 4 videos
Video1, Video2, Video3, Video4
I want Video1 to play first then followed Video2, then followed by Video3, then followed by Video4
String path="android.resource://" + getPackageName() +"/" + R.raw.Video1;
videoView1.setVideoURI(Uri.parse(path));
videoView1.start();
Short & Simple
Let's say you have four videos in array list
ArrayList<String> urlList = new ArrayList<>();
Create one counter variable to manage current video playing.
int video_counter = 0;
Now following code make your work easy to play dynamic range of video to play in loop (like one after another).
VideoView vv_video = findViewById(R.id.vv_video);
vv_video.setVideoURI(Uri.parse(urlList.get(counter_video_loop)));
vv_video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
vv_video.start();
}
});
vv_video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if ((video_counter + 1) <= urlList.size()) {
video_counter++;
vv_video.setVideoURI(Uri.parse(urlList.get(video_counter + 1)));
vv_video.start();
}
}
});
Assume you have a Videoview, you could for example put the paths to the videos into an array, and detect the end of playback like this:
videoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//start next video here
//for example, set video path to next array item
}
});
Maybe you should create a new VideoView to play next video at OnCompletionListener, and remove the old VideoView ,add the new VideoView.
It works for me.
use exoPlayer for concatenate multi video
I am using VideoView to play video.
If the video is not supported by the phone (such as my phone supporting only 480p video, not 720p which is the video I'm trying to play), it shows the dialog
Sorry,This video cannot be played
I want to do something after I click the dialog's "OK" button.
Where can I add the code to do this?
My code as below:
vv = (VideoView)this.findViewById(R.id.screen_video);
Uri uri = Uri.parse(FlePath);
vv.setVideoURI(uri);
vv.start();
After your code paste this.If you get this error then oncmpletion listner will call.
vv.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//release your MediaPlayer Resource
//do whatever you want
}
});
Hope this help you :)
I have the following code to take a video as a raw resource, start the video and loop it but I need the video to loop seamlessly as of now when it comes to an end of the clip and starts the clip again the transition between causes a flicker for a split second, which I really can't have for my app.
public class Example extends Activity {
VideoView vv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vv = (VideoView)findViewById(R.id.VideoView01);
//Video Loop
vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
vv.start(); //need to make transition seamless.
}
});
Uri uri = Uri.parse("android.resource://com.example/"
+ R.raw.video);
vv.setVideoURI(uri);
vv.requestFocus();
vv.start();
}
}
The clip is only 22 seconds long but was created to be seamless so it is possible to work without the delay.
Try this it will work 100%
VideoView videoView;<---write this in outside of method or else declare it as final variable.
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
In Kotlin simply use
videoView.setOnPreparedListener { it.isLooping = true }
Not sure if this helps years later, but I used
vv.start();
vv.setOnCompletionListener ( new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
vv.start();
}
});
and it has a seamless loop
The pause is for the underlying MediaPlayer to refresh its buffers. How long that will take will depend on a number of factors, many of which are outside your control (e.g., speed of CPU, speed of on-board flash storage).
One you can control is to get your video out of the resource and into the filesystem. Resources are stored in the APK, which is a ZIP file, so extracting the video this way probably takes extra time.
You may need to switch away from VideoView and use a SurfaceView with two MediaPlayers, alternating between them -- one is playing while the next is preparing, so when the playing one ends you can switch to the new player. I have not tried this, and so I do not know what the ramifications might be. However, I know that this technique is frequently used for audio playback to transition from one clip to another.
Little late, but any reason that you can't use the following?
MediaPlayer.setLooping(true);
If you are using Kotlin
videoView.setOnPreparedListener(object : MediaPlayer.OnPreparedListener {
override fun onPrepared(mp: MediaPlayer?) {
//Start Playback
videoView.start()
//Loop Video
mp!!.isLooping = true;
Log.i(TAG, "Video Started");
}
});
Using Arrow Expression short form
videoView.setOnPreparedListener { mp ->
//Start Playback
videoView.start()
//Loop Video
mp!!.isLooping = true;
Log.i(TAG, "Video Started");
};
Answer to this is to remove the audio from the video and convert that to a .ogg file which can be looped seamlessly and then use the video without audio to loop round and this works.
Here is answer friends, you must use vv.resume in setOnCompletionListener class
[https://stackoverflow.com/a/27606389/3414469][1]