Разрешение «MODIFY_AUDIO_SETTINGS» необходимо для изменения настроек звука.поэтому сначала объявите ниже разрешение в вашем файле манифеста:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
Ваш код также работает нормально и может записывать видео без звука с намерением захвата видео по умолчанию, но все же я публикую весь код активности ниже:
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btnRecord;
int REQUEST_CODE_TAKE_VIDEO = 201;
private final int MY_PERMISSIONS_REQUEST_FOR_VIDEO = 202;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
btnRecord = findViewById(R.id.btnRecord);
btnRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{android.Manifest.permission.RECORD_AUDIO,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.READ_PHONE_STATE,
android.Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
},
MY_PERMISSIONS_REQUEST_FOR_VIDEO);
} else {
takeVideo();
}
} else {
takeVideo();
}
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_FOR_VIDEO: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
takeVideo();
} else {
Toast.makeText(MainActivity.this, "Permissions Declined! Can't record video withon permissions. ", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
private void takeVideo() {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 3000);
startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO);
setMicMuted(true);
}
private void setMicMuted(Boolean state){
// AudioManager myAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
AudioManager myAudioManager = (AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
// get the working mode and keep it
int workingAudioMode = myAudioManager.getMode();
myAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
// change mic state only if needed
if (myAudioManager.isMicrophoneMute() != state) {
myAudioManager.setMicrophoneMute(state);
}
// set back the original working mode
myAudioManager.setMode(workingAudioMode);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try{
if (requestCode == REQUEST_CODE_TAKE_VIDEO) {
if (resultCode == RESULT_OK) {
Uri videoUri = data.getData();
Toast.makeText(this, "Video saved to:\n" +
videoUri, Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Video recording cancelled.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Failed to record video",
Toast.LENGTH_LONG).show();
}
}
}catch (Exception e){
Log.e("Unexpected error", "@video recording " + e.toString());
}
}
}