提交 b3643cbf authored 作者: kongdywang's avatar kongdywang

Fixing the issue of event disorder in multiple lists of short videos & remove third depend eventBus

上级 a730dbfe
...@@ -15,6 +15,7 @@ class _DemoShortVideoPlayerState extends State<DemoShortVideoPlayer> with Widget ...@@ -15,6 +15,7 @@ class _DemoShortVideoPlayerState extends State<DemoShortVideoPlayer> with Widget
static const TAG = "ShortVideo::ShortVideoListViewState"; static const TAG = "ShortVideo::ShortVideoListViewState";
int _currentIndex = 0; int _currentIndex = 0;
List<SuperPlayerModel> superPlayerModelList = []; List<SuperPlayerModel> superPlayerModelList = [];
VideoEventDispatcher eventDispatcher = VideoEventDispatcher();
@override @override
void initState() { void initState() {
...@@ -33,7 +34,11 @@ class _DemoShortVideoPlayerState extends State<DemoShortVideoPlayer> with Widget ...@@ -33,7 +34,11 @@ class _DemoShortVideoPlayerState extends State<DemoShortVideoPlayer> with Widget
List<Widget> widgetList = []; List<Widget> widgetList = [];
for (int i = 0; i < superPlayerModelList.length; i++) { for (int i = 0; i < superPlayerModelList.length; i++) {
widgetList.add(ShortVideoPageWidget( widgetList.add(ShortVideoPageWidget(
position: i, videoUrl: superPlayerModelList[i].videoURL, coverUrl: superPlayerModelList[i].coverUrl)); position: i,
videoUrl: superPlayerModelList[i].videoURL,
coverUrl: superPlayerModelList[i].coverUrl,
eventDispatcher: eventDispatcher,
));
} }
return Stack( return Stack(
...@@ -48,15 +53,15 @@ class _DemoShortVideoPlayerState extends State<DemoShortVideoPlayer> with Widget ...@@ -48,15 +53,15 @@ class _DemoShortVideoPlayerState extends State<DemoShortVideoPlayer> with Widget
), ),
SafeArea( SafeArea(
child: Container( child: Container(
child: InkWell( child: InkWell(
onTap: _onBackTap, onTap: _onBackTap,
child: const Image( child: const Image(
width: 40, width: 40,
height: 40, height: 40,
image: AssetImage("images/superplayer_btn_back_play.png", package: StringResource.PKG_NAME), image: AssetImage("images/superplayer_btn_back_play.png", package: StringResource.PKG_NAME),
), ),
), ),
)), )),
], ],
); );
} }
...@@ -81,20 +86,21 @@ class _DemoShortVideoPlayerState extends State<DemoShortVideoPlayer> with Widget ...@@ -81,20 +86,21 @@ class _DemoShortVideoPlayerState extends State<DemoShortVideoPlayer> with Widget
} }
_stopAndPlay(int index) async { _stopAndPlay(int index) async {
EventBusUtils.getInstance().fire(new StopAndResumeEvent(index)); eventDispatcher.notifyEvent(ShortVideoEvent(index, BaseEvent.PLAY_AND_STOP));
_currentIndex = index; _currentIndex = index;
} }
_onApplicationPause() { _onApplicationPause() {
EventBusUtils.getInstance().fire(new ApplicationPauseEvent()); eventDispatcher.notifyEvent(ShortVideoEvent(_currentIndex, BaseEvent.PAUSE));
} }
_onApplicationResume() { _onApplicationResume() {
EventBusUtils.getInstance().fire(new ApplicationResumeEvent()); eventDispatcher.notifyEvent(ShortVideoEvent(_currentIndex, BaseEvent.RESUME));
} }
@override @override
void dispose() { void dispose() {
eventDispatcher.closeStream();
WidgetsBinding.instance.removeObserver(this); WidgetsBinding.instance.removeObserver(this);
super.dispose(); super.dispose();
} }
......
...@@ -132,6 +132,5 @@ class _MyAppState extends State<MyApp> { ...@@ -132,6 +132,5 @@ class _MyAppState extends State<MyApp> {
@override @override
void dispose() { void dispose() {
super.dispose(); super.dispose();
EventBusUtils.getInstance().destroy();
} }
} }
...@@ -8,8 +8,8 @@ import 'package:flutter/material.dart'; ...@@ -8,8 +8,8 @@ import 'package:flutter/material.dart';
import 'package:super_player/super_player.dart'; import 'package:super_player/super_player.dart';
import 'package:superplayer_widget/demo_superplayer_lib.dart'; import 'package:superplayer_widget/demo_superplayer_lib.dart';
import 'package:super_player_example/ui/demo_video_slider_view.dart'; import 'package:super_player_example/ui/demo_video_slider_view.dart';
import 'package:event_bus/event_bus.dart';
part 'model/short_video_data_loader.dart'; part 'model/short_video_data_loader.dart';
part 'short_video_page_widget.dart'; part 'short_video_page_widget.dart';
part 'tools/event_bus_utils.dart'; part 'event/short_video_event.dart';
\ No newline at end of file part 'event/video_event_dispatcher.dart';
\ No newline at end of file
part of demo_short_video_player_lib;
abstract class BaseEvent {
static const PLAY_AND_STOP = 1;
static const PAUSE = 2;
static const RESUME = 3;
}
class ShortVideoEvent extends BaseEvent {
int playerIndex;
int eventType;
ShortVideoEvent(this.playerIndex, this.eventType);
}
\ No newline at end of file
part of demo_short_video_player_lib;
class VideoEventDispatcher {
StreamController<ShortVideoEvent> playerStreamController = StreamController.broadcast();
Stream<ShortVideoEvent> getEventStream() {
return playerStreamController.stream;
}
void notifyEvent(ShortVideoEvent event) {
playerStreamController.sink.add(event);
}
void closeStream() {
playerStreamController.close();
}
}
...@@ -5,8 +5,9 @@ class ShortVideoPageWidget extends StatefulWidget { ...@@ -5,8 +5,9 @@ class ShortVideoPageWidget extends StatefulWidget {
String videoUrl; String videoUrl;
String coverUrl; String coverUrl;
int position; int position;
VideoEventDispatcher eventDispatcher;
ShortVideoPageWidget({required this.position, required this.videoUrl, required this.coverUrl}); ShortVideoPageWidget({required this.position, required this.videoUrl, required this.coverUrl, required this.eventDispatcher});
@override @override
State<StatefulWidget> createState() { State<StatefulWidget> createState() {
...@@ -20,12 +21,7 @@ class _TXVodPlayerPageState extends State<ShortVideoPageWidget> { ...@@ -20,12 +21,7 @@ class _TXVodPlayerPageState extends State<ShortVideoPageWidget> {
bool _isVideoPrepared = false; bool _isVideoPrepared = false;
bool _isVideoPlaying = true; bool _isVideoPlaying = true;
GlobalKey<VideoSliderViewState> _progressSliderKey = GlobalKey(); GlobalKey<VideoSliderViewState> _progressSliderKey = GlobalKey();
late StreamSubscription _streamSubscriptionStopAndPlay; late StreamSubscription _eventSubscription;
late StreamSubscription _streamSubscriptionApplicationResume;
late StreamSubscription _streamSubscriptionApplicationPause;
StreamSubscription? _playEventSubscription; StreamSubscription? _playEventSubscription;
TXVodPlayerController _controller; TXVodPlayerController _controller;
...@@ -64,9 +60,7 @@ class _TXVodPlayerPageState extends State<ShortVideoPageWidget> { ...@@ -64,9 +60,7 @@ class _TXVodPlayerPageState extends State<ShortVideoPageWidget> {
} }
_dispose() async { _dispose() async {
_streamSubscriptionStopAndPlay.cancel(); _eventSubscription.cancel();
_streamSubscriptionApplicationResume.cancel();
_streamSubscriptionApplicationPause.cancel();
_playEventSubscription?.cancel(); _playEventSubscription?.cancel();
await _stop(); await _stop();
_controller.dispose(); _controller.dispose();
...@@ -158,10 +152,10 @@ class _TXVodPlayerPageState extends State<ShortVideoPageWidget> { ...@@ -158,10 +152,10 @@ class _TXVodPlayerPageState extends State<ShortVideoPageWidget> {
}); });
} }
_stopLastAndPlayCurrent(StopAndResumeEvent event) { _stopLastAndPlayCurrent(int index) {
LogUtils.i(TAG, LogUtils.i(TAG,
" [received at not current outside] ${widget.position.toString()} ${this.hashCode.toString()} ${widget.hashCode.toString()} ${_controller.hashCode.toString()}"); " [received at not current outside] ${widget.position.toString()} ${this.hashCode.toString()} ${widget.hashCode.toString()} ${_controller.hashCode.toString()}");
event.index != widget.position ? _stop() : _startPlay(); index != widget.position ? _stop() : _startPlay();
} }
Future<void> _stop() async { Future<void> _stop() async {
...@@ -190,16 +184,16 @@ class _TXVodPlayerPageState extends State<ShortVideoPageWidget> { ...@@ -190,16 +184,16 @@ class _TXVodPlayerPageState extends State<ShortVideoPageWidget> {
} }
_setEventBusListener() { _setEventBusListener() {
_streamSubscriptionStopAndPlay = EventBusUtils.getInstance().on<StopAndResumeEvent>().listen((event) { _eventSubscription = widget.eventDispatcher.getEventStream().listen((event) {
_stopLastAndPlayCurrent(event); if (event.eventType == BaseEvent.PLAY_AND_STOP) {
}); _stopLastAndPlayCurrent(event.playerIndex);
} else if (event.eventType == BaseEvent.PAUSE) {
_streamSubscriptionApplicationResume = EventBusUtils.getInstance().on<ApplicationResumeEvent>().listen((event) { _pause();
_resume(); } else if(event.eventType == BaseEvent.RESUME) {
}); _resume();
} else {
_streamSubscriptionApplicationPause = EventBusUtils.getInstance().on<ApplicationPauseEvent>().listen((event) { LogUtils.e(TAG, "receive unknown eventType${event.eventType}");
_pause(); }
}); });
} }
......
// Copyright (c) 2022 Tencent. All rights reserved.
part of demo_short_video_player_lib;
class EventBusUtils {
static final EventBus _instance = EventBus();
static EventBus getInstance() {
return _instance;
}
}
class ApplicationPauseEvent{
ApplicationPauseEvent();
}
class StopAndResumeEvent{
int index;
StopAndResumeEvent(this.index);
}
class ApplicationResumeEvent{
ApplicationResumeEvent();
}
\ No newline at end of file
...@@ -15,7 +15,6 @@ dependencies: ...@@ -15,7 +15,6 @@ dependencies:
sdk: flutter sdk: flutter
flutter: flutter:
sdk: flutter sdk: flutter
event_bus: ^2.0.0
flutter_easyloading: ^3.0.0 flutter_easyloading: ^3.0.0
......
...@@ -441,25 +441,29 @@ static const int CODE_ON_RECEIVE_FIRST_FRAME = 2003; ...@@ -441,25 +441,29 @@ static const int CODE_ON_RECEIVE_FIRST_FRAME = 2003;
*/ */
- (BOOL)onPlayerPixelBuffer:(CVPixelBufferRef)pixelBuffer - (BOOL)onPlayerPixelBuffer:(CVPixelBufferRef)pixelBuffer
{ {
if (_lastBuffer == nil) { if(pixelBuffer) {
_lastBuffer = CVPixelBufferRetain(pixelBuffer); if (_lastBuffer == nil) {
CFRetain(pixelBuffer); _lastBuffer = CVPixelBufferRetain(pixelBuffer);
} else if (_lastBuffer != pixelBuffer) { CFRetain(pixelBuffer);
CVPixelBufferRelease(_lastBuffer); } else if (_lastBuffer != pixelBuffer) {
_lastBuffer = CVPixelBufferRetain(pixelBuffer); CVPixelBufferRelease(_lastBuffer);
CFRetain(pixelBuffer); _lastBuffer = CVPixelBufferRetain(pixelBuffer);
} CFRetain(pixelBuffer);
}
CVPixelBufferRef newBuffer = pixelBuffer;
CVPixelBufferRef old = _latestPixelBuffer; CVPixelBufferRef newBuffer = pixelBuffer;
while (!OSAtomicCompareAndSwapPtrBarrier(old, newBuffer,
(void **)&_latestPixelBuffer)) {
old = _latestPixelBuffer;
}
if (old && old != pixelBuffer) { CVPixelBufferRef old = _latestPixelBuffer;
CFRelease(old); while (!OSAtomicCompareAndSwapPtrBarrier(old, newBuffer,
(void **)&_latestPixelBuffer)) {
old = _latestPixelBuffer;
}
if (old && old != pixelBuffer) {
CFRelease(old);
}
} else {
NSLog(@"receive a nil pixel");
} }
if (_textureId >= 0 && nil != _textureRegistry) { if (_textureId >= 0 && nil != _textureRegistry) {
[_textureRegistry textureFrameAvailable:_textureId]; [_textureRegistry textureFrameAvailable:_textureId];
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论