草庐IT

android - 为什么 MediaRecorder 函数不会导致 ANR 错误?

coder 2023-11-26 原文

我在 Android 5.1 中使用 MediaRecorder 和 MediaProjection 函数启动了一个重新记录屏幕的服务,我认为代码方法 1 会导致应用程序无响应错误,因为它在主线程中工作。

  1. 我测试代码方法1重新编码屏幕很长时间,它没有出现“应用程序无响应错误”,为什么?这是否意味着函数 MediaRecorder 和 MediaProjection 在单独的线程中工作?

  2. 在代码方法 2 中,我创建了一个线程来运行 mRecordHelper.StartRecord(mRecordArg,resultCode,mIntent);但我收到错误 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare(), why?

感谢您的帮助。

调用代码

MPublicPar.RecordArg mRecordArg =new MPublicPar().new RecordArg(mContext);
Intent intent = new Intent(mContext,bll.RecordService.class);
intent.putExtra("resultCode",resultCode);
intent.putExtra("dataIntent",data);
intent.putExtra("mRecordArg",mRecordArg);

startService(intent);

方法一

public class RecordService extends Service { 

    private RecordHelper mRecordHelper;
    private Context mContext;

    @Override
    public void onCreate(){  
        mContext=this;

        mRecordHelper=new RecordHelper(mContext);
    }

    @Override
    public void onDestroy(){
        mRecordHelper.StopRecord();
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

       final int resultCode=intent.getIntExtra("resultCode",0);
       final Intent mIntent=(Intent)intent.getParcelableExtra("dataIntent");
       final MPublicPar.RecordArg mRecordArg=(MPublicPar.RecordArg)intent.getSerializableExtra("mRecordArg");

        mRecordHelper.StartRecord(mRecordArg,resultCode,mIntent);
        return super.onStartCommand(intent, flags, startId);
    }

}

方法二

public class RecordService extends Service { 

    private RecordHelper mRecordHelper;
    private Context mContext;

    @Override
    public void onCreate(){  
        mContext=this;

        mRecordHelper=new RecordHelper(mContext);
    }

    @Override
    public void onDestroy(){
        mRecordHelper.StopRecord();
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

       final int resultCode=intent.getIntExtra("resultCode",0);
       final Intent mIntent=(Intent)intent.getParcelableExtra("dataIntent");
       final MPublicPar.RecordArg mRecordArg=(MPublicPar.RecordArg)intent.getSerializableExtra("mRecordArg");    

       new Thread(new Runnable() {
            public void run() {    
                mRecordHelper.StartRecord(mRecordArg,resultCode,mIntent);           
            }
        }).start();
        return super.onStartCommand(intent, flags, startId);
    }

}

RecordHelper.cs

public class RecordHelper {

    private  MediaRecorder mMediaRecorder;
    private  MediaProjection mMediaProjection;
    private  VirtualDisplay mVirtualDisplay;
    private  MediaProjectionManager mProjectionManager;

    private Context mContext;
    private Toast mToastText;

    public RecordHelper(Context mContext){
        this.mContext=mContext;
        mProjectionManager = (MediaProjectionManager) mContext.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        mMediaRecorder = new MediaRecorder();
    }

    public void StartRecord(RecordArg mRecordArg, int resultCode, Intent data){

        initRecorder(mRecordArg);
        prepareRecorder();

        mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
        MediaProjectionCallback mMediaProjectionCallback = new MediaProjectionCallback();
        mMediaProjection.registerCallback(mMediaProjectionCallback, null);

        mVirtualDisplay=createVirtualDisplay(mRecordArg);

        DelayStartRecord(mRecordArg);
    }


    public void StopRecord(){
        try {
            mMediaRecorder.stop();
            mMediaRecorder.reset();

            mVirtualDisplay.release();
            mMediaRecorder.release();

            mMediaProjection.stop();
            mMediaProjection = null;

        }catch (Exception e){
            Utility.LogError("StopRecord Error " + e.getMessage() + "  " + e.toString());
        }
    }

    private void DelayStartRecord(RecordArg mRecordArg){
        mMediaRecorder.start();
    }


    private void initRecorder(RecordArg mRecordArg) {
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
        mMediaRecorder.setVideoFrameRate(30);
        mMediaRecorder.setVideoSize(mRecordArg.screenWidth, mRecordArg.screenHeight);
        mMediaRecorder.setOutputFile(mRecordArg.videoFilename);
    }


    private void prepareRecorder() {
        try {
            mMediaRecorder.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
            Utility.LogError(e.getMessage());

        } catch (IOException e) {
            e.printStackTrace();
            Utility.LogError(e.getMessage());
        }
    }


    private VirtualDisplay createVirtualDisplay(RecordArg mRecordArg) {
          return mMediaProjection.createVirtualDisplay("ScreenRecord",
                 mRecordArg.screenWidth, mRecordArg.screenHeight, mRecordArg.mScreenDensity,
                 DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                 mMediaRecorder.getSurface(), null /*Callbacks*/, null /*Handler*/);
    }


    //Called when the MediaProjection session is no longer valid.
    private  class MediaProjectionCallback extends MediaProjection.Callback {
        @Override
        public void onStop() {

        }
    }

}

最佳答案

but I get the error java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare(), why?

我猜你知道你的第二个问题。事实上,如果您在主线程上调用 mRecordHelper.StartRecord(mRecordArg,resultCode,mIntent);,并不意味着所有代码函数都在该线程上运行,它所做的只是更新 UI 信息调用线程——这是主线程,在后台线程上努力工作。如果您明确地从不同的线程调用,那么您是在指示它从该线程更改 UI 对象,因此您会得到该异常-想象一个使用异步任务或 SurfaceView 的类,先生,请不要感到困惑,您可以随时去查看源代码,看看它是如何工作的。

这没什么大不了的-(我谦虚地说)

why? Does it mean that the function MediaRecorder and MediaProjection worked in separated thread?

检查上面的-我猜是

关于android - 为什么 MediaRecorder 函数不会导致 ANR 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33541279/

有关android - 为什么 MediaRecorder 函数不会导致 ANR 错误?的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  3. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  4. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  5. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  6. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  7. ruby - Highline 询问方法不会使用同一行 - 2

    设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案

  8. ruby - ruby 中的 TOPLEVEL_BINDING 是什么? - 2

    它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput

  9. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  10. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

随机推荐