mirror of
https://github.com/blanking003/cx-cocos.git
synced 2025-11-07 06:45:31 +00:00
init
This commit is contained in:
39
cx3-demo/project/cxdemo.ios/Classes/jsIntf-ios.mm
Normal file
39
cx3-demo/project/cxdemo.ios/Classes/jsIntf-ios.mm
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "cxCreator.h"
|
||||
|
||||
//#include <AMapFoundationKit/AMapFoundationKit.h>
|
||||
//#include "amapLocationIntf.h"
|
||||
#include "systemIntf.h"
|
||||
#include "videoIntf.h"
|
||||
|
||||
|
||||
void vendorSdkInit(std::string classname);
|
||||
|
||||
NativeIntfClass* createAppNativeClass(std::string classname)
|
||||
{
|
||||
vendorSdkInit(classname);
|
||||
|
||||
// if (classname == "amapLocation")
|
||||
// return AmapLocationIntf::ins();
|
||||
|
||||
if (classname == "system")
|
||||
return SystemIntf::ins();
|
||||
|
||||
if (classname == "video")
|
||||
return VideoIntf::ins();
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static bool vendorSdkInited = false;
|
||||
void vendorSdkInit(std::string classname)
|
||||
{
|
||||
if (vendorSdkInited)
|
||||
return;
|
||||
vendorSdkInited = true;
|
||||
|
||||
//[AMapServices sharedServices].apiKey = @"33492236ca1ace0d029c93755fa4d6d0";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
10
cx3-demo/project/cxdemo.ios/Classes/jsIntf-mac.mm
Normal file
10
cx3-demo/project/cxdemo.ios/Classes/jsIntf-mac.mm
Normal file
@@ -0,0 +1,10 @@
|
||||
#include "cxCreator.h"
|
||||
|
||||
NativeIntfClass* createAppNativeClass(std::string classname)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
16
cx3-demo/project/cxdemo.ios/Classes/system/systemIntf.h
Normal file
16
cx3-demo/project/cxdemo.ios/Classes/system/systemIntf.h
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cxDefine.h"
|
||||
|
||||
class SystemIntf : public NativeIntfClass
|
||||
{
|
||||
public:
|
||||
static SystemIntf* ins();
|
||||
virtual std::string call(std::string fname, cc::ValueVector params, const DataCallback& callback) override;
|
||||
|
||||
private:
|
||||
|
||||
DataCallback dataCallback;
|
||||
};
|
||||
|
||||
24
cx3-demo/project/cxdemo.ios/Classes/system/systemIntf.mm
Normal file
24
cx3-demo/project/cxdemo.ios/Classes/system/systemIntf.mm
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
#include "systemIntf.h"
|
||||
#include "AppController.h"
|
||||
|
||||
static SystemIntf* s_sharedSystemIntf = nullptr;
|
||||
SystemIntf* SystemIntf::ins()
|
||||
{
|
||||
if (!s_sharedSystemIntf)
|
||||
s_sharedSystemIntf = new SystemIntf();
|
||||
return s_sharedSystemIntf;
|
||||
}
|
||||
|
||||
std::string SystemIntf::call(std::string fname, cc::ValueVector params, const DataCallback& callback)
|
||||
{
|
||||
// callPhone(std::string num);
|
||||
if (fname == "callPhone")
|
||||
{
|
||||
NSString* strNum = [[NSString alloc] initWithUTF8String:params.at(0).asString().c_str()];
|
||||
NSMutableString* msNum = [[NSMutableString alloc] initWithFormat:@"tel:%@", strNum];
|
||||
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: msNum] options:@{} completionHandler:nil];
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
18
cx3-demo/project/cxdemo.ios/Classes/video/videoImpl.h
Normal file
18
cx3-demo/project/cxdemo.ios/Classes/video/videoImpl.h
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface VideoView : UIView
|
||||
|
||||
-(void) setPosition:(float)left top:(float)top;
|
||||
-(void) play:(NSString*)url listenProgress:(bool)listenProgress;
|
||||
-(void) pause;
|
||||
-(void) resume;
|
||||
-(void) close;
|
||||
-(void) setFullScreen:(bool)value;
|
||||
-(void) seekToTime:(int)seconds;
|
||||
-(void) showBar:(bool)value;
|
||||
-(void) lockSeek:(bool)value;
|
||||
|
||||
@end
|
||||
|
||||
413
cx3-demo/project/cxdemo.ios/Classes/video/videoImpl.mm
Normal file
413
cx3-demo/project/cxdemo.ios/Classes/video/videoImpl.mm
Normal file
@@ -0,0 +1,413 @@
|
||||
|
||||
#import "videoImpl.h"
|
||||
|
||||
#import <AVKit/AVKit.h>
|
||||
#import "AppController.h"
|
||||
#import "videoIntf.h"
|
||||
|
||||
@implementation VideoView
|
||||
{
|
||||
UIViewController* contentController;
|
||||
|
||||
//contentController 包含以下内容
|
||||
AVPlayerViewController* moviePlayer;
|
||||
UIView* tapView;
|
||||
UIView* actionView;
|
||||
|
||||
//actionView 包含以下内容
|
||||
UILabel* lblTimelen;
|
||||
UILabel* lblTimelenTotal;
|
||||
UIButton* btnPause;
|
||||
UIButton* btnResume;
|
||||
UIButton* btnClose;
|
||||
UISlider* slider;
|
||||
|
||||
bool defaultShowBar; //默认是否显示控制栏,默认false
|
||||
bool sliderTouchDown;
|
||||
bool seekDisabled;
|
||||
|
||||
int currTime;
|
||||
int priorTime;
|
||||
bool playing;
|
||||
|
||||
NSTimer* timer;
|
||||
}
|
||||
|
||||
-(id) initWithFrame:(CGRect)frame
|
||||
{
|
||||
[super initWithFrame:frame];
|
||||
|
||||
CGRect rect = CGRectMake(0, 0, frame.size.width, frame.size.height);
|
||||
|
||||
moviePlayer = [[AVPlayerViewController alloc] init];
|
||||
moviePlayer.showsPlaybackControls = NO;
|
||||
moviePlayer.view.frame = rect;
|
||||
|
||||
contentController = [[UIViewController alloc] init];
|
||||
contentController.view.frame = rect;
|
||||
[contentController addChildViewController:moviePlayer];
|
||||
[contentController.view addSubview:moviePlayer.view];
|
||||
[self addSubview:contentController.view];
|
||||
|
||||
self.userInteractionEnabled = false;
|
||||
|
||||
moviePlayer.view.backgroundColor = [UIColor colorWithRed:0.01f green:0.01f blue:0.01f alpha:1.0f];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
-(void) setPosition:(float)left top:(float)top
|
||||
{
|
||||
[self setCenter: CGPointMake(left + self.bounds.size.width/2, top + self.bounds.size.height/2)];
|
||||
}
|
||||
|
||||
-(void) setFullScreen:(bool)value
|
||||
{
|
||||
if (value)
|
||||
{
|
||||
[AppController.ins addView:contentController.view];
|
||||
[AppController.ins.rootViewController setBarHideStatus:true];
|
||||
if (!defaultShowBar)
|
||||
[self showBar:true];
|
||||
|
||||
//获取视频宽度尺寸
|
||||
CGSize videoSize = CGSizeZero;
|
||||
NSArray *array = moviePlayer.player.currentItem.tracks;
|
||||
for (AVPlayerItemTrack* track in array)
|
||||
{
|
||||
if ([track.assetTrack.mediaType isEqualToString:AVMediaTypeVideo])
|
||||
{
|
||||
videoSize = track.assetTrack.naturalSize;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//如果宽大于高,自动横屏
|
||||
CGSize screen = UIScreen.mainScreen.bounds.size;
|
||||
if (videoSize.width > videoSize.height)
|
||||
{
|
||||
[self updateViewSize:CGRectMake(0,0, screen.height, screen.width)];
|
||||
contentController.view.center = CGPointMake(screen.width/2, screen.height/2);
|
||||
CGAffineTransform transform = CGAffineTransformIdentity;
|
||||
transform = CGAffineTransformRotate(transform, M_PI_2);
|
||||
contentController.view.transform = transform;
|
||||
}
|
||||
else
|
||||
{
|
||||
[self updateViewSize:UIScreen.mainScreen.bounds];
|
||||
contentController.view.center = CGPointMake(screen.width/2, screen.height/2);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
[AppController.ins.rootViewController setBarHideStatus:false];
|
||||
CGAffineTransform transform = CGAffineTransformIdentity;
|
||||
transform = CGAffineTransformRotate(transform, 0);
|
||||
contentController.view.transform = transform;
|
||||
[self addSubview:contentController.view];
|
||||
[self updateViewSize:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
|
||||
contentController.view.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
|
||||
if (!defaultShowBar)
|
||||
[self showBar:false];
|
||||
}
|
||||
}
|
||||
|
||||
-(void) updateViewSize:(CGRect)rect
|
||||
{
|
||||
contentController.view.frame = rect;
|
||||
moviePlayer.view.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
|
||||
tapView.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
|
||||
actionView.frame = CGRectMake(0, rect.size.height-40, rect.size.width, 40);
|
||||
|
||||
int d = rect.size.width*0.1f;
|
||||
btnClose.frame = CGRectMake(d-40, 0, 40, 40);
|
||||
btnPause.frame = CGRectMake(rect.size.width-d, 0, 40, 40);
|
||||
btnResume.frame = CGRectMake(rect.size.width-d, 0, 40, 40);
|
||||
lblTimelen.frame = CGRectMake(d, 0, 60, 40);
|
||||
lblTimelenTotal.frame = CGRectMake(rect.size.width-d-60, 0, 60, 40);
|
||||
slider.frame = CGRectMake(d+70, 15, rect.size.width-d-70 - d-70, 10);
|
||||
}
|
||||
|
||||
-(void) createBarView
|
||||
{
|
||||
if (actionView)
|
||||
return;
|
||||
|
||||
//点击层
|
||||
tapView = [[UIView alloc] init];
|
||||
tapView.backgroundColor = [UIColor colorWithRed:1.0 green:0 blue:0 alpha:0.0f];
|
||||
tapView.userInteractionEnabled = YES;
|
||||
[tapView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(movieClick)]];
|
||||
|
||||
//操作栏
|
||||
actionView = [[UIView alloc] init];
|
||||
actionView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6f];
|
||||
|
||||
//进度条
|
||||
slider = [[UISlider alloc] init];
|
||||
slider.minimumTrackTintColor = [UIColor whiteColor];
|
||||
slider.maximumTrackTintColor = [UIColor grayColor];
|
||||
slider.minimumValue = 1;
|
||||
slider.maximumValue = 10000;
|
||||
slider.enabled = !seekDisabled;
|
||||
[slider addTarget:self action:@selector(onSliderValueChanged) forControlEvents:UIControlEventValueChanged];
|
||||
[slider addTarget:self action:@selector(onSliderTouchDown) forControlEvents:UIControlEventTouchDown];
|
||||
[slider addTarget:self action:@selector(onSliderTouchUp) forControlEvents:UIControlEventTouchUpInside];
|
||||
[slider addTarget:self action:@selector(onSliderTouchUp) forControlEvents:UIControlEventTouchUpOutside];
|
||||
|
||||
//当前播放时间
|
||||
lblTimelen = [[UILabel alloc] init];
|
||||
lblTimelen.text = @"";
|
||||
lblTimelen.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
|
||||
lblTimelen.textColor = [UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1];
|
||||
lblTimelen.textAlignment = NSTextAlignmentRight;
|
||||
|
||||
//总时长
|
||||
lblTimelenTotal = [[UILabel alloc] init];
|
||||
lblTimelenTotal.text = @"00:00";
|
||||
lblTimelenTotal.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
|
||||
lblTimelenTotal.textColor = [UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1];
|
||||
lblTimelenTotal.textAlignment = NSTextAlignmentLeft;
|
||||
|
||||
//关闭按钮
|
||||
btnClose = [self createButton:@"statics/video_stop.png"];
|
||||
[btnClose addTarget:self action:@selector(closeClick) forControlEvents:UIControlEventTouchUpInside];
|
||||
|
||||
//暂停按钮
|
||||
btnPause = [self createButton:@"statics/video_pause.png"];
|
||||
[btnPause addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside];
|
||||
|
||||
//继续按钮
|
||||
btnResume = [self createButton:@"statics/video_play.png"];
|
||||
[btnResume addTarget:self action:@selector(resume) forControlEvents:UIControlEventTouchUpInside];
|
||||
btnResume.hidden = YES;
|
||||
|
||||
[actionView addSubview:slider];
|
||||
[actionView addSubview:lblTimelen];
|
||||
[actionView addSubview:lblTimelenTotal];
|
||||
[actionView addSubview:btnClose];
|
||||
[actionView addSubview:btnPause];
|
||||
[actionView addSubview:btnResume];
|
||||
|
||||
[contentController.view addSubview:tapView];
|
||||
[contentController.view addSubview:actionView];
|
||||
|
||||
[self updateViewSize:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
|
||||
}
|
||||
|
||||
-(void) play:(NSString*)url listenProgress:(bool)listenProgress;
|
||||
{
|
||||
currTime = 0;
|
||||
playing = true;
|
||||
|
||||
if (moviePlayer.player)
|
||||
[moviePlayer.player release];
|
||||
|
||||
if ([[url substringToIndex:4] caseInsensitiveCompare:@"http"] == NSOrderedSame)
|
||||
{
|
||||
moviePlayer.player = [AVPlayer playerWithURL:[NSURL URLWithString:url]];
|
||||
}
|
||||
else //from statics
|
||||
{
|
||||
NSString* fileName = [url substringToIndex:url.length-4];
|
||||
NSString* filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"statics/%@", fileName] ofType:@".mp4"];
|
||||
moviePlayer.player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:filePath]];
|
||||
}
|
||||
[moviePlayer.player play];
|
||||
|
||||
if (listenProgress)
|
||||
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateClock:) userInfo:nil repeats:YES];
|
||||
}
|
||||
|
||||
-(void) seekToTime:(int)seconds
|
||||
{
|
||||
if (moviePlayer)
|
||||
[moviePlayer.player seekToTime:CMTimeMake(seconds*10000, 10000) toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
|
||||
}
|
||||
|
||||
-(void) onSliderTouchDown
|
||||
{
|
||||
sliderTouchDown = true;
|
||||
}
|
||||
|
||||
-(void) onSliderTouchUp
|
||||
{
|
||||
sliderTouchDown = false;
|
||||
}
|
||||
|
||||
-(void) onSliderValueChanged
|
||||
{
|
||||
int value = round(slider.value);
|
||||
int total = round(moviePlayer.player.currentItem.duration.value/moviePlayer.player.currentItem.duration.timescale);
|
||||
int curr = round((double)value/10000.0f*(double)total);
|
||||
int delta = priorTime - curr;
|
||||
if (delta >= 1 || delta <= -1)
|
||||
{
|
||||
priorTime = curr;
|
||||
[self seekToTime:curr];
|
||||
lblTimelen.text = [self formatTime:curr];
|
||||
}
|
||||
}
|
||||
|
||||
-(void) updateClock:(NSTimer *)timer
|
||||
{
|
||||
if (!playing)
|
||||
return;
|
||||
int total = round(moviePlayer.player.currentItem.duration.value/moviePlayer.player.currentItem.duration.timescale);
|
||||
if (total <= 0)
|
||||
return;
|
||||
int curr = round(moviePlayer.player.currentTime.value/moviePlayer.player.currentTime.timescale);
|
||||
//NSLog(@"%lld, %d, %d", moviePlayer.player.currentTime.value, moviePlayer.player.currentTime.timescale, curr);
|
||||
if (actionView != NULL && !sliderTouchDown)
|
||||
{
|
||||
lblTimelen.text = [self formatTime:curr];
|
||||
if ([lblTimelenTotal.text isEqualToString:@"00:00"])
|
||||
lblTimelenTotal.text = [self formatTime:total];
|
||||
[slider setValue:round((double)curr/(double)total*10000.0f)];
|
||||
}
|
||||
if (curr > currTime)
|
||||
currTime = curr;
|
||||
|
||||
VideoIntf::ins()->callJs(8, std::to_string(currTime));
|
||||
|
||||
if (curr >= total)
|
||||
[self videoFinished];
|
||||
}
|
||||
|
||||
-(NSString*) formatTime:(double)d
|
||||
{
|
||||
int c = (int)d;
|
||||
int c_m = c/60;
|
||||
int c_s = c%60;
|
||||
return [NSString stringWithFormat:@"%s%d:%s%d", c_m < 10 ? "0" : "" , c_m, c_s < 10 ? "0" : "", c_s];
|
||||
}
|
||||
|
||||
-(void) showBar:(bool)value
|
||||
{
|
||||
[self createBarView];
|
||||
[actionView setHidden:!value];
|
||||
}
|
||||
|
||||
-(void) lockSeek:(bool)value
|
||||
{
|
||||
seekDisabled = !value;
|
||||
if (slider)
|
||||
slider.enabled = !value;
|
||||
}
|
||||
|
||||
-(void) pause
|
||||
{
|
||||
if (playing)
|
||||
{
|
||||
playing = false;
|
||||
if (actionView)
|
||||
{
|
||||
btnPause.hidden = true;
|
||||
btnResume.hidden = false;
|
||||
}
|
||||
if (moviePlayer)
|
||||
[moviePlayer.player pause];
|
||||
}
|
||||
}
|
||||
|
||||
-(void) resume
|
||||
{
|
||||
if (!playing)
|
||||
{
|
||||
playing = true;
|
||||
if (actionView)
|
||||
{
|
||||
btnPause.hidden = false;
|
||||
btnResume.hidden = true;
|
||||
}
|
||||
if (moviePlayer)
|
||||
{
|
||||
int total = round(moviePlayer.player.currentItem.duration.value/moviePlayer.player.currentItem.duration.timescale);
|
||||
int curr = round(moviePlayer.player.currentTime.value/moviePlayer.player.currentTime.timescale);
|
||||
if (total > 0 && curr >= total)
|
||||
[self seekToTime:0];
|
||||
[moviePlayer.player play];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
-(void) movieClick
|
||||
{
|
||||
if (actionView)
|
||||
[actionView setHidden:!actionView.isHidden];
|
||||
}
|
||||
|
||||
-(void) closeClick
|
||||
{
|
||||
[self setFullScreen:false];
|
||||
}
|
||||
|
||||
-(void) close
|
||||
{
|
||||
[timer invalidate];
|
||||
timer = nil;
|
||||
playing = false;
|
||||
|
||||
[moviePlayer.player pause];
|
||||
[moviePlayer.view removeFromSuperview];
|
||||
[self removeFromSuperview];
|
||||
}
|
||||
|
||||
-(void) videoFinished
|
||||
{
|
||||
playing = false;
|
||||
if (actionView)
|
||||
{
|
||||
[actionView setHidden:false];
|
||||
[btnPause setHidden:true];
|
||||
[btnResume setHidden:false];
|
||||
}
|
||||
currTime = (int)moviePlayer.player.currentTime.value/moviePlayer.player.currentTime.timescale;
|
||||
VideoIntf::ins()->callJs(3, std::to_string(currTime));
|
||||
}
|
||||
|
||||
-(UIButton*)createButton:(NSString*)img
|
||||
{
|
||||
UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
|
||||
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
||||
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
|
||||
|
||||
UIImage *image = [UIImage imageNamed:img];
|
||||
[btn setImage:image forState:UIControlStateNormal];
|
||||
[btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
|
||||
|
||||
return btn;
|
||||
}
|
||||
|
||||
-(void) playStateChange
|
||||
{
|
||||
// MPMoviePlaybackState state = [moviePlayer playbackState];
|
||||
// switch (state)
|
||||
// {
|
||||
// case MPMoviePlaybackStatePaused:
|
||||
// //mediaPlayerIntf->mediaCallback(1, 0);
|
||||
// break;
|
||||
// case MPMoviePlaybackStateStopped:
|
||||
// //mediaPlayerIntf->mediaCallback(2, 0);
|
||||
// break;
|
||||
// case MPMoviePlaybackStatePlaying:
|
||||
// if (currTime > [moviePlayer currentPlaybackTime])
|
||||
// {
|
||||
// [moviePlayer setCurrentPlaybackTime:currTime];
|
||||
// currTime = (int)[moviePlayer currentPlaybackTime];
|
||||
// }
|
||||
// //mediaPlayerIntf->mediaCallback(0, 0);
|
||||
// break;
|
||||
// case MPMoviePlaybackStateInterrupted:
|
||||
// break;
|
||||
// case MPMoviePlaybackStateSeekingBackward:
|
||||
// break;
|
||||
// case MPMoviePlaybackStateSeekingForward:
|
||||
// break;
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
34
cx3-demo/project/cxdemo.ios/Classes/video/videoIntf.h
Normal file
34
cx3-demo/project/cxdemo.ios/Classes/video/videoIntf.h
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cxDefine.h"
|
||||
|
||||
class VideoIntf : public NativeIntfClass
|
||||
{
|
||||
public:
|
||||
static VideoIntf* ins();
|
||||
|
||||
virtual std::string call(std::string fname, cc::ValueVector params, const DataCallback& callback) override;
|
||||
|
||||
void callJs(int state, std::string value);
|
||||
|
||||
private:
|
||||
|
||||
DataCallback dataCallback;
|
||||
|
||||
void createInMask(std::string videoName, std::string maskName, float rectX, float rectY, float rectW, float rectH);
|
||||
void create(std::string videoName, float rectX, float rectY, float rectW, float rectH);
|
||||
void setRoundRadius(std::string videoName, float radius);
|
||||
void setPosition(std::string videoName, float rectX, float rectY);
|
||||
void removeVideo(std::string videoName);
|
||||
void removeInMask(std::string maskName);
|
||||
void play(std::string videoName, std::string url, const DataCallback& callback);
|
||||
void setFullScreen(std::string videoName, bool value);
|
||||
void pause(std::string videoName, bool hide);
|
||||
void resume(std::string videoName);
|
||||
void seekToTime(std::string videoName, int seconds);
|
||||
void lockSeek(std::string videoName, bool value);
|
||||
void showBar(std::string videoName, bool value);
|
||||
|
||||
};
|
||||
|
||||
265
cx3-demo/project/cxdemo.ios/Classes/video/videoIntf.mm
Normal file
265
cx3-demo/project/cxdemo.ios/Classes/video/videoIntf.mm
Normal file
@@ -0,0 +1,265 @@
|
||||
|
||||
#include "videoIntf.h"
|
||||
|
||||
#include "videoImpl.h"
|
||||
#include "cxMaskIntf.h"
|
||||
#include "AppController.h"
|
||||
|
||||
static VideoIntf* s_sharedVideoIntf = nullptr;
|
||||
VideoIntf* VideoIntf::ins()
|
||||
{
|
||||
if (!s_sharedVideoIntf)
|
||||
{
|
||||
s_sharedVideoIntf = new VideoIntf();
|
||||
s_sharedVideoIntf->dataCallback = nullptr;
|
||||
}
|
||||
return s_sharedVideoIntf;
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, VideoView*> m_videoList;
|
||||
VideoView* getVideoView(std::string name)
|
||||
{
|
||||
auto itr = m_videoList.find(name);
|
||||
if (itr != m_videoList.end())
|
||||
return itr->second;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::string VideoIntf::call(std::string fname, cc::ValueVector params, const DataCallback& callback)
|
||||
{
|
||||
//在指定的maskView中创建videoView
|
||||
if (fname == "createInMask")
|
||||
{
|
||||
std::string videoName = params.at(0).asString();
|
||||
std::string maskName = params.at(1).asString();
|
||||
float rectX = params.at(2).asFloat();
|
||||
float rectY = params.at(3).asFloat();
|
||||
float rectW = params.at(4).asFloat();
|
||||
float rectH = params.at(5).asFloat();
|
||||
createInMask(videoName, maskName, rectX, rectY, rectW, rectH);
|
||||
}
|
||||
|
||||
else if (fname == "create")
|
||||
{
|
||||
std::string videoName = params.at(0).asString();
|
||||
float rectX = params.at(1).asFloat();
|
||||
float rectY = params.at(2).asFloat();
|
||||
float rectW = params.at(3).asFloat();
|
||||
float rectH = params.at(4).asFloat();
|
||||
create(videoName, rectX, rectY, rectW, rectH);
|
||||
}
|
||||
|
||||
else if (fname == "setRoundRadius")
|
||||
{
|
||||
std::string videoName = params.at(0).asString();
|
||||
float radius = params.at(1).asFloat();
|
||||
setRoundRadius(videoName, radius);
|
||||
}
|
||||
|
||||
else if (fname == "setPosition")
|
||||
{
|
||||
std::string videoName = params.at(0).asString();
|
||||
float rectX = params.at(1).asFloat();
|
||||
float rectY = params.at(2).asFloat();
|
||||
setPosition(videoName, rectX, rectY);
|
||||
}
|
||||
|
||||
//删除指定的videoView
|
||||
else if (fname == "removeVideo")
|
||||
{
|
||||
std::string videoName = params.at(0).asString();
|
||||
removeVideo(videoName);
|
||||
}
|
||||
|
||||
//删除指定maskView中的所有videoView
|
||||
else if (fname == "removeInMask")
|
||||
{
|
||||
std::string maskName = params.at(0).asString();
|
||||
removeInMask(maskName);
|
||||
}
|
||||
|
||||
else if (fname == "play")
|
||||
{
|
||||
std::string videoName = params.at(0).asString();
|
||||
std::string url = params.at(1).asString();
|
||||
play(videoName, url, callback);
|
||||
}
|
||||
|
||||
else if (fname == "setFullScreen")
|
||||
{
|
||||
std::string videoName = params.at(0).asString();
|
||||
bool value = params.at(1).asBool();
|
||||
setFullScreen(videoName, value);
|
||||
}
|
||||
|
||||
else if (fname == "pause")
|
||||
{
|
||||
std::string videoName = params.at(0).asString();
|
||||
pause(videoName, params.at(1).asBool());
|
||||
}
|
||||
|
||||
else if (fname == "resume")
|
||||
{
|
||||
std::string videoName = params.at(0).asString();
|
||||
resume(videoName);
|
||||
}
|
||||
|
||||
else if (fname == "seekToTime")
|
||||
{
|
||||
std::string videoName = params.at(0).asString();
|
||||
int seconds = params.at(1).asInt();
|
||||
seekToTime(videoName, seconds);
|
||||
}
|
||||
|
||||
else if (fname == "lockSeek")
|
||||
{
|
||||
std::string videoName = params.at(0).asString();
|
||||
bool value = params.at(1).asBool();
|
||||
lockSeek(videoName, value);
|
||||
}
|
||||
|
||||
else if (fname == "showBar")
|
||||
{
|
||||
std::string videoName = params.at(0).asString();
|
||||
bool value = params.at(1).asBool();
|
||||
showBar(videoName, value);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
void VideoIntf::callJs(int state, std::string value)
|
||||
{
|
||||
if (dataCallback)
|
||||
dataCallback(state, value);
|
||||
}
|
||||
|
||||
void VideoIntf::createInMask(std::string videoName, std::string maskName, float rectX, float rectY, float rectW, float rectH)
|
||||
{
|
||||
if (!getVideoView(videoName))
|
||||
{
|
||||
VideoView* videoView = [[VideoView alloc] initWithFrame:CGRectMake(rectX, rectY, rectW, rectH)];
|
||||
videoView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:255 alpha:1];
|
||||
CxMaskIntf::ins()->addNativeView(maskName, videoView);
|
||||
m_videoList.emplace(videoName, videoView);
|
||||
}
|
||||
}
|
||||
|
||||
void VideoIntf::create(std::string videoName, float rectX, float rectY, float rectW, float rectH)
|
||||
{
|
||||
if (!getVideoView(videoName))
|
||||
{
|
||||
VideoView* videoView = [[VideoView alloc] initWithFrame:CGRectMake(rectX, rectY, rectW, rectH)];
|
||||
[AppController.ins addView:videoView];
|
||||
m_videoList.emplace(videoName, videoView);
|
||||
}
|
||||
}
|
||||
|
||||
void VideoIntf::setRoundRadius(std::string videoName, float radius)
|
||||
{
|
||||
auto videoView = getVideoView(videoName);
|
||||
if (videoView)
|
||||
{
|
||||
UIBezierPath* round =[UIBezierPath bezierPathWithRoundedRect:videoView.bounds cornerRadius:radius];
|
||||
CAShapeLayer* maskLayer = [CAShapeLayer layer];
|
||||
maskLayer.path = [round CGPath];
|
||||
maskLayer.fillRule = kCAFillRuleNonZero;
|
||||
videoView.layer.mask = maskLayer;
|
||||
}
|
||||
}
|
||||
|
||||
void VideoIntf::setPosition(std::string videoName, float rectX, float rectY)
|
||||
{
|
||||
auto videoView = getVideoView(videoName);
|
||||
if (videoView)
|
||||
[videoView setPosition:rectX top:rectY];
|
||||
}
|
||||
|
||||
void VideoIntf::removeVideo(std::string videoName)
|
||||
{
|
||||
auto videoView = getVideoView(videoName);
|
||||
if (videoView)
|
||||
{
|
||||
[videoView close];
|
||||
m_videoList.erase(videoName);
|
||||
}
|
||||
}
|
||||
|
||||
void VideoIntf::removeInMask(std::string maskName)
|
||||
{
|
||||
if (maskName.empty())
|
||||
return;
|
||||
for (auto itr = m_videoList.begin(); itr != m_videoList.end();)
|
||||
{
|
||||
auto videoView = itr->second;
|
||||
if (CxMaskIntf::ins()->hasNativeView(maskName, videoView))
|
||||
{
|
||||
[videoView close];
|
||||
m_videoList.erase(itr++);
|
||||
}
|
||||
else
|
||||
++itr;
|
||||
}
|
||||
}
|
||||
|
||||
void VideoIntf::play(std::string videoName, std::string url, const DataCallback& callback)
|
||||
{
|
||||
dataCallback = callback;
|
||||
auto videoView = getVideoView(videoName);
|
||||
if (videoView)
|
||||
{
|
||||
[videoView play:[NSString stringWithUTF8String:url.c_str()] listenProgress:!!callback];
|
||||
}
|
||||
}
|
||||
|
||||
void VideoIntf::setFullScreen(std::string videoName, bool value)
|
||||
{
|
||||
auto videoView = getVideoView(videoName);
|
||||
if (videoView)
|
||||
[videoView setFullScreen:value];
|
||||
}
|
||||
|
||||
void VideoIntf::pause(std::string videoName, bool hide)
|
||||
{
|
||||
auto videoView = getVideoView(videoName);
|
||||
if (videoView)
|
||||
{
|
||||
[videoView pause];
|
||||
if (hide)
|
||||
[videoView setHidden:true];
|
||||
}
|
||||
}
|
||||
|
||||
void VideoIntf::resume(std::string videoName)
|
||||
{
|
||||
auto videoView = getVideoView(videoName);
|
||||
if (videoView)
|
||||
{
|
||||
[videoView resume];
|
||||
if (videoView.isHidden)
|
||||
[videoView setHidden:false];
|
||||
}
|
||||
}
|
||||
|
||||
void VideoIntf::seekToTime(std::string videoName, int seconds)
|
||||
{
|
||||
auto videoView = getVideoView(videoName);
|
||||
if (videoView)
|
||||
[videoView seekToTime:seconds];
|
||||
}
|
||||
|
||||
void VideoIntf::lockSeek(std::string videoName, bool value)
|
||||
{
|
||||
auto videoView = getVideoView(videoName);
|
||||
if (videoView)
|
||||
[videoView lockSeek:value];
|
||||
}
|
||||
|
||||
void VideoIntf::showBar(std::string videoName, bool value)
|
||||
{
|
||||
auto videoView = getVideoView(videoName);
|
||||
if (videoView)
|
||||
[videoView showBar:value];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user