无法在网络视图中录制语音

时间:2019-06-28 12:43:31

标签: android webview audio-recording

在Android网络视图内部,尽管先请求权限,但从调用navigator.mediaDevices.getUserMedia时,我始终收到PermissionDenied错误。

我的过程始于对下面的StartRecordAudio函数的调用,如果在Android中工作,则CheckPermissionFirst参数设置为true。

var AudioRecorder ; // recorder
var RawAudioDataArray ; // raw recorded data
var AudioSaveFormat = "audio/wav" ; // waveform audio - alternatives "audio/mpeg-3" and "audio/webm" ;
var AudioStartCallback ; // function to be called at start of input
var AudioErrorCallback ; // function to be called if audio cannot be recorded (no capability orno permission)
var AudioDoneCallback ; // function to be called at end of input

function StartRecordAudio (CheckPermissionFirst,StartCallback,ErrorCallback,DoneCallback) 
{
AudioStartCallback = StartCallback ; 
AudioErrorCallback = ErrorCallback ; 
AudioDoneCallback  = DoneCallback ; 
RawAudioDataArray = [] ;
if (AudioRecorder)
    {
    StartRecording ();
    }
else if (CheckPermissionFirst)
    {
    CheckAppMicrophonePermission ()
    } 
else
    {
    navigator.mediaDevices.getUserMedia ({audio:true}).then (AudioDeviceFound).catch (AudioErrorCallback); 
    };
};

CheckAppMicrophonePermission如下

function CheckAppMicrophonePermission ()
{
AndroidBridge.call ("CheckMicrophonePermission" , []);
};

跳过AndroidBridge的详细信息,此调用在Android包装器代码中触发以下Java函数

if ("CheckMicrophonePermission".equals(command))
    {
    if (ContextCompat.checkSelfPermission (MainActivity.this, android.Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED)
        {
        ActivityCompat.requestPermissions (MainActivity.this, new String[]{android.Manifest.permission.RECORD_AUDIO}, requestMicrophoneId );
        }
    else
        {
        MicrophonePermissionsOK ();
        }
    }

在询问完用户后将呼叫

public void onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults)
    {
    switch (requestCode)
        {
        case requestMicrophoneId:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                {
                MicrophonePermissionsOK();
                break;
                }
            else if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE))
                {
                MicrophonePermissionsNOTOK(false);
                }
            else
                {
                MicrophonePermissionsNOTOK(true);
                };
            break;
        }
    }

功能MicrophonePermissionsOK如下:

public void MicrophonePermissionsOK ()
{
String jsString = "AppMicrophonePermissionGranted()";
mWebView.evaluateJavascript (jsString);
}

回到Android Bridge的原始端

function AppMicrophonePermissionGranted ()
{
AndroidMicrophonePermission = true ; 
ShowAlert ("Please select OK to activate the app microphone link");
AlertCallback = function () {HideAlert (); AudioPermissionGranted ()};
};

最后,当用户确定我的自定义警报对话框时(我认为此功能的详细信息与此处无关),我们进入:

function AudioPermissionGranted ()
{
navigator.mediaDevices.getUserMedia ({audio:true}).then (AudioDeviceFound).catch (AudioErrorCallback); 

};

对于任何有兴趣的人,诺言成功之路是

function AudioDeviceFound (Stream)
{
AudioRecorder = new MediaRecorder (Stream);
AudioRecorder.ondataavailable = function (e)
    {
    RawAudioDataArray.push (e.data);
    if (AudioRecorder.state == "inactive") // recorder has been stopped either by user or after max seconds
        {
        var blob = new Blob (RawAudioDataArray, {type:AudioSaveFormat});
        var reader = new FileReader ();
        reader.onload = function () {AudioDoneCallback (reader.result.split (',')[1])};
        reader.readAsDataURL (blob);
        };    
    };
StartRecording ();
};

function StartRecording ()
{
AudioStartCallback ();
AudioRecorder.start (1000); // record in 1 second chunks
SetAudioTimeout ();
};

function SetAudioTimeout ()
{
AudioTimeout = window.setTimeout (function () {AudioRecorder.stop(); 
AudioTimeout = null}, MaxAudioSeconds * 1000);
};

所有这些代码都可以在Chrome和Chrome打包应用中正常运行。

但是,我已经在Android Webview中跟踪了代码,并且在调用getUserMedia承诺之前,我一直通过所有代码获得正确的操作序列。

这是新加载的应用程序或现有加载的应用程序的情况。同样,如果之前从未请求过权限,拒绝了请求权限以及授予了权限,则都可以直接通过设置应用程序中上方显示的权限对话框进行操作。

但是(在Android Webview中),调用getUserMedia promise总是会触发捕获条件,错误参数名称设置为PermissionDenied,消息设置为空字符串。

请有人帮我吗?

0 个答案:

没有答案