我正在开发一些具有录音功能的应用程序,我只想将其保存在我的外部存储器上,现在我知道如何在没有文件夹的情况下保存它但我希望我的应用程序为我录制的文件创建唯一的文件夹< / p>
我的代码:
public class MainActivity extends Activity {
MediaRecorder recorder = null;
MediaPlayer player = null;
public static String OUTPUT_FILE;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OUTPUT_FILE = Environment.getExternalStorageDirectory()+"audiorecorder.acc";
}
public void record(View v) {
try {
beginRecording();
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
}
public void stop(View v) {
stopRecording();
}
public void play(View v) {
try {
beginPlay();
} catch (IllegalArgumentException | SecurityException
| IllegalStateException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stopPlayback(View v) {
StopPlay();
}
private void beginRecording() throws IllegalStateException, IOException {
dictchMediaRecorder();
File file = new File(OUTPUT_FILE);
if(file.exists()){
file.delete();
}
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
recorder.setAudioEncodingBitRate(9600);
recorder.setAudioSamplingRate(44100);
recorder.setOutputFile(OUTPUT_FILE);
recorder.prepare();
recorder.start();
}
private void beginPlay() throws IllegalArgumentException, SecurityException, IllegalStateException, IOException {
ditchMediaPlayer();
player = new MediaPlayer();
player.setDataSource(OUTPUT_FILE);
player.prepare();
player.start();
}
我只添加了这一行,并且它的工作非常完美:
File dir_image;
public static String OUTPUT_FILE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dir_image = new File(Environment.getExternalStorageDirectory()+File.separator+"folderName");
dir_image.mkdirs();
OUTPUT_FILE = dir_image.getPath()+"/acapella.wav";
答案 0 :(得分:0)
添加:
private File dir_image;
String myfile="the name of the song.acc";
dir_image = new File(Environment.getExternalStorageDirectory()+
File.separator+"The name of your folder");
dir_image.mkdirs();
File tmpFile = new File(dir_image,myfile);
我想补充一点,您需要将以下内容添加到清单
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
或者尝试将此添加到您的代码中: 而不是这个:
OUTPUT_FILE = Environment.getExternalStorageDirectory()+"audiorecorder.acc";
尝试:
OUTPUT_FILE = Environment.getExternalStorageDirectory()+
File.separator+"The name of your folder"+
File.separator+"audiorecorder.acc";