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

SuperPlayer supports screen autorotation for Android&iOS platform

上级 5f170bba
...@@ -58,4 +58,17 @@ public class FTXEvent { ...@@ -58,4 +58,17 @@ public class FTXEvent {
// pip 错误,当前界面已销毁 // pip 错误,当前界面已销毁
public static final int ERROR_PIP_ACTIVITY_DESTROYED = -103; public static final int ERROR_PIP_ACTIVITY_DESTROYED = -103;
// 屏幕旋转事件
public static final int EVENT_ORIENTATION_CHANGED = 401;
// 屏幕旋转方向
public static final String EXTRA_NAME_ORIENTATION = "orientation";
// 正竖屏
public static final int ORIENTATION_PORTRAIT_UP = 411;
// 横屏,底部在右
public static final int ORIENTATION_LANDSCAPE_RIGHT = 412;
// 竖屏,顶部在下
public static final int ORIENTATION_PORTRAIT_DOWN = 413;
// 横屏,底部在左
public static final int ORIENTATION_LANDSCAPE_LEFT = 414;
} }
...@@ -7,8 +7,10 @@ import android.content.Intent; ...@@ -7,8 +7,10 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.media.AudioManager; import android.media.AudioManager;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.SparseArray; import android.util.SparseArray;
import android.view.OrientationEventListener;
import android.view.Window; import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
...@@ -58,6 +60,9 @@ public class SuperPlayerPlugin implements FlutterPlugin, MethodCallHandler, Acti ...@@ -58,6 +60,9 @@ public class SuperPlayerPlugin implements FlutterPlugin, MethodCallHandler, Acti
private FTXAudioManager mTxAudioManager; private FTXAudioManager mTxAudioManager;
private FTXPIPManager mTxPipManager; private FTXPIPManager mTxPipManager;
private OrientationEventListener mOrientationManager;
private int mCurrentOrientation = FTXEvent.ORIENTATION_PORTRAIT_UP;
private final FTXAudioManager.AudioFocusChangeListener audioFocusChangeListener = private final FTXAudioManager.AudioFocusChangeListener audioFocusChangeListener =
new FTXAudioManager.AudioFocusChangeListener() { new FTXAudioManager.AudioFocusChangeListener() {
@Override @Override
...@@ -78,8 +83,7 @@ public class SuperPlayerPlugin implements FlutterPlugin, MethodCallHandler, Acti ...@@ -78,8 +83,7 @@ public class SuperPlayerPlugin implements FlutterPlugin, MethodCallHandler, Acti
channel.setMethodCallHandler(this); channel.setMethodCallHandler(this);
mPlayers = new SparseArray(); mPlayers = new SparseArray();
initAudioManagerIfNeed(); initAudioManagerIfNeed();
mEventChannel = new EventChannel(flutterPluginBinding.getBinaryMessenger(), "cloud.tencent" + mEventChannel = new EventChannel(flutterPluginBinding.getBinaryMessenger(), "cloud.tencent.com/playerPlugin/event");
".com/playerPlugin/event");
mEventChannel.setStreamHandler(new EventChannel.StreamHandler() { mEventChannel.setStreamHandler(new EventChannel.StreamHandler() {
@Override @Override
public void onListen(Object o, EventChannel.EventSink eventSink) { public void onListen(Object o, EventChannel.EventSink eventSink) {
...@@ -92,6 +96,31 @@ public class SuperPlayerPlugin implements FlutterPlugin, MethodCallHandler, Acti ...@@ -92,6 +96,31 @@ public class SuperPlayerPlugin implements FlutterPlugin, MethodCallHandler, Acti
} }
}); });
mFTXDownloadManager = new FTXDownloadManager(mFlutterPluginBinding); mFTXDownloadManager = new FTXDownloadManager(mFlutterPluginBinding);
mOrientationManager = new OrientationEventListener(flutterPluginBinding.getApplicationContext()) {
@Override
public void onOrientationChanged(int orientation) {
if (isAutoRotateOn()) {
int orientationEvent = mCurrentOrientation;
// 每个方向判断当前方向正负30度,共计60度的区间
if (((orientation >= 0) && (orientation < 30)) || (orientation > 330)) {
orientationEvent = FTXEvent.ORIENTATION_PORTRAIT_UP;
} else if (orientation > 240 && orientation < 300) {
orientationEvent = FTXEvent.ORIENTATION_LANDSCAPE_RIGHT;
} else if (orientation > 150 && orientation < 210) {
orientationEvent = FTXEvent.ORIENTATION_PORTRAIT_DOWN;
} else if (orientation > 60 && orientation < 110) {
orientationEvent = FTXEvent.ORIENTATION_LANDSCAPE_LEFT;
}
if (orientationEvent != mCurrentOrientation) {
mCurrentOrientation = orientationEvent;
Bundle bundle = new Bundle();
bundle.putInt(FTXEvent.EXTRA_NAME_ORIENTATION, orientationEvent);
mEventSink.success(getParams(FTXEvent.EVENT_ORIENTATION_CHANGED, bundle));
}
}
}
};
mOrientationManager.enable();
} }
@Override @Override
...@@ -228,6 +257,9 @@ public class SuperPlayerPlugin implements FlutterPlugin, MethodCallHandler, Acti ...@@ -228,6 +257,9 @@ public class SuperPlayerPlugin implements FlutterPlugin, MethodCallHandler, Acti
if (null != mTxPipManager) { if (null != mTxPipManager) {
mTxPipManager.releaseReceiver(); mTxPipManager.releaseReceiver();
} }
if (null != mOrientationManager) {
mOrientationManager.disable();
}
unregisterReceiver(); unregisterReceiver();
} }
...@@ -240,10 +272,20 @@ public class SuperPlayerPlugin implements FlutterPlugin, MethodCallHandler, Acti ...@@ -240,10 +272,20 @@ public class SuperPlayerPlugin implements FlutterPlugin, MethodCallHandler, Acti
} }
/** /**
* 注册音量广播接收器 * 系统是否允许自动旋转屏幕
* *
* @return * @return
*/ */
protected boolean isAutoRotateOn() {
//获取系统是否允许自动旋转屏幕
return (android.provider.Settings.System.getInt(
mFlutterPluginBinding.getApplicationContext().getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION, 0) == 1);
}
/**
* 注册音量广播接收器
*/
public void registerReceiver() { public void registerReceiver() {
// volume receiver // volume receiver
mVolumeBroadcastReceiver = new VolumeBroadcastReceiver(); mVolumeBroadcastReceiver = new VolumeBroadcastReceiver();
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
[volumeView sizeToFit]; [volumeView sizeToFit];
// 单例slider // 单例slider
_volumeSlider = nil; _volumeSlider = nil;
//开始接收遥控事件
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
for (UIView *view in [volumeView subviews]) { for (UIView *view in [volumeView subviews]) {
if ([view.class.description isEqualToString:@"MPVolumeSlider"]) { if ([view.class.description isEqualToString:@"MPVolumeSlider"]) {
_volumeSlider = (UISlider *)view; _volumeSlider = (UISlider *)view;
......
...@@ -44,4 +44,17 @@ ...@@ -44,4 +44,17 @@
// 视频下载错误 // 视频下载错误
#define EVENT_DOWNLOAD_ERROR 305 #define EVENT_DOWNLOAD_ERROR 305
// 屏幕旋转事件
#define EVENT_ORIENTATION_CHANGED 401
// 屏幕旋转方向
#define EXTRA_NAME_ORIENTATION @"orientation"
// 正竖屏
#define ORIENTATION_PORTRAIT_UP 411
// 横屏,底部在右
#define ORIENTATION_LANDSCAPE_RIGHT 412
// 竖屏,顶部在下
#define ORIENTATION_PORTRAIT_DOWN 413
// 横屏,底部在左
#define ORIENTATION_LANDSCAPE_LEFT 414
#endif /* FTXEvent_h */ #endif /* FTXEvent_h */
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
FTXPlayerEventSinkQueue *_pipEventSink; FTXPlayerEventSinkQueue *_pipEventSink;
FTXAudioManager *audioManager; FTXAudioManager *audioManager;
FTXDownloadManager *_FTXDownloadManager; FTXDownloadManager *_FTXDownloadManager;
int mCurrentOrientation;
} }
SuperPlayerPlugin* instance; SuperPlayerPlugin* instance;
...@@ -43,6 +44,7 @@ SuperPlayerPlugin* instance; ...@@ -43,6 +44,7 @@ SuperPlayerPlugin* instance;
if (nil != _FTXDownloadManager) { if (nil != _FTXDownloadManager) {
[_FTXDownloadManager destroy]; [_FTXDownloadManager destroy];
} }
[[NSNotificationCenter defaultCenter] removeObserver:self];
} }
- (instancetype)initWithRegistrar: - (instancetype)initWithRegistrar:
...@@ -62,10 +64,18 @@ SuperPlayerPlugin* instance; ...@@ -62,10 +64,18 @@ SuperPlayerPlugin* instance;
_eventChannel = [FlutterEventChannel eventChannelWithName:@"cloud.tencent.com/playerPlugin/event" binaryMessenger:[registrar messenger]]; _eventChannel = [FlutterEventChannel eventChannelWithName:@"cloud.tencent.com/playerPlugin/event" binaryMessenger:[registrar messenger]];
_pipEventChannel = [FlutterEventChannel eventChannelWithName:@"cloud.tencent.com/playerPlugin/pipEvent" binaryMessenger:[registrar messenger]]; _pipEventChannel = [FlutterEventChannel eventChannelWithName:@"cloud.tencent.com/playerPlugin/pipEvent" binaryMessenger:[registrar messenger]];
[_eventChannel setStreamHandler:self]; [_eventChannel setStreamHandler:self];
[_pipEventChannel setStreamHandler:self];
[audioManager registerVolumeChangeListener:self selector:@selector(systemVolumeDidChangeNoti:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil]; [audioManager registerVolumeChangeListener:self selector:@selector(systemVolumeDidChangeNoti:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
// pip
[_pipEventChannel setStreamHandler:self];
// download
_FTXDownloadManager = [[FTXDownloadManager alloc] initWithRegistrar:registrar]; _FTXDownloadManager = [[FTXDownloadManager alloc] initWithRegistrar:registrar];
// orientation
mCurrentOrientation = ORIENTATION_PORTRAIT_UP;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onDeviceOrientationChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
return self; return self;
} }
...@@ -203,6 +213,7 @@ SuperPlayerPlugin* instance; ...@@ -203,6 +213,7 @@ SuperPlayerPlugin* instance;
if ([arguments isKindOfClass:NSString.class]) { if ([arguments isKindOfClass:NSString.class]) {
if ([arguments isEqualToString:@"event"]) { if ([arguments isEqualToString:@"event"]) {
[_eventSink setDelegate:events]; [_eventSink setDelegate:events];
} else if ([arguments isEqualToString:@"pipEvent"]) {
[_pipEventSink setDelegate:events]; [_pipEventSink setDelegate:events];
} }
} }
...@@ -215,6 +226,7 @@ SuperPlayerPlugin* instance; ...@@ -215,6 +226,7 @@ SuperPlayerPlugin* instance;
if ([arguments isKindOfClass:NSString.class]) { if ([arguments isKindOfClass:NSString.class]) {
if ([arguments isEqualToString:@"event"]) { if ([arguments isEqualToString:@"event"]) {
[_eventSink setDelegate:nil]; [_eventSink setDelegate:nil];
} else if ([arguments isEqualToString:@"pipEvent"]) {
[_pipEventSink setDelegate:nil]; [_pipEventSink setDelegate:nil];
} }
} }
...@@ -247,4 +259,39 @@ SuperPlayerPlugin* instance; ...@@ -247,4 +259,39 @@ SuperPlayerPlugin* instance;
[_pipEventSink success:@{@"event" : @(EVENT_PIP_MODE_RESTORE_UI)}]; [_pipEventSink success:@{@"event" : @(EVENT_PIP_MODE_RESTORE_UI)}];
} }
#pragma mark - orientation
- (void)onDeviceOrientationChange:(NSNotification *)notification {
// IOS 此处不需要判断是否打开自动屏幕旋转/竖排锁定开关,当IOS打开锁定之后,这里默认是收不到回调的
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
UIInterfaceOrientation interfaceOrientation = (UIInterfaceOrientation)orientation;
int tempOrientationCode = mCurrentOrientation;
switch (interfaceOrientation) {
case UIInterfaceOrientationPortrait:
// 电池栏在上
tempOrientationCode = ORIENTATION_PORTRAIT_UP;
break;
case UIInterfaceOrientationLandscapeLeft:
// 电池栏在左
tempOrientationCode = ORIENTATION_LANDSCAPE_LEFT;
break;
case UIInterfaceOrientationPortraitUpsideDown:
// 电池栏在下
tempOrientationCode = ORIENTATION_PORTRAIT_DOWN;
break;
case UIInterfaceOrientationLandscapeRight:
// 电池栏在右
tempOrientationCode = ORIENTATION_LANDSCAPE_RIGHT;
break;
default:
break;
}
if(tempOrientationCode != mCurrentOrientation) {
mCurrentOrientation = tempOrientationCode;
[_eventSink success:@{
@"event" : @(EVENT_ORIENTATION_CHANGED),
EXTRA_NAME_ORIENTATION : @(tempOrientationCode)}];
}
}
@end @end
...@@ -27,7 +27,7 @@ class SuperPlayerPlugin { ...@@ -27,7 +27,7 @@ class SuperPlayerPlugin {
eventChannel.receiveBroadcastStream("event").listen(_eventHandler, onError: _errorHandler); eventChannel.receiveBroadcastStream("event").listen(_eventHandler, onError: _errorHandler);
EventChannel pipEventChanne = EventChannel("cloud.tencent.com/playerPlugin/pipEvent"); EventChannel pipEventChanne = EventChannel("cloud.tencent.com/playerPlugin/pipEvent");
pipEventChanne.receiveBroadcastStream("event").listen(_pipEventHandler, onError: _errorHandler); pipEventChanne.receiveBroadcastStream("pipEvent").listen(_pipEventHandler, onError: _errorHandler);
} }
_pipEventHandler(event) { _pipEventHandler(event) {
......
...@@ -97,6 +97,12 @@ abstract class TXVodPlayEvent { ...@@ -97,6 +97,12 @@ abstract class TXVodPlayEvent {
static const EVENT_IOS_PIP_MODE_RESTORE_UI = 5; // 重置UI only support iOS static const EVENT_IOS_PIP_MODE_RESTORE_UI = 5; // 重置UI only support iOS
static const EVENT_IOS_PIP_MODE_WILL_EXIT = 6; // 将要退出画中画 only support iOS static const EVENT_IOS_PIP_MODE_WILL_EXIT = 6; // 将要退出画中画 only support iOS
static const EVENT_ORIENTATION_CHANGED = 401; // 屏幕发生旋转
static const EXTRA_NAME_ORIENTATION = "orientation"; // 屏幕旋转方向
static const ORIENTATION_PORTRAIT_UP = 411; // 竖屏,顶部在上
static const ORIENTATION_LANDSCAPE_RIGHT = 412; // 横屏,顶部在左,底部在右
static const ORIENTATION_PORTRAIT_DOWN = 413; // 竖屏,顶部在下
static const ORIENTATION_LANDSCAPE_LEFT = 414; // 横屏,顶部在右,底部在左
static const NO_ERROR = 0; static const NO_ERROR = 0;
static const ERROR_PIP_LOWER_VERSION = -101; // pip 错误,android版本过低 static const ERROR_PIP_LOWER_VERSION = -101; // pip 错误,android版本过低
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论