问题
Samsung galaxy S6 edge 6.0.1 works fine, so maybe Android M is not the factor here.
First off, the Android 6.0+ permissions are given, so that's not the case .
Here it is :
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);
}
}
I've tested my app on other devices and it works fine, but on Nexus 5 (hammerhead) I've got an error when MediaRecorder.start() is called and I don't know much what to do with that.
The stacktrace is as simple as :
java.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method)
at com.vladdrummer.headsup.ScreenVideoRecorder.record(ScreenVideoRecorder.java:94)
So, not too much info. Here's the code, but keep in mind , it Works on other devices
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
}
}
Of course, it may be crashing on other devices as well, I tested it on 3 devices only
回答1:
For android M You need to add Runtime Permission like Camera,Audio
Try This it may be help to you
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
}
}
回答2:
try this:
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);
}
}
来源:https://stackoverflow.com/questions/37651356/android-m-mediarecorder-start-failed