Skip to content

Commit

Permalink
Feature/march changes 3 (jhomlala#393)
Browse files Browse the repository at this point in the history
* Fixed pause method in dispose.

* Added clear cache method

* Clear cache ios implementation

* Fixed reusable video player example issues

* Format, 0.0.63 release
  • Loading branch information
jhomlala authored Mar 20, 2021
1 parent 6a00029 commit 7f9f924
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.0.63
* Fixed pause method in dispose.
* Added clearCache method in BetterPlayerController.
* Fixed reusable video player example issues.

## 0.0.62
* Refactored internal event handling.
* [BREAKING_CHANGE] Migrated to null safety.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ This plugin is based on [Chewie](https://github.com/brianegan/chewie). Chewie is

```yaml
dependencies:
better_player: ^0.0.62
better_player: ^0.0.63
```
2. Install it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.view.TextureRegistry;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
Expand Down Expand Up @@ -789,9 +790,21 @@ private void sendSeekToEvent(long positionMs) {
}

public void setMixWithOthers(Boolean mixWithOthers) {
setAudioAttributes(exoPlayer,mixWithOthers);
setAudioAttributes(exoPlayer, mixWithOthers);
}

//Clear cache without accessing BetterPlayerCache.
@SuppressWarnings("ResultOfMethodCallIgnored")
public void clearCache(Context context) {
try {
File file = context.getCacheDir();
if (file != null) {
file.delete();
}
} catch (Exception exception) {
Log.e("Cache", exception.toString());
}
}

void dispose() {
disposeMediaSession();
Expand Down Expand Up @@ -827,6 +840,7 @@ public int hashCode() {
result = 31 * result + (surface != null ? surface.hashCode() : 0);
return result;
}

}


Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jhomlala.better_player;

import android.content.Context;
import android.util.Log;

import com.google.android.exoplayer2.database.ExoDatabaseProvider;
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor;
Expand All @@ -25,15 +26,14 @@ public static SimpleCache createCache(Context context, long cacheFileSize) {
return instance;
}


public static void releaseCache() {
try {
if (instance != null) {
instance.release();
instance = null;
}
} catch (Exception exception) {

Log.e("BetterPlayerCache", exception.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public class BetterPlayerPlugin implements FlutterPlugin, ActivityAware, MethodC
private static final String DISABLE_PICTURE_IN_PICTURE_METHOD = "disablePictureInPicture";
private static final String IS_PICTURE_IN_PICTURE_SUPPORTED_METHOD = "isPictureInPictureSupported";
private static final String SET_MIX_WITH_OTHERS_METHOD = "setMixWithOthers";
private static final String CLEAR_CACHE_METHOD = "clearCache";
private static final String DISPOSE_METHOD = "dispose";

private final LongSparseArray<BetterPlayer> videoPlayers = new LongSparseArray<>();
Expand Down Expand Up @@ -276,6 +277,9 @@ private void onMethodCall(MethodCall call, Result result, long textureId, Better
case SET_MIX_WITH_OTHERS_METHOD:
player.setMixWithOthers(call.argument(MIX_WITH_OTHERS_PARAMETER));
break;
case CLEAR_CACHE_METHOD:
player.clearCache(flutterState.applicationContext);
break;
case DISPOSE_METHOD:
dispose(player, textureId);
result.success(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.jhomlala.better_player;

import android.content.Context;
import android.util.Log;

import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
Expand All @@ -8,6 +11,8 @@
import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
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;
Expand Down
6 changes: 6 additions & 0 deletions example/lib/pages/cache_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class _CachePageState extends State<CachePage> {
aspectRatio: 16 / 9,
child: BetterPlayer(controller: _betterPlayerController),
),
TextButton(
child: Text("Clear cache"),
onPressed: () {
_betterPlayerController.clearCache();
},
)
],
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ class _ReusableVideoListWidgetState extends State<ReusableVideoListWidget> {
void _setupController() {
if (controller == null) {
controller = widget.videoListController!.getBetterPlayerController();
controller!.setupDataSource(BetterPlayerDataSource.network(
videoListData!.videoUrl,
cacheConfiguration: BetterPlayerCacheConfiguration(useCache: true)));
betterPlayerControllerStreamController.add(controller);
controller!.addEventsListener(onPlayerEvent);
if (controller != null) {
controller!.setupDataSource(BetterPlayerDataSource.network(
videoListData!.videoUrl,
cacheConfiguration:
BetterPlayerCacheConfiguration(useCache: true)));
if (!betterPlayerControllerStreamController.isClosed) {
betterPlayerControllerStreamController.add(controller);
}
controller!.addEventsListener(onPlayerEvent);
}
}
}

Expand All @@ -63,7 +68,9 @@ class _ReusableVideoListWidgetState extends State<ReusableVideoListWidget> {
widget.videoListController!.freeBetterPlayerController(controller);
controller!.pause();
controller = null;
betterPlayerControllerStreamController.add(null);
if (!betterPlayerControllerStreamController.isClosed) {
betterPlayerControllerStreamController.add(null);
}
_initialized = false;
}
}
Expand Down Expand Up @@ -186,7 +193,6 @@ class _ReusableVideoListWidgetState extends State<ReusableVideoListWidget> {
videoListData!.wasPlaying = controller!.isPlaying();
}
_initialized = true;
_freeController();
super.deactivate();
}
}
6 changes: 3 additions & 3 deletions ios/Classes/FLTBetterPlayerPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -1228,9 +1228,9 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
[player setAudioTrack:name index: index];
} else if ([@"setMixWithOthers" isEqualToString:call.method]){
[player setMixWithOthers:[argsMap[@"mixWithOthers"] boolValue]];
}

else {
} else if ([@"clearCache" isEqualToString:call.method]){
[KTVHTTPCache cacheDeleteAllCaches];
} else {
result(FlutterMethodNotImplemented);
}
}
Expand Down
19 changes: 15 additions & 4 deletions lib/src/core/better_player_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,15 @@ class BetterPlayerController {
videoPlayerController!.setMixWithOthers(mixWithOthers);
}

///Clear all cached data. Video player controller must be initialized to
///clear the cache.
void clearCache() {
if (videoPlayerController == null) {
throw StateError("The data source has not been initialized");
}
videoPlayerController!.clearCache();
}

///Build headers map that will be used to setup video player controller. Apply
///DRM headers if available.
Map<String, String?> _getHeaders() {
Expand All @@ -1066,11 +1075,13 @@ class BetterPlayerController {
return;
}
if (!_disposed) {
pause();
if (videoPlayerController != null) {
pause();
videoPlayerController!.removeListener(_onFullScreenStateChanged);
videoPlayerController!.removeListener(_onVideoPlayerChanged);
videoPlayerController!.dispose();
}
_eventListeners.clear();
videoPlayerController?.removeListener(_onFullScreenStateChanged);
videoPlayerController?.removeListener(_onVideoPlayerChanged);
videoPlayerController?.dispose();
_nextVideoTimer?.cancel();
nextVideoTimeStreamController.close();
_controlsVisibilityStreamController.close();
Expand Down
10 changes: 10 additions & 0 deletions lib/src/video_player/method_channel_video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,16 @@ class MethodChannelVideoPlayer extends VideoPlayerPlatform {
);
}

@override
Future<void> clearCache(int? textureId) {
return _channel.invokeMethod<void>(
'clearCache',
<String, dynamic>{
'textureId': textureId,
},
);
}

@override
Stream<VideoEvent> videoEventsFor(int? textureId) {
return _eventChannelFor(textureId)
Expand Down
4 changes: 4 additions & 0 deletions lib/src/video_player/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,10 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
void setMixWithOthers(bool mixWithOthers) {
_videoPlayerPlatform.setMixWithOthers(_textureId, mixWithOthers);
}

void clearCache() {
_videoPlayerPlatform.clearCache(_textureId);
}
}

/// Widget that displays the video controlled by [controller].
Expand Down
4 changes: 4 additions & 0 deletions lib/src/video_player/video_player_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ abstract class VideoPlayerPlatform {
throw UnimplementedError('setMixWithOthers() has not been implemented.');
}

Future<void> clearCache(int? textureId) {
throw UnimplementedError('clearCache() has not been implemented.');
}

/// Returns a widget displaying the video with a given textureID.
Widget buildView(int? textureId) {
throw UnimplementedError('buildView() has not been implemented.');
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: better_player
description: Advanced video player based on video_player and Chewie. It's solves many typical use cases and it's easy to run.
version: 0.0.62
version: 0.0.63
authors:
- Jakub Homlala <jhomlala@gmail.com>
homepage: https://github.com/jhomlala/betterplayer
Expand Down

0 comments on commit 7f9f924

Please sign in to comment.