【问题标题】:Recording and saving audio file without replacing another录制和保存音频文件而不替换另一个
【发布时间】:2016-02-13 19:28:17
【问题描述】:

我正在开发一个具有录音功能的安卓应用。我可以录制音频文件并将其保存到设备的存储中,但似乎存在问题。当我录制两次时,录制的音频会替换第一次录制的音频。

我希望我的应用这样做:

  • 我录好了,已经成功录制并保存了音频 文件进入存储;
  • 我重新录制了,已经成功录制并保存了音频 文件进入存储;
  • 存储中将有两个录制和保存的音频文件。

这是我正在使用的代码:

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.IOException;

public class RecordModule extends Activity {

Button SpeakBtn, StopBtn;
private MediaRecorder myAudioRecorder;
private String outputFile = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recordmodule);

    SpeakBtn = (Button) findViewById(R.id.SpeakBtn);
    StopBtn = (Button) findViewById(R.id.StopBtn);

    StopBtn.setEnabled(false);
    SpeakBtn.setEnabled(true);
    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);

    SpeakBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                myAudioRecorder.prepare();
                myAudioRecorder.start();
            }

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

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

            SpeakBtn.setEnabled(false);
            StopBtn.setEnabled(true);

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

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

            StopBtn.setEnabled(false);
            SpeakBtn.setEnabled(true);

            Toast.makeText(getApplicationContext(), "Audio recorded successfully", 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_record_module, 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);
}

【问题讨论】:

  • 我有一个关于您的代码的问题,您是否认为记录的文件会在您的应用程序运行时显示?

标签: android audio android-studio record


【解决方案1】:

第一个记录被覆盖,所以其他的需要有另一个文件名。

我建议您使用输出文件名作为记录的创建时间:

Date createdTime = new Date();    
outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + createdTime + "_rec.3gp";

【讨论】:

  • 当我尝试导入日期类时,它给了我两个选择; java.util.Date & java.sql.Date。我应该选择哪一个?
  • 我在真实设备上尝试了你的代码,在按下停止记录按钮后,应用程序给了我一个错误,上面写着:不幸的是,MyApp 已停止。这是为什么呢?
  • 它是 java.util.Date;错误应该是因为你可能使用了 sql
  • 不,我使用了 java.util.Date 但仍然出现错误。有没有其他办法?
  • 反正我的问题已经解决了。感谢您的帮助并让我知道为什么我会遇到上述问题。我使用 SimpleDateFormat 类而不是 Date 类,现在一切正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-25
  • 1970-01-01
  • 2016-10-13
  • 2023-03-23
  • 2012-06-09
相关资源
最近更新 更多