EasyMovieTexture视频流播放错误问题

关于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");
 }
                ```
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值