Quick-Cocos2dx 中 使用 ccexp.VideoPlayer 播放视频,
local videoPlayer = ccexp.VideoPlayer:create()
local pathMp4 = device.writablePath..'hotupdate//res//'..mp4name
if not io.exists(pathMp4) then
pathMp4 = cc.FileUtils:getInstance():fullPathForFilename(mp4name)
printCustom("got Mp4", pathMp4)
end
videoPlayer:setFileName(pathMp4)
videoPlayer:setPosition(cc.p(display.width/2, display.height/2))
videoPlayer:setAnchorPoint(cc.p(0.5, 0.5))
videoPlayer:setContentSize(cc.size(display.width, display.height))
--播放视频时是否始终保持高宽比
videoPlayer:setKeepAspectRatioEnabled(false)
videoPlayer:setFullScreenEnabled(true)
videoPlayer:setVisible(true)
self:addChild(videoPlayer, 99)
videoPlayer:addEventListener(function(videoPlayer, eventType)
if eventType == ccexp.VideoPlayerEvent.PLAYING then
printCustom("LoginScene:playMp4 playing")
elseif eventType == ccexp.VideoPlayerEvent.PAUSED then
printCustom("LoginScene:playMp4 paused")
if self.videoCouldSkip then
videoPlayer:onPlayEvent(ccexp.VideoPlayerEvent.COMPLETED)
end
elseif eventType == ccexp.VideoPlayerEvent.STOPPED then
printCustom("STOPPED")
elseif eventType == ccexp.VideoPlayerEvent.COMPLETED then
printCustom("LoginScene:playMp4 completed")
videoPlayer:stop()
self:performWithDelay(function ( ... )
self:removeChild(videoPlayer)
videoPlayer = nil
if callback then
callback()
end
end, 0.01)
end
end)
videoPlayer:play()
在 Android 环境下, 用户的点击事件响应的是 播放/暂停 事件, 在Quick-Cocos2dx引擎UIVideoPlayer-android.cpp文件中可以看到. 我们在处理时可以在 videoPlayer:addEventListener 的ccexp.VideoPlayerEvent.PAUSED 中 做 videoPlayer:onPlayEvent(ccexp.VideoPlayerEvent.COMPLETED)处理, 即可完成点击后停止播放.
在 iOS 环境下, 犹豫用户点击的响应会调出播放器操作面板, 这里需要在 UIVideoPlayer-ios.mm中做个设置
self.moviePlayer.controlStyle = MPMovieControlStyleNone;// MPMovieControlStyleEmbedded;
去掉播放器操作面板
出现的问题
在希望self.moviePlayer.view能响应用户点击的时候设置了
self.moviePlayer.view.userInteractionEnabled = true;
再去截获响应事件,但并没有得到点击的响应.
按照iOS的出来机制,用户的点击是肯定是被响应的,问题应该是没截获到.
一怒之下做了如下处理.
UIView *tapView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
[tapView setTag:101];
tapView.backgroundColor = [UIColor clearColor];
UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
[tapView addGestureRecognizer:tapGesturRecognizer];
[[self mainWindow] addSubview:tapView];
- (UIWindow *)mainWindow
{
UIApplication *app = [UIApplication sharedApplication];
if ([app.delegate respondsToSelector:@selector(window)])
{
return [app.delegate window];
}
else
{
return [app keyWindow];
}
}
-(void)tapAction:(id)tap
{
NSLog(@"tapView on touch");
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::PAUSED);
}
-(void) playStateChange
{
MPMoviePlaybackState state = [self.moviePlayer playbackState];
switch (state) {
case MPMoviePlaybackStatePaused:
[[[self mainWindow] viewWithTag:101] removeFromSuperview];
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::PAUSED);
break;
case MPMoviePlaybackStateStopped:
[[[self mainWindow] viewWithTag:101] removeFromSuperview];
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::STOPPED);
break;
case MPMoviePlaybackStatePlaying:
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::PLAYING);
break;
case MPMoviePlaybackStateInterrupted:
break;
case MPMoviePlaybackStateSeekingBackward:
break;
case MPMoviePlaybackStateSeekingForward:
break;
default:
break;
}
}
因为 iOS 事件响应顺序
UIApplication > UIWindow > UIView
所以在UIWindow 的最上多加了一个 UIView 响应用户点击.
【喜欢的点个赞,用得上的打个赏,拿钱去买猫粮。楼下的流浪猫在等我。】