gpt4 book ai didi

android - 如何保存多个音频文件?

转载 作者:行者123 更新时间:2023-12-03 00:40:07 25 4
gpt4 key购买 nike

我刚开始使用录音机应用程序。到目前为止,该应用程序可以录制音频并播放。我的最终目标是能够将多个音频文件保存到设备。我该怎么做?我可以使用SharedPreferences吗?我什么都找不到。

public class MainActivity extends AppCompatActivity {

Button play;
Button stop;
Button record;
private MediaRecorder myAudioRecorder;
private String outputFile = null;

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

play=(Button)findViewById(R.id.button3);
stop=(Button)findViewById(R.id.button2);
record=(Button)findViewById(R.id.button);

stop.setEnabled(false);
play.setEnabled(false);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";;

myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);
myAudioRecorder.prepare();
myAudioRecorder.start();
}

catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

stop.setEnabled(true);

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

record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});

stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAudioRecorder.stop();
myAudioRecorder.release();
myAudioRecorder = null;

play.setEnabled(true);

Toast.makeText(getApplicationContext(), "Audio recorded successfully",Toast.LENGTH_LONG).show();
}
});

play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) throws IllegalArgumentException,SecurityException,IllegalStateException {
MediaPlayer m = new MediaPlayer();

try {
m.setDataSource(outputFile);
}

catch (IOException e) {
e.printStackTrace();
}

try {
m.prepare();
}

catch (IOException e) {
e.printStackTrace();
}

m.start();
Toast.makeText(getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

最佳答案

在您的外部存储器中创建一个文件夹,并在每个文件的名称后附加一个时间戳。

您可以列出存储在此文件夹中的所有视频/音频文件,并随时选择要播放的视频/音频。

获取所有文件

public void displayAllFilesInFolder() {
List<String> filepath = new ArrayList<String>();

if (!hasSDCard()) {
return;
}
File dir = new File(android.os.Environment.getExternalStorageDirectory() + "folderName");
if (!dir.exists()) {
return;
}
File listAllFiles[] = dir.listFiles();
if (listAllFiles != null) {
for (int i = 0; i < listAllFiles.length; i++) {
if (!listAllFiles[i].isDirectory()) {
filepath.add(listAllFiles[i].getAbsolutePath());
}
}
}
}

检查是否已安装SD
private boolean hasSDCard(){
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(isSDPresent) {
return true;
}
else{
return false;
}
}

关于android - 如何保存多个音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35470481/

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