我想使用默认视频录制意图ACTION_VIDEO_CAPTURE
我正在尝试遵循以下代码以实现静音目的,但是它不起作用
private void setMicMuted(boolean state){
AudioManager myAudioManager = (AudioManager)mContext.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);
}
对此有任何想法或任何可用的第三方库或演示吗?
或我有第二种选择,例如在录制视频后删除声音,例如快速聊天。有没有可能?还是我可以实现相同的功能,例如快速聊天?
请帮助
答案 0 :(得分:0)
此代码有效,但是您能告诉我您在主要活动中针对布尔状态发送的内容吗?
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);
}
这是您的代码兄弟...
答案 1 :(得分:0)
必须具有“ 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());
}
}
}