我的录音应用无法开始录音

My Audio Recording App Is Failing To Start Recording

当我尝试录制音频时,我的应用程序抛出 IOException。 java.io.FileNotFoundException:/storage/emulated/0/recording.3gp:打开失败:EACCES(权限被拒绝)。它发生在我的 recordButton.SetOnClickListener 方法中,更具体地说,发生在我代码的 try-catch 部分。我真的不知道为什么它会失败,但我觉得它与我的 outputFile 字符串有关。 这是我的 JAVA 文件:

    package com.example.audiorecorder;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

Button playBtn, recordBtn, stopBtn;

String outputFile;

MediaRecorder mediaRecorder;

private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;

private boolean permissionToRecordAccepted = false;
private String [] permissions = {Manifest.permission.RECORD_AUDIO};

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull         int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode){
        case REQUEST_RECORD_AUDIO_PERMISSION:
            permissionToRecordAccepted  = grantResults[0] == PackageManager.PERMISSION_GRANTED;
            break;
    }
    if (!permissionToRecordAccepted ) finish();

}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION);

    outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";

    playBtn = findViewById(R.id.play_btn);
    recordBtn = findViewById(R.id.record_btn);
    stopBtn = findViewById(R.id.stop_btn);

    mediaRecorder = new MediaRecorder();

    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    mediaRecorder.setOutputFile(outputFile);


    recordBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try{
                mediaRecorder.prepare();
                mediaRecorder.start();
                Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
                System.out.println("IT TRIED RECORDING");
            } catch (IllegalStateException ise){
                System.out.println("FAIL RECORDING 1");
            } catch (IOException ioe){
                System.out.println("FAIL RECORDING 2");
            }

            System.out.println("ITS JUST PRESSING RECORD BUT NOTHING");

        }
    });

    stopBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try{
                mediaRecorder.stop();
                mediaRecorder.release();
                mediaRecorder = null;
                System.out.println("IT TRIED STOPPING");

            } catch (Exception e){
                System.out.println("FAIL STOP 1");
            }

            Toast.makeText(getApplicationContext(), "Stop Recording", Toast.LENGTH_LONG).show();

        }
    });

    playBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MediaPlayer mediaPlayer = new MediaPlayer();
            try {
                mediaPlayer.setDataSource(outputFile);
                mediaPlayer.prepare();
                mediaPlayer.start();
                Toast.makeText(getApplicationContext(), "Playing Audio", Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                // make something
            }
        }
    });

}
}

这是我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="40dp"
    android:textStyle="bold"
    android:text="RECORDER TEST LAB"
    android:textColor="#000000"
    android:textSize="24dp"
    />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:background="#000000"
    android:layout_marginTop="10dp"
    >

<Button
    android:id="@+id/record_btn"
    android:layout_height="80dp"
    android:layout_width="match_parent"
    android:textSize="22dp"
    android:background="#E70D0D"
    android:text="record"
    android:textStyle="bold"

    >

</Button>

</RelativeLayout>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:background="#000000"
    android:layout_marginTop="10dp"
    >

    <Button
        android:id="@+id/stop_btn"
        android:layout_height="80dp"
        android:layout_width="match_parent"
        android:textSize="22dp"
        android:background="#E70D0D"
        android:text="stop"
        android:textStyle="bold"

        >

    </Button>

</RelativeLayout>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:background="#000000"
    android:layout_marginTop="10dp"
    >

    <Button
        android:id="@+id/play_btn"
        android:layout_height="80dp"
        android:layout_width="match_parent"
        android:text="Play"
        android:textSize="22dp"
        android:background="#35E70D"
        android:textStyle="bold"
        />

</RelativeLayout>

这是我的 android 清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.audiorecorder">

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

<application
android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

提前感谢您的帮助。我会投票给一个对我有帮助的答案!

您忘记也为 WRITE_EXTERNAL_STORAGE 请求权限。

String[] permissions = { Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE };
ActivityCompat.requestPermissions(this, permissions, PERMISSIONS_REQUEST_CODE);