Android M - MediaRecorder启动失败

时间:2016-06-06 07:04:33

标签: android mediarecorder android-mediarecorder

三星galaxy S6 edge 6.0.1运行良好,所以也许Android M不是这里的因素。

首先,给出了Android 6.0+权限,因此情况并非如此。

这是:

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        //нужно проверять пермишны
        if (ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.CAMERA)
        != PackageManager.PERMISSION_GRANTED || 
        ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.RECORD_AUDIO)
        != PackageManager.PERMISSION_GRANTED ||
        ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE)
        != PackageManager.PERMISSION_GRANTED ||
        ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
        != PackageManager.PERMISSION_GRANTED ||
        ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.ACCESS_NETWORK_STATE)
        != PackageManager.PERMISSION_GRANTED ||
        ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.INTERNET)
        != PackageManager.PERMISSION_GRANTED) {


                ActivityCompat.requestPermissions(MainActivity.this,
                   new String[]{Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO,Manifest.permission.INTERNET,Manifest.permission.ACCESS_NETWORK_STATE,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        PERMISSIONS_REQUEST);           

        }
    }

我已经在其他设备上测试了我的应用程序并且工作正常,但是在Nexus 5(hammerhead)上我调用了MediaRecorder.start()并且我不知道时出错了该怎么做。

stacktrace就像:

一样简单
java.lang.RuntimeException: start failed.
    at android.media.MediaRecorder.start(Native Method)
    at com.vladdrummer.headsup.ScreenVideoRecorder.record(ScreenVideoRecorder.java:94)

所以,没有太多的信息。这是代码,但请记住,工作在其他设备上

private  Camera camera;
...
 camera.setPreviewDisplay(holder);
                  camera.startPreview();
                  isPrepared = prepareVideoRecorder();
..
    private boolean prepareVideoRecorder() {
        try{

        camera.unlock();

        mediaRecorder = new MediaRecorder();

        mediaRecorder.setCamera(camera);
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mediaRecorder.setProfile(CamcorderProfile
            .get(CamcorderProfile.QUALITY_HIGH));
        String path = A.getRawGameVideoPath();//4 debug purposes
        mediaRecorder.setOutputFile(path);
        mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }
        try {
          mediaRecorder.prepare();
        } catch (Exception e) {
          e.printStackTrace();
          release();
          return false;
        }
        return true;
      }

      public void record(){
          if (isPrepared){        
          mediaRecorder.start(); // Here where it crashes on Android M
          }
      }

当然,它也可能在其他设备上崩溃,我只在3台设备上测试

2 个答案:

答案 0 :(得分:0)

对于Android M您需要添加像Camera,Audio这样的运行时权限 试试这可能对你有所帮助

private static final int MY_PERMISSIONS_REQUEST = 11;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (mContext.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO},
            MY_PERMISSIONS_REQUEST);
} else {
    Log.d("TAG", "Already granted access");
    initializeView(v);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
    case MY_PERMISSIONS_REQUEST: {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Log.d("TAG", "Permission Granted");
            initializeView(v);
        } else {
            Log.d("TAG", "Permission Failed");
            Toast.makeText(getActivity().getBaseContext(), "You must allow permission record audio to your mobile device.", Toast.LENGTH_SHORT).show();
            getActivity().finish();
        }
    }
    // Add additional cases for other permissions you may have asked for
}
  }

答案 1 :(得分:-2)

试试这个:

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.CAMERA)
    != PackageManager.PERMISSION_GRANTED || 
    ContextCompat.checkSelfPermission(this,
            Manifest.permission.RECORD_AUDIO)
    != PackageManager.PERMISSION_GRANTED ||
    ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_EXTERNAL_STORAGE)
    != PackageManager.PERMISSION_GRANTED ||
    ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
    != PackageManager.PERMISSION_GRANTED ||
    ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_NETWORK_STATE)
    != PackageManager.PERMISSION_GRANTED ||
    ContextCompat.checkSelfPermission(this,
            Manifest.permission.INTERNET)
    != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(this,
               new String[]{Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO,Manifest.permission.INTERNET,Manifest.permission.ACCESS_NETWORK_STATE,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    0);
    }
}