Skip to content

Commit

Permalink
Feature/februrary changes 10 (jhomlala#340)
Browse files Browse the repository at this point in the history
* Fixed WebVTT subtitles parsing

* Updated ExoPlayer version, added missing dispose call from BetterPlayer widget.

* Updated ExoPlayer version, added missing dispose call from BetterPlayer widget.

* Added fix for iOS aspect ratio issue

* Added fix for iOS aspect ratio issue

* Fixed auto play issue where player starts video after load initialization process and player is not visible

* Updated texts in examples

* Updated changelog

* Lint & format fixes

* Lint & format fixes

* Lint & format fixes
  • Loading branch information
jhomlala authored Feb 25, 2021
1 parent d546adf commit a73b56b
Show file tree
Hide file tree
Showing 26 changed files with 161 additions and 128 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 0.0.59
* Fixed WEBVTT subtitles parsing.
* Updated ExoPlayer version.
* Refactored ExoPlayer code.
* Added missing controller dispose from BetterPlayer widget dispose.
* Added fix for iOS aspect ratio issue.
* Fixed auto play issue where player starts video after load initialization process and player is not visible.
* Updated texts in examples.
* Added missing widevine DRM parameters (by https://github.com/FlutterSu)

## 0.0.58
* Added overflowModalColor and overflowModalTextColor in BetterPlayerControlsConfiguration.
* Disabled picture in picture in fullscreen mode.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ This plugin is based on [Chewie](https://github.com/brianegan/chewie). Chewie is

```yaml
dependencies:
better_player: ^0.0.58
better_player: ^0.0.59
```
2. Install it
Expand Down
12 changes: 6 additions & 6 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ android {
}

dependencies {
implementation 'com.google.android.exoplayer:exoplayer-core:2.12.2'
implementation 'com.google.android.exoplayer:exoplayer-hls:2.12.2'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.12.2'
implementation 'com.google.android.exoplayer:exoplayer-smoothstreaming:2.12.2'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.12.2'
implementation 'com.google.android.exoplayer:extension-mediasession:2.12.2'
implementation 'com.google.android.exoplayer:exoplayer-core:2.13.1'
implementation 'com.google.android.exoplayer:exoplayer-hls:2.13.1'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.13.1'
implementation 'com.google.android.exoplayer:exoplayer-smoothstreaming:2.13.1'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.13.1'
implementation 'com.google.android.exoplayer:extension-mediasession:2.13.1'
implementation "android.arch.lifecycle:runtime:1.1.1"
implementation "android.arch.lifecycle:common:1.1.1"
implementation "android.arch.lifecycle:common-java8:1.1.1"
Expand Down
106 changes: 57 additions & 49 deletions android/src/main/java/com/jhomlala/better_player/BetterPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.ui.PlayerNotificationManager;

Expand Down Expand Up @@ -142,7 +141,7 @@ void setDataSource(

if (licenseUrl != null && !licenseUrl.isEmpty()) {
HttpMediaDrmCallback httpMediaDrmCallback =
new HttpMediaDrmCallback(licenseUrl, new DefaultHttpDataSourceFactory());
new HttpMediaDrmCallback(licenseUrl, new DefaultHttpDataSource.Factory());
if (Util.SDK_INT < 18) {
Log.e(TAG, "Protected content not supported on API levels below 18");
drmSessionManager = null;
Expand Down Expand Up @@ -170,25 +169,21 @@ void setDataSource(
}

if (isHTTP(uri)) {
DefaultHttpDataSourceFactory defaultHttpDataSourceFactory =
new DefaultHttpDataSourceFactory(
userAgent,
null,
DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS,
true);
dataSourceFactory = new DefaultHttpDataSource.Factory()
.setUserAgent(userAgent)
.setAllowCrossProtocolRedirects(true)
.setConnectTimeoutMs(DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS)
.setReadTimeoutMs(DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS);

if (headers != null) {
defaultHttpDataSourceFactory.getDefaultRequestProperties().set(headers);
((DefaultHttpDataSource.Factory) dataSourceFactory).setDefaultRequestProperties(headers);
}

if (useCache && maxCacheSize > 0 && maxCacheFileSize > 0) {
dataSourceFactory =
new CacheDataSourceFactory(context, maxCacheSize, maxCacheFileSize, defaultHttpDataSourceFactory);
} else {
dataSourceFactory = defaultHttpDataSourceFactory;
new CacheDataSourceFactory(context, maxCacheSize, maxCacheFileSize, dataSourceFactory);
}
} else {

dataSourceFactory = new DefaultDataSourceFactory(context, userAgent);
}

Expand Down Expand Up @@ -275,6 +270,7 @@ public Bitmap getCurrentLargeIcon(@NonNull Player player,
playerNotificationManager.setUsePreviousAction(false);
playerNotificationManager.setUseStopAction(false);


MediaSessionCompat mediaSession = setupMediaSession(context, false);
playerNotificationManager.setMediaSessionToken(mediaSession.getSessionToken());

Expand Down Expand Up @@ -335,10 +331,7 @@ public boolean dispatchSetPlayWhenReady(Player player, boolean playWhenReady) {

@Override
public boolean dispatchSeekTo(Player player, int windowIndex, long positionMs) {
Map<String, Object> event = new HashMap<>();
event.put("event", "seek");
event.put("position", positionMs);
eventSink.success(event);
sendSeekToEvent(positionMs);
return true;
}

Expand All @@ -354,12 +347,14 @@ public boolean dispatchNext(Player player) {

@Override
public boolean dispatchRewind(Player player) {
sendSeekToEvent(player.getCurrentPosition() - 5000);
return false;
}

@Override
public boolean dispatchFastForward(Player player) {
return false;
sendSeekToEvent(player.getCurrentPosition() + 5000);
return true;
}

@Override
Expand All @@ -378,13 +373,18 @@ public boolean dispatchStop(Player player, boolean reset) {
}

@Override
public boolean isRewindEnabled() {
public boolean dispatchSetPlaybackParameters(Player player, PlaybackParameters playbackParameters) {
return false;
}

@Override
public boolean isRewindEnabled() {
return true;
}

@Override
public boolean isFastForwardEnabled() {
return false;
return true;
}
};
}
Expand Down Expand Up @@ -437,7 +437,11 @@ private MediaSource buildMediaSource(
Uri uri, DataSource.Factory mediaDataSourceFactory, String formatHint, Context context) {
int type;
if (formatHint == null) {
type = Util.inferContentType(uri.getLastPathSegment());
String lastPathSegment = uri.getLastPathSegment();
if (lastPathSegment == null) {
lastPathSegment = "";
}
type = Util.inferContentType(lastPathSegment);
} else {
switch (formatHint) {
case FORMAT_SS:
Expand Down Expand Up @@ -535,9 +539,7 @@ public void onPlaybackStateChanged(int playbackState) {

@Override
public void onPlayerError(final ExoPlaybackException error) {
if (eventSink != null) {
eventSink.error("VideoError", "Video player had error " + error, null);
}
eventSink.error("VideoError", "Video player had error " + error, null);
}
});

Expand All @@ -555,13 +557,18 @@ void sendBufferingUpdate() {
eventSink.success(event);
}

@SuppressWarnings("deprecation")
private static void setAudioAttributes(SimpleExoPlayer exoPlayer) {
Player.AudioComponent audioComponent = exoPlayer.getAudioComponent();
if (audioComponent == null) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
exoPlayer.setAudioAttributes(
new AudioAttributes.Builder().setContentType(C.CONTENT_TYPE_MOVIE).build());

audioComponent.setAudioAttributes(
new AudioAttributes.Builder().setContentType(C.CONTENT_TYPE_MOVIE).build(), false);
} else {
exoPlayer.setAudioStreamType(C.STREAM_TYPE_MUSIC);
audioComponent.setAudioAttributes(
new AudioAttributes.Builder().setContentType(C.CONTENT_TYPE_MUSIC).build(), false);
}
}

Expand Down Expand Up @@ -664,30 +671,22 @@ public MediaSessionCompat setupMediaSession(Context context, boolean setupContro
}
ComponentName mediaButtonReceiver = new ComponentName(context, MediaButtonReceiver.class);
MediaSessionCompat mediaSession = new MediaSessionCompat(context, "BetterPlayer", mediaButtonReceiver, null);
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

mediaSession.setCallback(new MediaSessionCompat.Callback() {
@Override
public void onSeekTo(long pos) {
exoPlayer.seekTo(pos);
Map<String, Object> event = new HashMap<>();
event.put("event", "seek");
event.put("position", pos);
eventSink.success(event);
sendSeekToEvent(pos);
super.onSeekTo(pos);
}
});

mediaSession.setActive(true);

MediaSessionConnector mediaSessionConnector =
new MediaSessionConnector(mediaSession);
if (setupControlDispatcher) {
mediaSessionConnector.setControlDispatcher(setupControlDispatcher());
}
mediaSessionConnector.setPlayer(exoPlayer);


Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setClass(context, MediaButtonReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0);
Expand Down Expand Up @@ -767,17 +766,26 @@ void setAudioTrack(String name, Integer index) {
private void setAudioTrack(int rendererIndex, int groupIndex, int groupElementIndex) {
MappingTrackSelector.MappedTrackInfo mappedTrackInfo =
trackSelector.getCurrentMappedTrackInfo();
Log.d(TAG, "Selected audio!");
DefaultTrackSelector.ParametersBuilder builder =
trackSelector.getParameters().buildUpon();
builder.clearSelectionOverrides(rendererIndex)
.setRendererDisabled(rendererIndex, false);
int[] tracks = {groupElementIndex};
DefaultTrackSelector.SelectionOverride override =
new DefaultTrackSelector.SelectionOverride(groupIndex, tracks);
builder.setSelectionOverride(rendererIndex,
mappedTrackInfo.getTrackGroups(rendererIndex), override);
trackSelector.setParameters(builder);
if (mappedTrackInfo != null) {
DefaultTrackSelector.ParametersBuilder builder =
trackSelector.getParameters().buildUpon();
builder.clearSelectionOverrides(rendererIndex)
.setRendererDisabled(rendererIndex, false);
int[] tracks = {groupElementIndex};
DefaultTrackSelector.SelectionOverride override =
new DefaultTrackSelector.SelectionOverride(groupIndex, tracks);
builder.setSelectionOverride(rendererIndex,
mappedTrackInfo.getTrackGroups(rendererIndex), override);
trackSelector.setParameters(builder);
}
}

private void sendSeekToEvent(long positionMs) {
exoPlayer.seekTo(positionMs);
Map<String, Object> event = new HashMap<>();
event.put("event", "seek");
event.put("position", positionMs);
eventSink.success(event);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package com.jhomlala.better_player;

import android.content.Context;

import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.FileDataSource;
import com.google.android.exoplayer2.upstream.cache.CacheDataSink;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;

import java.io.File;

class CacheDataSourceFactory implements DataSource.Factory {
private final Context context;
private final DefaultDataSourceFactory defaultDatasourceFactory;

///TODO: remove maxCacheSize
private final long maxFileSize, maxCacheSize;


Expand All @@ -30,14 +23,15 @@ class CacheDataSourceFactory implements DataSource.Factory {
this.context = context;
this.maxCacheSize = maxCacheSize;
this.maxFileSize = maxFileSize;
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter.Builder(context).build();
defaultDatasourceFactory =
new DefaultDataSourceFactory(this.context, bandwidthMeter, upstreamDataSource);
}

@SuppressWarnings("NullableProblems")
@Override
public DataSource createDataSource() {
SimpleCache betterPlayerCache = BetterPlayerCache.createCache(context,maxCacheSize);
SimpleCache betterPlayerCache = BetterPlayerCache.createCache(context, maxCacheSize);
return new CacheDataSource(
betterPlayerCache,
defaultDatasourceFactory.createDataSource(),
Expand Down
2 changes: 2 additions & 0 deletions example/lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ class Constants {
"https://storage.googleapis.com/wvmedia/cenc/h264/tears/tears_sd.mpd";
static String widevineLicenseUrl =
"https://proxy.uat.widevine.com/proxy?provider=widevine_test";
static String catImageUrl =
"https://img.webmd.com/dtmcms/live/webmd/consumer_assets/site_images/article_thumbnails/other/cat_relaxing_on_patio_other/1800x1200_cat_relaxing_on_patio_other.jpg";
}
2 changes: 1 addition & 1 deletion example/lib/pages/auto_fullscreen_orientation_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class _AutoFullscreenOrientationPageState
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Normal player"),
title: Text("Auto full screen orientation"),
),
body: Column(
children: [
Expand Down
8 changes: 5 additions & 3 deletions example/lib/pages/basic_player_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ class _BasicPlayerPageState extends State<BasicPlayerPage> {
),
),
AspectRatio(
aspectRatio: 16 / 9,
child: BetterPlayer.network(
'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4')),
aspectRatio: 16 / 9,
child: BetterPlayer.network(
Constants.forBiggerBlazesUrl,
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Text(
Expand Down
2 changes: 1 addition & 1 deletion example/lib/pages/cache_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class _CachePageState extends State<CachePage> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Normal player"),
title: Text("Cache"),
),
body: Column(
children: [
Expand Down
3 changes: 2 additions & 1 deletion example/lib/pages/change_player_theme_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class _ChangePlayerThemePageState extends State<ChangePlayerThemePage> {
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Text(
"Player with the possibility to change the theme",
"Player with the possibility to change the theme. Click on "
"buttons below to change theme of player.",
style: TextStyle(fontSize: 16),
),
),
Expand Down
5 changes: 3 additions & 2 deletions example/lib/pages/controller_controls_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ class _ControllerControlsPageState extends State<ControllerControlsPage> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Controller controls page"),
title: Text("Controller controls"),
),
body: Column(
children: [
const SizedBox(height: 8),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Text(
"Control player with BetterPlayerController",
"Control player with BetterPlayerController. You can control all"
"aspects of player without using UI of player.",
style: TextStyle(fontSize: 16),
),
),
Expand Down
3 changes: 2 additions & 1 deletion example/lib/pages/controls_always_visible_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class _ControlsAlwaysVisiblePageState extends State<ControlsAlwaysVisiblePage> {
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Text(
"Controls always visible",
"Controls are always visible. Click on button below to"
" enable/disable this mode.",
style: TextStyle(fontSize: 16),
),
),
Expand Down
Loading

0 comments on commit a73b56b

Please sign in to comment.