Audio and Video Player for Android with HLS and DASH support. Based on ExoPlayer
Chromecast library was deprecated so we have removed AJCast module
If you want to add chromecast feature:
https://developers.google.com/cast/docs/developers
https://developers.google.com/cast/docs/android_sender/integrate
repositories {
maven { url 'https://github.com/anthorlop/mvn-android/raw/master/' }
}
// AJCPlayer gradle dependencies
compile 'es.lombrinus.projects.mods:AJCPlayer:1.0.6'
compile 'es.lombrinus.projects.mods:AJCNotification:1.0' // if you want notifications
In activity layout:
<FrameLayout
android:id="@+id/videoSurfaceContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true">
<com.google.android.exoplayer.AspectRatioFrameLayout
android:id="@+id/video_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<SurfaceView
android:id="@+id/videoSurface"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</com.google.android.exoplayer.AspectRatioFrameLayout>
</FrameLayout>
Activity class: Create an instance of AudioPlayer or VideoPlayer:
AJCPlayer videoPlayer = new VideoPlayer(this, new MediaPlayer());
If you are playing video you have to load SurfaceHolder:
// load SurfaceHolder
SurfaceHolder holder = mSurfaceView.getHolder();
DisplayMetrics metrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
holder.setFixedSize(metrics.widthPixels, (int) ((float) metrics.widthPixels / (float) 16 / (float) 9));
holder.addCallback(this);
And your activity has to implement SurfaceHolder.Callback. You can play video on surfaceCreated(...) method:
// views you want to hide automatically when content is playing
View viewBot = findViewById(R.id.audioPlayerLayoutBottom);
View viewTop = findViewById(R.id.audioPlayerLayoutTop);
CList viewsToHide = new CList(viewBot, viewTop);
// we send FrameLayout as param because AJCPlayer uses this view to detect click and double click
FrameLayout mFrameLayout = (FrameLayout) findViewById(R.id.videoSurfaceContainer);
final VideoPlayerView videoPlayerView = new VideoPlayerView(mFrameLayout, surfaceHolder, currentPositionTextView, durationTextView, seekBar, viewsToHide); // you could send currentPosition, duration or seekbar views as null
final VideoPlayerOptions options = new VideoPlayerOptions(ActivityInfo.SCREEN_ORIENTATION_SENSOR, true, true, true);
final Controls controls = new Controls(plays, pauses, stops);
controlBarManager = new VideoControlBarManager(context, controls, new LoadingView(){...}, new OnDoubleClick{...}, videoPlayerView, options); //LoadingView you can hide/show your progress bar. LoadingView and OnDoubleClick could be null
videoPlayer.addEventListener(controlBarManager);
Create Asset and Play video
Asset asset = new Asset(idString, urlString, ContentType.VIDEO);
videoPlayer.play(asset, true); // autoplay=true
Create Asset and Play video (streaming or local) from a position
Asset asset = new Asset(idString, urlString, ContentType.VIDEO);
if (TextUtils.isEmpty(localPath)) {
asset.setmUrl(localPath);
asset.setLocalPath(true);
}
if (currentPosition <= 0) {
videoPlayer.play(asset, autoPlay);
} else {
videoPlayer.play(asset, currentPosition, autoPlay);
}
The purpose of this library is to decouple the video player and audio from the applications allowing to customize the view of the player from your application.
Include in 'build.gradle' file:
repositories {
maven { url 'https://github.com/anthorlop/mvn-android/raw/master/' }
}
// AJCPlayer dependencies
compile 'es.lombrinus.projects.mods:AJCPlayer:1.0.6'
compile 'es.lombrinus.projects.mods:AJCNotification:1.0' // if you want notifications
AJCPlayer is the main interface.
AJCPlayer has two implementations (AudioPlayer and VideoPlayer).
The sample project use Dagger to view inyection. (It is not necessary to use Dagger) If you decide to use Dagger it will be necessary to do a Build Project.
You can check the project code: PlayerComponent.java and ExampleApp.java
Without Dagger:
AJCPlayer videoPlayer = new VideoPlayer(context, new MediaPlayer());
As we saw before, AJCPlayer defines a method to add implementations of PlayerEventListener to our player. These implementations will be executed from the library when the state of our player changes, for example if the content is paused, the pause event will be thrown in all its listeners.
PlayerEventListener interface:
public interface PlayerEventListener {
void onPreparing(Asset asset, MediaPlayer mediaPlayer);
void onPlayBegins(Asset asset, int duration);
void onResume(Asset asset, int currentPosition);
void onCompletion(Asset asset);
void onPause(Asset asset, int currentPosition);
void onForward(Asset asset, int currentPosition);
}
Then, we have to add listeners before play content. There are some listeners already created that we can use:
final VideoPlayerView videoPlayerView = new VideoPlayerView(mFrameLayout, surfaceHolder, current, duration, seekBar, controller);
final VideoPlayerOptions options = new VideoPlayerOptions(ActivityInfo.SCREEN_ORIENTATION_SENSOR, true, true, true);
final Controls controls = new Controls(plays, pauses, stops);
controlBarManager = new VideoControlBarManager(context, controls, new LoadingView(){...}, new OnDoubleClick{...}, videoPlayerView, options);
videoPlayer.addEventListener(controlBarManager);
SubtitleManager Implementation to detect and send subtitles that we should to show.
final SubtitleManager subtitleManager = new SubtitleManager(activity, urlVtt, new OnSubtitleDetect(){...});
videoPlayer.addEventListener(subtitleManager);
NotificationPlayerManager Implementation to launch automatically a notification from which user could pause, play or stop player.
// small notification view
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.audio_player_notification_little);
// big notification view
RemoteViews bigContentView = new RemoteViews(getPackageName(), R.layout.audio_player_notification);
// set views PlayerNotificationControls notifControls = new PlayerNotificationControls( R.mipmap.ic_audio_small_icon, contentView, bigContentView, R.id.play_icon, R.id.pause_icon, R.id.stop_icon, R.id.progressbar_player);
// AJCNotification audioNotification = new PlayerNotification(context, audioPlayer); audioPlayer.addEventListener(new NotificationPlayerManager(notifConfig, audioNotification, new NotificationCallback(){...));
NotificationCallback interface methods:
```java
RemoteViews notificationChange(Notification notification, int notificationId, RemoteViews remoteViews);
void notificationViewClicked(int viewId);