我已经在这里环顾四周但是找不到任何有帮助的东西,这就是我发布这个的原因
所以基本上我尝试做的就是让用户只要按下按钮就可以录制音频,并在用户完成后将录音添加到ListView。使用new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z")
将名称分配给录音,这样他们就会有所不同。
示例:第一个文件将是2016年1月1日星期四00:00:01 -0000.mp3
,第二个文件将是2016年1月1日星期四00:00:02 -0000.mp3
我已经把ListView部分出现了,但出于某种原因,我随时都会录制,虽然录制内容在ListView上显示为具有不同名称的录制内容,但当我触摸它们播放时,所有文件都是相同的音频文件。我不知道发生了什么事。我花了一些时间看,但我不确定为什么。我对Android开发相对较新,所以我很抱歉,如果它恰好是次要的,但无论如何,这是我的代码:
public class MainActivity extends Activity {
private MediaRecorder myAudioRecorder;
String nameOfRecording; // name of recording (for ListView)
String outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + nameOfRecording;
// for list of recordings
ListView ResultsListView;
ArrayList<String> lines = new ArrayList<>(); // stores names of mp3 files.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ResultsListView = (ListView) findViewById(R.id.ResultsListView);
listRecordings();
// handles "record" and "stop"
final ImageButton roundButton = (ImageButton) findViewById(R.id.fab_button);
roundButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
Log.i("Touched", "Recording");
SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
Date now = new Date();
nameOfRecording = formatter.format(now) + ".mp3";
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);
startRecording();
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
stopRecording();
Log.i("Released", "Stopped");
// refresh the ResultsListView
lines.add(nameOfRecording); // add name of recording to ArrayList.
listRecordings(); // refreshes the listView
ResultsListView.setOnItemClickListener(
new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String touchedRecording = String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(MainActivity.this, "Playing", Toast.LENGTH_SHORT).show();
playRecording(touchedRecording);
}
}
);
}
return true;
}
});
}
这是playRecording
方法()
public void playRecording(String touchedRecording)
{
touchedRecording = nameOfRecording;
MediaPlayer m = new MediaPlayer();
try {
m.setDataSource(outputFile);
}
catch (IOException e) {
e.printStackTrace();
}
try {
m.prepare();
}
catch (IOException e) {
e.printStackTrace();
}
m.start();
}
我不明白为什么文件名显示为不同的文件但其中包含相同的音频。我对Android开发相对较新,很抱歉,如果它实际上只是一些小问题。有谁知道发生了什么?非常感谢你。
P.S。我关闭应用程序时,我也没有正确发布MediaPlayer错误,但我应该能够解决这个问题。但是关于这一点的任何意见也不是高优先级,但仍然受到欢迎。