提交 412c00dd authored 作者: dokieyang's avatar dokieyang

超级播放器添加事件回调和pause、resume、stop接口

上级 5963b9de
......@@ -10,7 +10,9 @@ import com.tencent.liteav.demo.superplayer.SuperPlayerGlobalConfig;
import com.tencent.liteav.demo.superplayer.SuperPlayerModel;
import com.tencent.liteav.demo.superplayer.SuperPlayerVideoId;
import com.tencent.liteav.demo.superplayer.SuperPlayerView;
import com.tencent.liteav.demo.superplayer.model.ISuperPlayerListener;
import com.tencent.rtmp.TXLiveBase;
import com.tencent.rtmp.TXVodPlayer;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -26,18 +28,21 @@ import io.flutter.plugin.platform.PlatformView;
import android.util.Log;
public class SuperPlatformPlayerView implements PlatformView, MethodChannel.MethodCallHandler, SuperPlayerView.OnSuperPlayerViewCallback {
public class SuperPlatformPlayerView implements PlatformView, MethodChannel.MethodCallHandler, SuperPlayerView.OnSuperPlayerViewCallback, ISuperPlayerListener {
private SuperPlayerView mSuperPlayerView;
private FlutterPlugin.FlutterPluginBinding mFlutterPluginBinding;
private final MethodChannel mMethodChannel;
private final EventChannel mEventChannel;
private final EventChannel mNetChannel;
private final FTXPlayerEventSink mEventSink = new FTXPlayerEventSink();
final private FTXPlayerEventSink mNetStatusSink = new FTXPlayerEventSink();
public SuperPlatformPlayerView(Context context, Map<String, Object> params, int viewId, FlutterPlugin.FlutterPluginBinding flutterPluginBinding) {
super();
mSuperPlayerView = new SuperPlayerView(context);
mSuperPlayerView.setPlayerViewCallback(this);
mSuperPlayerView.setSuperPlayerListener(this);
mMethodChannel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "cloud.tencent.com/superPlayer/" + viewId);
mMethodChannel.setMethodCallHandler(this);
mEventChannel = new EventChannel(flutterPluginBinding.getBinaryMessenger(), "cloud.tencent.com/superPlayer/event/" + viewId);
......@@ -90,6 +95,18 @@ public class SuperPlatformPlayerView implements PlatformView, MethodChannel.Meth
//
// mPlayerModel = model;
// mSuperPlayerView.playWithModel(model);
mNetChannel = new EventChannel(flutterPluginBinding.getBinaryMessenger(), "cloud.tencent.com/superPlayer/net/" + viewId);
mNetChannel.setStreamHandler(new EventChannel.StreamHandler() {
@Override
public void onListen(Object o, EventChannel.EventSink eventSink) {
mNetStatusSink.setEventSinkProxy(eventSink);
}
@Override
public void onCancel(Object o) {
mNetStatusSink.setEventSinkProxy(null);
}
});
}
......@@ -179,6 +196,15 @@ public class SuperPlatformPlayerView implements PlatformView, MethodChannel.Meth
} else if(call.method.equals("resetPlayer")){
destory();
result.success(null);
} else if(call.method.equals("pause")){
setPause();
result.success(null);
} else if(call.method.equals("resume")){
setResume();
result.success(null);
} else if(call.method.equals("stop")){
setStop();
result.success(null);
}else {
result.notImplemented();
}
......@@ -287,6 +313,18 @@ public class SuperPlatformPlayerView implements PlatformView, MethodChannel.Meth
mSuperPlayerView.setLoop(b);
}
public void setPause() {
mSuperPlayerView.onPause();
}
public void setResume() {
mSuperPlayerView.onResume();
}
public void setStop() {
mSuperPlayerView.resetPlayer();
}
private Map<String, Object> getParams(String event, Bundle bundle) {
Map<String, Object> param = new HashMap();
if (!event.isEmpty()) {
......@@ -311,4 +349,23 @@ public class SuperPlatformPlayerView implements PlatformView, MethodChannel.Meth
mEventChannel.setStreamHandler(null);
}
@Override
public void onVodPlayEvent(TXVodPlayer player, int event, Bundle param) {
mEventSink.success(getParams("onVodPlayEvent", param));
}
@Override
public void onVodNetStatus(TXVodPlayer player, Bundle status) {
mNetStatusSink.success(getParams("onVodNetStatus", status));
}
@Override
public void onLivePlayEvent(int event, Bundle param) {
mEventSink.success(getParams("onLivePlayEvent", param));
}
@Override
public void onLiveNetStatus(Bundle status) {
mNetStatusSink.success(getParams("onLiveNetStatus", status));
}
}
......@@ -28,6 +28,7 @@ import android.widget.RelativeLayout;
import android.widget.Toast;
import com.tencent.liteav.basic.log.TXCLog;
import com.tencent.liteav.demo.superplayer.model.ISuperPlayerListener;
import com.tencent.liteav.demo.superplayer.model.SuperPlayer;
import com.tencent.liteav.demo.superplayer.model.SuperPlayerImpl;
import com.tencent.liteav.demo.superplayer.model.SuperPlayerObserver;
......@@ -87,6 +88,7 @@ public class SuperPlayerView extends RelativeLayout {
private OnSuperPlayerViewCallback mPlayerViewCallback; // SuperPlayerView回调
private NetWatcher mWatcher; // 网络质量监视器
private SuperPlayer mSuperPlayer;
private ISuperPlayerListener mSuperPlayerListener;
public SuperPlayerView(Context context) {
super(context);
......@@ -291,6 +293,18 @@ public class SuperPlayerView extends RelativeLayout {
mPlayerViewCallback = callback;
}
/**
* 设置超级播放器中点播播放器和直播播放器的回调
*
* @param superPlayerListener
*/
public void setSuperPlayerListener(ISuperPlayerListener superPlayerListener) {
mSuperPlayerListener = superPlayerListener;
if(mSuperPlayer != null) {
mSuperPlayer.setSuperPlayerListener(mSuperPlayerListener);
}
}
/**
* 控制是否全屏显示
*/
......
package com.tencent.liteav.demo.superplayer.model;
import android.os.Bundle;
import com.tencent.rtmp.TXVodPlayer;
public interface ISuperPlayerListener {
public void onVodPlayEvent(final TXVodPlayer player, final int event, final Bundle param);
public void onVodNetStatus(final TXVodPlayer player, final Bundle status);
public void onLivePlayEvent(final int event, final Bundle param);
public void onLiveNetStatus(final Bundle status);
}
......@@ -137,6 +137,12 @@ public interface SuperPlayer {
*/
void setObserver(SuperPlayerObserver observer);
/**
* 设置超级播放器中点播事件和直播事件的回调
* @param superPlayerListener
*/
void setSuperPlayerListener(ISuperPlayerListener superPlayerListener);
/**
* 设置是否循环
* @param isLoop true循环,false不循环
......
......@@ -54,6 +54,7 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
private TXVodPlayConfig mVodPlayConfig; // 点播播放器配置
private TXLivePlayer mLivePlayer; // 直播播放器
private TXLivePlayConfig mLivePlayConfig; // 直播播放器配置
private ISuperPlayerListener mSuperPlayerListener;
private SuperPlayerModel mCurrentModel; // 当前播放的model
private SuperPlayerObserver mObserver;
......@@ -138,6 +139,9 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
default:
break;
}
if(mSuperPlayerListener != null){
mSuperPlayerListener.onLivePlayEvent(event, param);
}
}
/**
......@@ -147,7 +151,9 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
*/
@Override
public void onNetStatus(Bundle bundle) {
if(mSuperPlayerListener != null){
mSuperPlayerListener.onLiveNetStatus(bundle);
}
}
/**
......@@ -218,6 +224,9 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
updatePlayerState(SuperPlayerDef.PlayerState.PAUSE);
onError(SuperPlayerCode.VOD_PLAY_FAIL, param.getString(TXLiveConstants.EVT_DESCRIPTION));
}
if(mSuperPlayerListener != null){
mSuperPlayerListener.onVodPlayEvent(player, event, param);
}
}
/**
......@@ -228,7 +237,9 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
*/
@Override
public void onNetStatus(TXVodPlayer player, Bundle bundle) {
if(mSuperPlayerListener != null){
mSuperPlayerListener.onVodNetStatus(player, bundle);
}
}
private void initialize(Context context, TXCloudVideoView videoView) {
......@@ -909,6 +920,11 @@ public class SuperPlayerImpl implements SuperPlayer, ITXVodPlayListener, ITXLive
mObserver = observer;
}
@Override
public void setSuperPlayerListener(ISuperPlayerListener superPlayerListener) {
mSuperPlayerListener = superPlayerListener;
}
@Override
public void setLoop(boolean isLoop) {
mVodPlayer.setLoop(isLoop);
......
......@@ -19,7 +19,7 @@ class TestSuperPlayer extends StatefulWidget {
_TestSuperPlayerState createState() => _TestSuperPlayerState();
}
class _TestSuperPlayerState extends State<TestSuperPlayer> {
class _TestSuperPlayerState extends State<TestSuperPlayer> with WidgetsBindingObserver{
bool _liveSelected = true;//live is default
var _currentModels = <SuperPlayerViewModel>[];
SuperPlayerViewConfig _playerConfig = SuperPlayerViewConfig();
......@@ -41,13 +41,33 @@ class _TestSuperPlayerState extends State<TestSuperPlayer> {
void initState() {
super.initState();
_getLiveListData();
WidgetsBinding.instance?.addObserver(this);
debugPrint("= initState = ${window.padding.top}, ${window.physicalSize.width}");
}
@override
Future didChangeAppLifecycleState(AppLifecycleState state) async {
super.didChangeAppLifecycleState(state);
print("didChangeAppLifecycleState $state");
switch (state) {
case AppLifecycleState.inactive:
break;
case AppLifecycleState.resumed:
_playerController.resume();
break;
case AppLifecycleState.paused:
_playerController.pause();
break;
default:
break;
}
}
@override
void dispose() {
_playerController.resetPlayer();
super.dispose();
WidgetsBinding.instance?.removeObserver(this);
}
void onPressed(BuildContext context) {
......@@ -147,6 +167,8 @@ class _TestSuperPlayerState extends State<TestSuperPlayer> {
print("onSuperPlayerBackAction");
} else {
print(evtName);
//final Map<dynamic, dynamic> map = event;
//print(map);
}
}
);
......
......@@ -9,7 +9,7 @@ class TestTXLivePlayer extends StatefulWidget {
_TestTXPLivelayerState createState() => _TestTXPLivelayerState();
}
class _TestTXPLivelayerState extends State<TestTXLivePlayer> {
class _TestTXPLivelayerState extends State<TestTXLivePlayer> with WidgetsBindingObserver{
TXLivePlayerController _controller;
double _aspectRatio = 0;
......@@ -70,9 +70,28 @@ class _TestTXPLivelayerState extends State<TestTXLivePlayer> {
void initState() {
super.initState();
init();
WidgetsBinding.instance?.addObserver(this);
EasyLoading.show(status: 'loading...');
}
@override
Future didChangeAppLifecycleState(AppLifecycleState state) async {
super.didChangeAppLifecycleState(state);
print("didChangeAppLifecycleState $state");
switch (state) {
case AppLifecycleState.inactive:
break;
case AppLifecycleState.resumed:
_controller.resume();
break;
case AppLifecycleState.paused:
_controller.pause();
break;
default:
break;
}
}
@override
Widget build(BuildContext context) {
return Container(
......@@ -227,6 +246,7 @@ class _TestTXPLivelayerState extends State<TestTXLivePlayer> {
void dispose() {
_controller.dispose();
super.dispose();
WidgetsBinding.instance?.removeObserver(this);
EasyLoading.dismiss();
}
......
......@@ -14,7 +14,7 @@ class TestTXVodPlayer extends StatefulWidget {
_TestTXPVodlayerState createState() => _TestTXPVodlayerState();
}
class _TestTXPVodlayerState extends State<TestTXVodPlayer> {
class _TestTXPVodlayerState extends State<TestTXVodPlayer> with WidgetsBindingObserver{
TXVodPlayerController _controller;
double _aspectRatio = 0;
......@@ -72,9 +72,28 @@ class _TestTXPVodlayerState extends State<TestTXVodPlayer> {
void initState() {
super.initState();
init();
WidgetsBinding.instance?.addObserver(this);
EasyLoading.show(status: 'loading...');
}
@override
Future didChangeAppLifecycleState(AppLifecycleState state) async {
super.didChangeAppLifecycleState(state);
print("didChangeAppLifecycleState $state");
switch (state) {
case AppLifecycleState.inactive:
break;
case AppLifecycleState.resumed:
_controller.resume();
break;
case AppLifecycleState.paused:
_controller.pause();
break;
default:
break;
}
}
@override
Widget build(BuildContext context) {
return Container(
......@@ -269,6 +288,7 @@ class _TestTXPVodlayerState extends State<TestTXVodPlayer> {
void dispose() {
_controller.dispose();
super.dispose();
WidgetsBinding.instance?.removeObserver(this);
EasyLoading.dismiss();
}
......
......@@ -61,5 +61,17 @@ class SuperPlayerPlatformViewController {
Future<void> resetPlayer() async {
await _channel.invokeMethod("resetPlayer");
}
Future<void> pause() async {
await _channel.invokeMethod("pause");
}
Future<void> resume() async {
await _channel.invokeMethod("resume");
}
Future<void> stop() async {
await _channel.invokeMethod("stop");
}
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论