Better Video Player

Additional

Language
Kotlin
Version
2.0.0-alpha01 (Dec 6, 2018)
Created
Sep 16, 2016
Updated
Jan 10, 2019
Owner
Halil Ozercan (halilozercan)
Contributors
Aidan Follestad (afollestad)
franciscofranco
bulwinkel
Halil Ozercan (halilozercan)
Márton Braun (zsmb13)
autovalue
porkyhead
hexlay
8
Activity
Badge
Generate
Download
Source code
APK file

Blurb

Better Video Player

Features
  • Completely written in Kotlin.
  • Based on the stock MediaPlayer API It will work on all devices and all CPUs, and it works with both local and remote sources.
  • Simple Much less code is required than alternative options to get up and running.
  • Very configurable There are lots of options available to make the player behave exactly how you want it to behave.
  • Swipe Gestures Supports the common on-screen scroll behavior which is used by MXPlayer, VLC and other Android video players.
  • Tap Gestures Double tap on right or left side of the screen to jump back and forward like Youtube and Netflix players.


Add this in your module's build.gradle file:

dependencies {
    // ... other dependencies
    compile 'com.github.halilozercan:BetterVideoPlayer:kotlin-SNAPSHOT'
}

Getting Started

Configuring a Player Activity

You will need an Activity in your app that will hold the BetterVideoPlayer view and playback content. There's only a bit of configuration required. However, BetterVideoPlayer offers great 'customizability'.

Host Activity should disable recreation on orientation changes. This allows playback to continue when the device orientation changes. The player will adapt the aspect ratio accordingly. You just need to set android:configChanges values to your Activity in AndroidManifest.xml:

<activity
    android:name=".MyPlayerActivity"
    android:label="@string/my_player_activity"
    android:configChanges="orientation|keyboardHidden|screenLayout|screenSize" /> 
Layouts

The layout for your player Activity can be very simple. You only need a BetterVideoPlayer view, all the controls and everything else are created by the player itself.

<com.halilibo.bvpkotlin.BetterVideoPlayer
    android:id="@+id/player"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Notable Features

BetterVideoPlayer is capable of almost all functionality that you expect from a VideoPlayer. However, it is important to repeat that BetterVideoPlayer uses Android MediaPlayer API. Thus, it does not provide every codec in the world. In the future, there is a plan for moving underlying player to ExoPlayer.

Captions

BetterVideoPlayer supports captions in 2 formats; SRT and WEBVTT. Support for more formats through pull requests will be appreciated.

Captions can be obtained both online and from resource directory. BetterVideoPlayer currently does not support captions from local file storage.

// Online SUBRIP subtitle
bvp.setCaptions("https://www.example.com/subrip.srt", CaptionsView.SubMime.SUBRIP)

// res/raw SUBRIP subtitle
bvp.setCaptions(R.raw.sub, CaptionsView.SubMime.SUBRIP)

BetterVideoPlayer also lets you define the text size(in sp) and color of captions inside XML view.

<com.halilibo.bvpkotlin.BetterVideoPlayer
        android:id="@+id/bvp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        bvp:bvp_captionSize="20sp"
        bvp:bvp_captionColor="@android:color/holo_blue_light"/>

Toolbar

BetterVideoPlayer deploys a common toolbar at the top of the player. Toolbar is useful in a video player in two different ways.

  • It offers a highly customizable Title area.
  • You can inflate a menu on the toolbar and define as many actions as you need.

To access toolbar, just use getToolbar() method. You can also show and hide the toolbar by using showToolbar() and hideToolbar(). Besides these methods, it is not recommended to alter Toolbar's visibility.

Swipe Gestures

Swipe Gestures on a video player are proved to be very useful by MX, VLC and others. Swiping left and right to seek to any point in video or swipe up and down to control volume and brightness. BetterVideoPlayer comes with built-in support for these gestures. This feature enables developers to have a player that their users are familiar with.

You can enable or disable gestures by enableSwipeGestures() and disableSwipeGestures() methods.

Important point: You need to use enableSwipeGestures(Window) method to also enable brightness control. Brightness setting needs a reference window.

Code Setup

Initializing the player is very simple. You just set a callback listener and a source.

class MyPlayerActivity : AppCompatActivity , BetterVideoCallback {

    lateinit var player: BetterVideoPlayer

    override fun onCreate(savedInstanceState: Bundle) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_myplayer)

        // Grab a reference to the player view
        player = findViewById(R.id.player)

        // Set the source to the HTTP URL held in the TEST_URL variable.
        // To play files, you can use Uri.fromFile(new File("..."))
        player.setSource(Uri.parse(TEST_URL))

        // From here, the player view will show a progress indicator until the player is prepared.
        // Once it's prepared, the progress indicator goes away and the controls become enabled for the user to begin playback.
    }

    override fun onPause() {
        super.onPause()
        // Make sure the player stops playing if the user presses the home button.
        player.pause()
    }
    
    companion object {
        const val TEST_URL = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
    }
}

You can see the almost identical code in action in the sample project.