com.sinch.android.rtc.MissingPermissionException:需要权限:android.permission.RECORD_AUDIO

时间:2016-11-17 12:33:29

标签: android

我已经在清单中添加了 record_audio权限,但仍然存在错误..请帮助!!

com.sinch.android.rtc.MissingPermissionException: Requires permission: android.permission.RECORD_AUDIO
    at com.sinch.android.rtc.internal.client.calling.DefaultCallClient.throwIfMissingPermission(DefaultCallClient.java:412)
    at com.sinch.android.rtc.internal.client.calling.DefaultCallClient.call(DefaultCallClient.java:150)
    at com.sinch.android.rtc.internal.client.calling.DefaultCallClient.callUser(DefaultCallClient.java:102)
    at com.sinch.android.rtc.internal.client.calling.DefaultCallClient.callUser(DefaultCallClient.java:97)
    at com.example.ram.dummy.CallActivity$1.onClick(CallActivity.java:45)
    at android.view.View.performClick(View.java:5697)
    at android.widget.TextView.performClick(TextView.java:10826)
    at android.view.View$PerformClick.run(View.java:22526)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7224)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

1 个答案:

答案 0 :(得分:1)

检查manifest.xml中是否添加了权限。如果您没有检查权限。请添加权限。我希望它能起作用

  <uses-permission android:name="android.permission.RECORD_AUDIO" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

否则我建议检查以下代码,以获取录制音频并检查运行时权限:

 public static final int RequestPermissionCode = 1;

检查Audio RECORD的权限并开始录制'

 if(checkPermission()) {

           AudioSavePathInDevice = 
              Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + 
                 CreateRandomAudioFileName(5) + "AudioRecording.3gp";

           MediaRecorderReady();

           try {
              mediaRecorder.prepare();
              mediaRecorder.start();
           } catch (IllegalStateException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
           } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
           }

           buttonStart.setEnabled(false);
           buttonStop.setEnabled(true);

           Toast.makeText(MainActivity.this, "Recording started", 
              Toast.LENGTH_LONG).show();
        } else {
           requestPermission();
        }

   private void requestPermission() {
     ActivityCompat.requestPermissions(MainActivity.this, new 
     String[]{WRITE_EXTERNAL_STORAGE, RECORD_AUDIO}, RequestPermissionCode);
   }

   @Override
   public void onRequestPermissionsResult(int requestCode, 
     String permissions[], int[] grantResults) {
     switch (requestCode) {
       case RequestPermissionCode:
          if (grantResults.length> 0) {
            boolean StoragePermission = grantResults[0] == 
            PackageManager.PERMISSION_GRANTED;
            boolean RecordPermission = grantResults[1] == 
           PackageManager.PERMISSION_GRANTED;

        if (StoragePermission && RecordPermission) {
            Toast.makeText(MainActivity.this, "Permission Granted", 
              Toast.LENGTH_LONG).show();
          } else {
            Toast.makeText(MainActivity.this,"Permission 
              Denied",Toast.LENGTH_LONG).show();
         }
      }
     break;
   }
 }

 public boolean checkPermission() {
   int result = ContextCompat.checkSelfPermission(getApplicationContext(), 
     WRITE_EXTERNAL_STORAGE);
   int result1 = ContextCompat.checkSelfPermission(getApplicationContext(), 
     RECORD_AUDIO);
   return result == PackageManager.PERMISSION_GRANTED && 
     result1 == PackageManager.PERMISSION_GRANTED;
 }

有关详细信息,请参阅此Android - Audio Capture

我希望它能正常工作。