Skip to content
This repository has been archived by the owner on Feb 14, 2022. It is now read-only.

Commit

Permalink
单独抽离videl播放器
Browse files Browse the repository at this point in the history
  • Loading branch information
januwA committed Jun 21, 2019
1 parent 23af22e commit eb8aac5
Show file tree
Hide file tree
Showing 6 changed files with 629 additions and 538 deletions.
174 changes: 8 additions & 166 deletions lib/pages/detail/detail.store.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import 'dart:convert';
import 'dart:io';

import 'package:flutter/services.dart';
import 'package:flutter_video_app/models/detail_data_dto/detail_data_dto.dart';
import 'package:flutter_video_app/shared/globals.dart';
import 'package:mobx/mobx.dart';
import 'package:validators/validators.dart';
import 'package:video_player/video_player.dart';
import 'package:http/http.dart' as http;

part 'detail.store.g.dart';
Expand All @@ -29,9 +26,6 @@ abstract class _DetailStore with Store {
getDetailData();
}

@observable
VideoPlayerController videoCtrl;

@observable
String weekDataerror = '';

Expand All @@ -45,76 +39,17 @@ abstract class _DetailStore with Store {
@observable
int currentPlayIndex = 0;

/// 当前播放的视频
@computed
PlayUrlTab get currentPlayVideo => detailData.playUrlTab[currentPlayIndex];

@observable
bool isPageLoading = true;

@observable
bool isVideoLoading = true;

/// 当前anime播放位置
@observable
Duration position;

/// anime总时长
@observable
Duration duration;
String src = '';

/// 是否显示控制器
@observable
bool isShowVideoCtrl = true;

/// 是否为全屏播放
@observable
bool isFullScreen = false;

/// 25:00 or 2:00:00 总时长
@computed
String get durationText {
return duration == null
? ''
: duration
.toString()
.split('.')
.first
.split(':')
.where((String e) => e != '0')
.toList()
.join(':');
}

/// 00:01 当前时间
@computed
String get positionText {
return (videoCtrl == null)
? ''
: position
.toString()
.split('.')
.first
.split(':')
.where((String e) => e != '0')
.toList()
.join(':');
}

@computed
double get sliderValue {
if (position?.inSeconds != null && duration?.inSeconds != null) {
return position.inSeconds / duration.inSeconds;
} else {
return 0.0;
}
}
bool isPageLoading = true;

var client = http.Client();

@action
void videoListenner() {
position = videoCtrl.value.position;
void setSrc(String s) {
src = s;
}

/// 获取detail数据
Expand All @@ -127,8 +62,9 @@ abstract class _DetailStore with Store {
if (r.statusCode == HttpStatus.ok) {
var body = DetailDataDto.fromJson(r.body);
detailData = body.detailData;
src = detailData.playUrlTab.first.src;
isPageLoading = false;
initVideoPlaer();
// initVideoPlaer();
} else {
weekDataerror = r.body.toString();
isPageLoading = false;
Expand All @@ -138,124 +74,30 @@ abstract class _DetailStore with Store {
}
}

/// 初始化viedo控制器
@action
Future<void> initVideoPlaer([String src]) async {
isVideoLoading = true;
videoCtrl = VideoPlayerController.network(
!isNull(src) ? src : currentPlayVideo.src,
);

await videoCtrl.initialize();

videoCtrl.setVolume(1.0);
position = videoCtrl.value.position;
duration = videoCtrl.value.duration;
isVideoLoading = false;

// 用户点击了切换,加载完后自动播放
if (!isNull(src)) {
videoCtrl.play();
isShowVideoCtrl = false;
}
videoCtrl.addListener(videoListenner);
}

/// 获取指定集的src
@action
Future<void> getVideoSrc(Function onErrorCb) async {
var e = detailData.playUrlTab[currentPlayIndex];
isVideoLoading = true;
src = '';
var url = Uri.http(baseUrl, videoSrcUrl, {"id": e.id});
var r = await http.get(url);
if (r.statusCode == 200) {
var src = jsonDecode(r.body)['src'];
initVideoPlaer(src);
src = jsonDecode(r.body)['src'];
} else {
// error
videoSrcerror = r.body.toString();
onErrorCb();
}
}

@action
void showVideoCtrl(bool show) {
isShowVideoCtrl = show;
}

void setVolume() {
if (videoCtrl.value.volume > 0) {
videoCtrl.setVolume(0.0);
} else {
videoCtrl.setVolume(1.0);
}
}

void seekTo(double v) {
videoCtrl.seekTo(Duration(seconds: (v * duration.inSeconds).toInt()));
}

@action
void togglePlay() {
if (videoCtrl.value.isPlaying) {
videoCtrl.pause();
isShowVideoCtrl = true;
} else {
videoCtrl.play();
isShowVideoCtrl = false;
}
}

@action
void setCurrentPlayIndex(int i) {
currentPlayIndex = i;
}

/// 全屏播放切换事件
onFullScreen() {
if (isFullScreen) {
setPortrait();
} else {
setLandscape();
}
}

@action
void setIsFullScreen(bool full) {
isFullScreen = full;
}

/// 设置为横屏模式
@action
setLandscape() {
isFullScreen = true;
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
]);
}

/// 设置为正常模式
@action
setPortrait() {
isFullScreen = false;
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}

@override
void dispose() {
videoCtrl?.removeListener(videoListenner);
videoCtrl?.pause();
videoCtrl?.dispose();
client?.close();
if (isFullScreen) {
setPortrait();
}
super.dispose();
}
}
Loading

0 comments on commit eb8aac5

Please sign in to comment.