提交 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
static const TAG = "ShortVideo::ShortVideoListViewState";
int _currentIndex = 0;
List<SuperPlayerModel> superPlayerModelList = [];
VideoEventDispatcher eventDispatcher = VideoEventDispatcher();
@override
void initState() {
......@@ -33,7 +34,11 @@ class _DemoShortVideoPlayerState extends State<DemoShortVideoPlayer> with Widget
List<Widget> widgetList = [];
for (int i = 0; i < superPlayerModelList.length; i++) {
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(
......@@ -81,20 +86,21 @@ class _DemoShortVideoPlayerState extends State<DemoShortVideoPlayer> with Widget
}
_stopAndPlay(int index) async {
EventBusUtils.getInstance().fire(new StopAndResumeEvent(index));
eventDispatcher.notifyEvent(ShortVideoEvent(index, BaseEvent.PLAY_AND_STOP));
_currentIndex = index;
}
_onApplicationPause() {
EventBusUtils.getInstance().fire(new ApplicationPauseEvent());
eventDispatcher.notifyEvent(ShortVideoEvent(_currentIndex, BaseEvent.PAUSE));
}
_onApplicationResume() {
EventBusUtils.getInstance().fire(new ApplicationResumeEvent());
eventDispatcher.notifyEvent(ShortVideoEvent(_currentIndex, BaseEvent.RESUME));
}
@override
void dispose() {
eventDispatcher.closeStream();
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
......
......@@ -132,6 +132,5 @@ class _MyAppState extends State<MyApp> {
@override
void dispose() {
super.dispose();
EventBusUtils.getInstance().destroy();
}
}
......@@ -8,8 +8,8 @@ import 'package:flutter/material.dart';
import 'package:super_player/super_player.dart';
import 'package:superplayer_widget/demo_superplayer_lib.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 'short_video_page_widget.dart';
part 'tools/event_bus_utils.dart';
\ No newline at end of file
part 'event/short_video_event.dart';
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 {
String videoUrl;
String coverUrl;
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
State<StatefulWidget> createState() {
......@@ -20,12 +21,7 @@ class _TXVodPlayerPageState extends State<ShortVideoPageWidget> {
bool _isVideoPrepared = false;
bool _isVideoPlaying = true;
GlobalKey<VideoSliderViewState> _progressSliderKey = GlobalKey();
late StreamSubscription _streamSubscriptionStopAndPlay;
late StreamSubscription _streamSubscriptionApplicationResume;
late StreamSubscription _streamSubscriptionApplicationPause;
late StreamSubscription _eventSubscription;
StreamSubscription? _playEventSubscription;
TXVodPlayerController _controller;
......@@ -64,9 +60,7 @@ class _TXVodPlayerPageState extends State<ShortVideoPageWidget> {
}
_dispose() async {
_streamSubscriptionStopAndPlay.cancel();
_streamSubscriptionApplicationResume.cancel();
_streamSubscriptionApplicationPause.cancel();
_eventSubscription.cancel();
_playEventSubscription?.cancel();
await _stop();
_controller.dispose();
......@@ -158,10 +152,10 @@ class _TXVodPlayerPageState extends State<ShortVideoPageWidget> {
});
}
_stopLastAndPlayCurrent(StopAndResumeEvent event) {
_stopLastAndPlayCurrent(int index) {
LogUtils.i(TAG,
" [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 {
......@@ -190,16 +184,16 @@ class _TXVodPlayerPageState extends State<ShortVideoPageWidget> {
}
_setEventBusListener() {
_streamSubscriptionStopAndPlay = EventBusUtils.getInstance().on<StopAndResumeEvent>().listen((event) {
_stopLastAndPlayCurrent(event);
});
_streamSubscriptionApplicationResume = EventBusUtils.getInstance().on<ApplicationResumeEvent>().listen((event) {
_resume();
});
_streamSubscriptionApplicationPause = EventBusUtils.getInstance().on<ApplicationPauseEvent>().listen((event) {
_eventSubscription = widget.eventDispatcher.getEventStream().listen((event) {
if (event.eventType == BaseEvent.PLAY_AND_STOP) {
_stopLastAndPlayCurrent(event.playerIndex);
} else if (event.eventType == BaseEvent.PAUSE) {
_pause();
} else if(event.eventType == BaseEvent.RESUME) {
_resume();
} else {
LogUtils.e(TAG, "receive unknown eventType${event.eventType}");
}
});
}
......
// 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:
sdk: flutter
flutter:
sdk: flutter
event_bus: ^2.0.0
flutter_easyloading: ^3.0.0
......
......@@ -441,6 +441,7 @@ static const int CODE_ON_RECEIVE_FIRST_FRAME = 2003;
*/
- (BOOL)onPlayerPixelBuffer:(CVPixelBufferRef)pixelBuffer
{
if(pixelBuffer) {
if (_lastBuffer == nil) {
_lastBuffer = CVPixelBufferRetain(pixelBuffer);
CFRetain(pixelBuffer);
......@@ -461,6 +462,9 @@ static const int CODE_ON_RECEIVE_FIRST_FRAME = 2003;
if (old && old != pixelBuffer) {
CFRelease(old);
}
} else {
NSLog(@"receive a nil pixel");
}
if (_textureId >= 0 && nil != _textureRegistry) {
[_textureRegistry textureFrameAvailable:_textureId];
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论