gpt4 book ai didi

java - 录音机状态无效(文件路径错误?)

转载 作者:行者123 更新时间:2023-12-02 10:36:38 25 4
gpt4 key购买 nike

我有一个录音机,我正在尝试制作它来测试它并看看它是如何工作的。但是,当我运行它时,我收到错误:

E/MediaRecorder: start called in an invalid state: 4

所以我做了一些 googlejitzu 并发现我的文件路径是错误的。这是我的代码:

final MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("Test");

System.out.println("Hello");
try {
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();

}
recorder.start();
CountDownTimer countDownTimer = new CountDownTimer(30000, 1000) {
@Override
public void onTick(long l) {

}

@Override
public void onFinish() {
recorder.stop();
}
}.start();

所以我想我想知道如何在手机上创建一个文件路径目录来保存音频。

错误是:

    11-11 01:49:48.275 23703-23703/com.example.arege.dayatalisten W/System.err: java.io.FileNotFoundException: Test: open failed: EROFS (Read-only file system)
11-11 01:49:48.276 23703-23703/com.example.arege.dayatalisten W/System.err: at libcore.io.IoBridge.open(IoBridge.java:455)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:247)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:128)
at android.media.MediaRecorder.prepare(MediaRecorder.java:834)
at com.example.arege.dayatalisten.ListeningToTheWorld.onStartCommand(ListeningToTheWorld.java:47)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3432)
at android.app.ActivityThread.-wrap21(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1633)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6316)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)
11-11 01:49:48.277 23703-23703/com.example.arege.dayatalisten W/System.err: at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:187)
at libcore.io.IoBridge.open(IoBridge.java:441)
... 13 more

我拥有这两个必需的权限。

最佳答案

您正在尝试写入您的应用无权访问的内部存储。它不是 root 设备。

您需要将文件写入外部存储。

使用:getFilesDir() 或 getExternalFilesDir()(在上下文或 Activity 上)获取目录并将文件写入该位置。

伪代码:

File file = new File(context.getFilesDir(), "Test");
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write("contents".getBytes());
outputStream.close();

关于java - 录音机状态无效(文件路径错误?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53246190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com