我刚开始制作录音机应用程序。到目前为止,该应用程序可以录制音频并播放它。我的最终目标是能够将多个音频文件保存到设备中。我该怎么做?我可以使用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);
}
}
答案 0 :(得分:1)
在外部存储中创建一个文件夹并存储每个文件,并在文件名后附加时间戳。
您可以列出此文件夹中存储的所有视频/音频文件,并随时选择您想要播放的视频/音频。
获取所有文件
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;
}
}
答案 1 :(得分:0)
您已将其保存到文件中:
Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";
您只需添加更多逻辑来选择一个尚不存在的文件名。