-
Notifications
You must be signed in to change notification settings - Fork 12
Integration Tutorial
Integrating VPlayer is very easy!
First you must have compiled the project and imported it into Android Studio and attached the library to your project. Now you are ready to use this project and have videos playing in no time!
- Simple Programmatic Example
- Layout Example
- Get State Feedback: Listeners
- Resizing Video when Loaded
- Using VPlayerView Multiple Times
- Subtitles
Here is the most barebones example of an Android activity using VPlayer. Once this activity starts, it will immediately start playing the video till it is finished.
public class VideoActivity extends Activity {
private VPlayerView mPlayerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Attach the player
mPlayerView = new VPlayerView(this);
setContentView(mPlayerView);
// Set the content and play the video
mPlayerView.setDataSource(path);
mPlayerView.play();
}
@Override
protected void onPause() {
super.onPause();
mPlayerView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mPlayerView.onResume();
}
@Override
public void finish() {
super.finish();
mPlayerView.finish();
}
}Here the video will play after the activity has been created. Notice that we have not gone fullscreen nor resized the video correctly to its aspect ratio. The 3 lifecycle events have been used for onPause, onResume and finish. onPause and onResume give the video access to pause the video when users leave the app and come back later. Finish allows the video to cleanly exit without lag when hitting the back button or other means. If you put mPlayerView.finish() on onStop or onDestroy, it will be a bit late and the activity will lag a bit before going back to the previous activity. Activity's lifecycle when closing when called finish() or when the back button is pressed has the events in the following order:
finish()
onPause()
onStop()
onDestroy()Because finish() runs first, it gives the video player enough time to clean up and for the activity to exit.
If you include VPlayerView as a view inside layouts, you can do this:
<com.vplayer.VPlayerView
android:layout_width="match_parent"
android:layout_height="match_parent"
id="@+id/video"/>And in the java code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id.mylayout);
// Attach the player
mPlayerView = (VPlayerView)findViewById(R.id.video);
// Set the content and play the video
mPlayerView.setDataSource(path);
mPlayerView.play();
}To understand the state of your video you can use VPlayerListener. You use it like any other event listener in Java.
For example, you can detect when the video has loaded after you setDataSource.
mPlayerView.setVideoListener(new VPlayerListener() {
@Override
public void onMediaSourceLoaded(VPlayerException err, MediaStreamInfo[] streams) {
if (err != null) {
Toast.makeText(VideoActivity.this, "Unable to read the video!",
Toast.LENGTH_SHORT).show();
} else {
// Play the video
mPlayerView.play();
}
}
});Here you can detect if the video player was able to read your video file. If it can it will play otherwise it will tell the user via a toast that it has failed.
You can use the event onMediaSourceLoaded to get the width and height of the video:
mPlayerView.setVideoListener(new VPlayerListener() {
@Override
public void onMediaSourceLoaded(VPlayerException err, MediaStreamInfo[] streams) {
if (err != null) {
Toast.makeText(VideoActivity.this, "Unable to read the video!",
Toast.LENGTH_SHORT).show();
} else {
// Get the display dimensions
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
// Fill the screen with the video with correct aspect ratio
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)
mPlayerView.getLayoutParams();
params.height = size.y;
params.width = (int) (size.y * mPlayerView.getVideoWidth() * 1.0 /
mPlayerView.getVideoHeight());
params.gravity = Gravity.CENTER;
mPlayerView.setLayoutParams(params);
// Play the video
mPlayerView.play();
}
}
});We get the screen size and resize the view to fit the size of the screen while containing the correct aspect ratio. We get the width with mPlayerView.getVideoWidth() and height with mPlayerView.getVideoHeight(). Note that you can only query the height and width after the video is loaded, querying before will either get 0 or the last video.
Yes you can reuse the video player without needing to allocate another instance of it. All you need to do is stop it once you are done and setDataSource once you want to play a new video. Calling finish will stop the video from working and should be used only when you know the video won't be used anymore (like exiting the activity).
mPlayerView.setDataSource("/path/to/video.mp4");
mPlayerView.play();
// Later...
mPlayerView.stop(); // Finished with the video for now
// Later... Needs to play another video
mPlayerView.setDataSource("/path/to/video2.mp4");
mPlayerView.play();To play with subtitles you can use setDataSource to specify the file and the font file path. If the font does not exist, it will not play anything. Because setDataSource chooses the first stream if not specified, you may want to get the information after from onMediaSourceLoaded and set the source again with the correct subtitle stream index.