gpt4 book ai didi

java - 无法保存音频文件,文件夹为空

转载 作者:行者123 更新时间:2023-12-03 02:19:11 26 4
gpt4 key购买 nike

我正在尝试开发一个录音应用程序,在其中我可以启动和停止我的录音。从逻辑上讲,它应该使用保存的音频创建一个音频文件。不幸的是,当我检查文件时,没有任何音频。因此,我不太确定代码中哪里有错误。

这是我的主要录音机类(class)。

    package com.example.soundrecorder;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class SoundRecorder extends Activity {

MediaRecorder recorder;
File audiofile = null;
private static final String TAG = "SoundRecordingActivity";
private View startButton;
private View stopButton;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startButton = findViewById(R.id.start);
stopButton = findViewById(R.id.stop);
}

public void startRecording(View view) throws IOException {

startButton.setEnabled(false);
stopButton.setEnabled(true);

File sampleDir = Environment.getExternalStorageDirectory();
try {
audiofile = File.createTempFile("sound", ".3gp", sampleDir);
} catch (IOException e) {
Log.e(TAG, "sdcard access error");
return;
}
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audiofile.getAbsolutePath());
recorder.prepare();
recorder.start();
}

public void stopRecording(View view) {
startButton.setEnabled(true);
stopButton.setEnabled(false);
recorder.stop();
recorder.release();
addRecordingToMediaLibrary();
}

protected void addRecordingToMediaLibrary() {
ContentValues values = new ContentValues(4);
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
ContentResolver contentResolver = getContentResolver();

Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
}
}

有我的录音机 list
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.soundrecorder"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />

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

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SoundRecorder"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>

最佳答案

首先,您需要定义:

MediaRecorder mrec = new MediaRecorder();
File sampleDir = Environment.getExternalStorageDirectory();
File audiofile = File.createTempFile("ibm", ".mp4", sampleDir);

之后,您需要定义FileProcessing函数:
protected void processaudiofile()
{
ContentValues values = new ContentValues(3);
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
//values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/mp4");
values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
ContentResolver contentResolver = getContentResolver();

Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
}

同时使用它们,并确保一切都将正常工作。

晚安。

关于java - 无法保存音频文件,文件夹为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16270098/

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