关于EasyMovieTexture视频流播放错误造成Unity卡顿或闪退问题解决
博主在学习过程中遇到一个播放RTSP视频流的功能,于是遍览群书,查遍各大博客,终于皇天不负有心人,找到了两种实现方式
1.使用unity自带的UMP进行播放(支持PC,安卓端材质莫名丢失,目前未解决)
2.使用EasyMovieTexture插件进行播放(支持安卓,PC,IOS未测试)
今天着重讲一下在使用EasyMovieTexture过程中所遇到的问题(视频流正常,但是在没网的情况下导致线程未及时关闭,unity闪退)
上述所说这种情况和视频流地址失效同理
针对EasyMovieTexture里面loader线程的Thread.Abort()未关闭情况,我做了很多尝试,但是未找到原因,有大牛发现的话,敬请指导
言归正传,最终原因并非是线程的原因,而是ffmpeg设置avformat_open_input( )超时的原因
贴出部分代码
if (loader == null)
{
loader = new Thread(() => {
AVFormatContext* ppFomatContext = null;
if (ffmpeg.avformat_open_input(&ppFomatContext, strFileName, null, null) != 0)
{
pFormatContext = null;
m_CurrentState = MEDIAPLAYER_STATE.ERROR;
Debug.Log("Could not open file");
loader = null;
throw new ApplicationException(@"Could not open file");
}
//ffmpeg.av_dict_free(&opts);
pFormatContext = ppFomatContext;
if (ffmpeg.avformat_find_stream_info(pFormatContext, null) != 0)
{
m_CurrentState = MEDIAPLAYER_STATE.ERROR;
Debug.Log("Could not find stream info");
loader = null;
throw new ApplicationException(@"Could not find stream info");
}
AddActionForUnityMainThread(() => {
LoadVideoPart2();
});
});
}
FFmpeg超时处理可参见博客:https://www.cnblogs.com/wainiwann/p/4218341.html
解决办法:在此段线程内加入,代码如下:
AVDictionary* opts = null;
ffmpeg.av_dict_set(&opts, "rtsp_transport", "tcp", 0);//采用tcp传输(默认udp)
ffmpeg.av_dict_set(&opts, "stimeout", "200000", 0);
ffmpeg.av_dict_set(&opts, "probesize", "50000 ", 0);
ffmpeg.av_dict_set(&opts, "analyzeduration", "10000 ", 0);
if (ffmpeg.avformat_open_input(&ppFomatContext, strFileName, null, null) != 0)
判断修改为
if (ffmpeg.avformat_open_input(&ppFomatContext, strFileName, null, &opts) != 0)
在判断逻辑内加入
if (ffmpeg.avformat_open_input(&ppFomatContext, strFileName, null, &opts) != 0)
{
pFormatContext = null;
m_CurrentState = MEDIAPLAYER_STATE.ERROR;
Debug.Log("Could not open file");
loader.Abort(); //在未解析此段视频流的情况下 将此段线程终止
throw new ApplicationException(@"Could not open file");
}
```