当前位置: 首页 > news >正文

兰溪高端网站建设公司百度搜索网址大全

兰溪高端网站建设公司,百度搜索网址大全,网站建设需要保存什么,做网站先做前台还是后台0,前言 工作需要使用C制作一个ue4的视频插件,其中一个功能是能够选择 运行时是否自动播放 视频的功能。 在实现时遇见了一个问题,取消自动播放之后,运行时首帧是没有取到的,在场景里面看是黑色的。就这个问题我想到了使…

0,前言

        工作需要使用C++制作一个ue4的视频插件,其中一个功能是能够选择 运行时是否自动播放 视频的功能。

        在实现时遇见了一个问题,取消自动播放之后,运行时首帧是没有取到的,在场景里面看是黑色的。就这个问题我想到了使用了一个线程去监控纹理渲染,渲染到第一帧,就暂停,这样的效果看起来就是取消自动播放时,视频插件在场景中显示视频的第一帧。插件的代码不方便贴出来,我这里只贴线程监控的代码。

        大部分都是使用蓝图和UE自带的mediaplayer实现视频播放的方案,直接使用UE的mediaplayer,他在给plane附加纹理的时候,会自动生成一个当前视频帧的材质,所以看不到黑帧。但是其实在打开ue第一次运行的时候,还没有读取视频,视频帧的材质也是黑帧。

        中间尝试了在mediaplayer中添加回调,在MediaOpened事件中调用play(),然后调用seek()函数,在SeekCompleted事件中调用pause()函数,来做到播放第一帧的效果,但是play()函数只是设置rate为1,渲染第一帧的工作是别的线程实现的,这也就导致,在SeekCompleted事件中调用pause()函数时,另一个线程还没有渲染第一帧,从而运行时还是显示黑帧。

1,原理

        查看了mediaplayer的源码,大概捋了一下它的流程,下面是流程图,分了两个线程,一个向队列里面送buffer,一个从队列里面取buffer之后送去渲染

         这只是其中一部分和mediaplayer有关的线程,中间还有很多流程。但我们只需要了解到这个线程是怎么运行的就能够解决目前的问题。

        大家可以去贴出来的函数里面打个断点跟一下下流程,大概了解一下就行。

        主要看一下第二天取buffer的线程,也就是红框中的流程,有下面的代码:

				while (SampleQueue->Dequeue(Sample));if (!Sample.IsValid()){// Player is active (do not clear), but we have no new data// -> we do not need to trigger anything on the renderthreadreturn;}UpdateSampleInfo(Sample);RenderParams.TextureSample = Sample;RenderParams.Rate = CurrentPlayerPtr->GetRate();RenderParams.Time = Sample->GetTime();

        其中最后一行代码 Sample->GetTime(); 这个GetTime()是纹理中player的时间,我猜是记录播放时长,我们利用这个来实现监控纹理。

        原理就是获取视频的帧率,从而获取每帧播放的时长,启动一个线程,监控纹理的player的播放时长,在大于每帧播放时间的时候就暂停掉,从而实现第一帧暂停的效果。

2,实现

        多线程的时候代码参考(照抄)了这位博主的:

        UE4 C++ 子线程的创建及使用_ue4 启动线程_北极熊的奋斗史的博客-CSDN博客

头文件:

class RNGThread : public FRunnable
{
public://ConstructorRNGThread(int Count = 50000, int minNumber = 0, int maxNumber = 1000, int chunkCount = 20);//Destructor~RNGThread();//	杀死线程,该线程将不能再使用,想再开启的话,需要重新创建//Use this method to kill the thread!!void EnsureCompletion();//	暂停线程//Pause the thread void PauseThread();//	继续线程//Continue/UnPause the threadvoid ContinueThread();//	当前线程是否处于暂停状态bool IsThreadPaused();bool setMediaPlayer(UMediaPlayer* MediaPlayer);bool setVideoComponent(UPXVideoComponent* PXVideoComponent);protected://FRunnable interface.virtual bool Init();virtual uint32 Run();virtual void Stop();private://Thread to run the worker FRunnable onFRunnableThread* Thread;UMediaPlayer* MediaPlayer = nullptr;UPXVideoComponent* PXVideoComponent = nullptr;FCriticalSection m_mutex;		//	线程锁FEvent* m_semaphore;			//	信号量int m_chunkCount;int m_amount;int m_MinInt;int m_MaxInt;//As the name states those members are Thread safeFThreadSafeBool m_Kill;		//	bool 类型的变量,线程安全FThreadSafeBool m_Pause;};

源文件:

RNGThread::RNGThread(int Count, int minNumber, int maxNumber, int chunkCount)
{m_Kill = false;m_Pause = false;//Initialize FEvent (as a cross platform (Confirmed Mac/Windows))m_semaphore = FGenericPlatformProcess::GetSynchEventFromPool(false);			//	信号量m_MinInt = minNumber;m_MaxInt = maxNumber;m_chunkCount = chunkCount;//	启动线程Thread = FRunnableThread::Create(this, TEXT("RNGThread"), 0, TPri_BelowNormal);
}RNGThread::~RNGThread()
{if (m_semaphore){//Cleanup the FEventFGenericPlatformProcess::ReturnSynchEventToPool(m_semaphore);m_semaphore = nullptr;}if (Thread){//Cleanup the worker threaddelete Thread;Thread = nullptr;}
}bool RNGThread::Init()
{//Init the Data return true;
}/***************************************************************************************/
/*注意:不要在线程中做 spawning / modifying / deleting UObjects / AActors 等等之类的事 */
/***************************************************************************************/uint32 RNGThread::Run()
{//	等待一下初始化//Initial wait before startingFPlatformProcess::Sleep(0.03);//	判断是否停止了线程while (!m_Kill){//	判断当前是否处于暂停状态if (m_Pause){//使用信号量使线程处于睡眠状态,直到被唤醒m_semaphore->Wait();if (m_Kill){return 0;}}else{if(MediaPlayer != nullptr){float framerate = MediaPlayer->GetVideoTrackFrameRate(0,0);FTimespan FrameTime = FTimespan::FromSeconds(1 / (FMath::IsNearlyZero(framerate) ? -1 : framerate));if (MediaPlayer->GetTime() <= FrameTime){while ((MediaPlayer->GetTime() <= FrameTime) && !m_Pause);MediaPlayer->Pause();break;}}}FPlatformProcess::Sleep(0.01);}return 0;
}void RNGThread::PauseThread()
{m_Pause = true;
}void RNGThread::ContinueThread()
{m_Pause = false;//	启动线程if (m_semaphore){//Here is a FEvent signal "Trigger()" -> it will wake up the thread.m_semaphore->Trigger();}
}void RNGThread::Stop()
{//	设置停止的标志m_Kill = true; //Thread kill condition "while (!m_Kill){...}"m_Pause = false;//	触发一下线程,让其在下一次判断中退出if (m_semaphore){//We shall signal "Trigger" the FEvent (in case the Thread is sleeping it shall wake up!!)m_semaphore->Trigger();}
}//Use this method to kill the thread!!
void RNGThread::EnsureCompletion()
{//	停止线程Stop();//	等待线程运行结束if (Thread){Thread->WaitForCompletion();}if (this->MediaPlayer != nullptr){MediaPlayer = nullptr;}if (this->PXVideoComponent != nullptr){this->PXVideoComponent->UnregisterComponent();this->PXVideoComponent->DestroyComponent();this->PXVideoComponent = nullptr;}
}bool RNGThread::IsThreadPaused()
{return (bool)m_Pause;
}bool RNGThread::setMediaPlayer(UMediaPlayer* pMediaPlayer)
{this->MediaPlayer = pMediaPlayer;return true;
}
bool RNGThread::setVideoComponent(UPXVideoComponent* pPXVideoComponent)
{	this->PXVideoComponent = pPXVideoComponent;return true;
}

使用方法:

启动线程:
RNGThread * pThread = new RNGThread();
pThread->setMediaPlayer(MediaPlayer);关闭线程:
if (pThread != nullptr)
{pThread->EnsureCompletion();delete pThread;pThread = nullptr;
}

        

 

http://www.shuangfujiaoyu.com/news/61445.html

相关文章:

  • java做简易网站相似图片在线查找
  • 珠海科技网站建设电商网
  • ppt可以做网站百度在线客服系统
  • 温州开发网站公司长沙网站seo公司
  • 涂料 网站 源码小游戏推广接单平台
  • 做关于灯饰的网站百度站长工具网站提交
  • 周口网站建设关键词查网址
  • 如何做网站清风制作移动优化课主讲:夫唯老师
  • 做优化网站能以量取胜么seo搜索引擎优化兴盛优选
  • 制作单页网站教程北京百度推广优化公司
  • 电子商务网站建设期末试题08答案网络营销期末总结
  • 全国做网站的公司有哪些百度首页广告
  • 网站建设 自适应网站优化推广seo公司
  • html动态网站怎么做手游推广平台代理
  • wordpress按钮西安seo网络推广
  • 优购物官方网站今日直播喜来健山东建站管理系统
  • 美国惠尔润滑油官方网站什么企业需要网络营销和网络推广
  • 外贸网站搭建用哪个平台比较好网站推广平台搭建
  • 营销型网站北京爱站网seo综合查询工具
  • 私人网站建设成本百度健康人工客服电话24小时
  • 手机端网站首页怎么做百度app下载官方
  • php做电子商城网站百度公司官方网站
  • 洛阳网站建设多少钱兰州网络seo公司
  • 加强政府网站信息内容建设措施站长工具友链检测
  • 手表网站哪个好大众网疫情最新消息
  • 大连零基础网站建设教学公司自己搭建网站
  • 海南住房和城乡建设厅网站首页it培训学校哪家好
  • 信息化建设seo岗位是什么意思
  • 福州台江区网站建设太原网站快速排名提升
  • 北京网站建设公司分形科技免费引流微信推广