我如何改变android中录制声音的频率

问题描述 投票:0回答:1

我正在开发一个Android应用程序,通过MIC录音并改变其频率。类似于 iPhone 的 Helium Booth 应用程序。目前我已经编写了代码来记录文件并将其保存在SD卡中。现在我的问题是我可以在录音时改变声音的频率吗?或者我可以设置录制音频的频率吗? 以下是我的代码:

 public class MainActivity extends Activity {

MediaRecorder recorder;
  File audiofile = null;
  private static final String TAG = "SoundRecordingActivity";
  private View startButton;
  private View stopButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startButton = findViewById(R.id.start);
    stopButton = findViewById(R.id.stop);
    startButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
                startRecording(v);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });
    stopButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            stopRecording(v);

        }
    });
}

 public void startRecording(View view) throws IOException {

        startButton.setEnabled(false);
        stopButton.setEnabled(true);

        File sampleDir = Environment.getExternalStorageDirectory();
        try {
          audiofile = File.createTempFile("sound", ".3gp", sampleDir);
        } catch (IOException e) {
          Log.e(TAG, "sdcard access error");
          return;
        }
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(audiofile.getAbsolutePath());
        recorder.prepare();
        recorder.start();
      }

      public void stopRecording(View view) {
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
        recorder.stop();
        recorder.release();
        addRecordingToMediaLibrary();
      }

      protected void addRecordingToMediaLibrary() {
        ContentValues values = new ContentValues(4);
        long current = System.currentTimeMillis();
        values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
        values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
        values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
        values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
        ContentResolver contentResolver = getContentResolver();

        Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Uri newUri = contentResolver.insert(base, values);

        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
        Toast.makeText(this, "Added File " + newUri, 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.main, menu);
    return true;
}}
android audio-recording
1个回答
0
投票

您可以在提供的代码中设置录音参数,包括频率等:

mediaRecorder.setAudioEncodingBitRate(64000); 
mediaRecorder.setAudioChannels(1);
mediaRecorder.setAudioSamplingRate(22050);

有关使用 AudioRecord 在缓冲区中录音的想法,请参阅我在其他问题中的回答:返回值 minBufferSize 的值是多少,尝试使用不同的频率(如果不支持 8000);并在调用 startRecording 之前睡眠?

使用 AudioRecord 实例获取错误代码 20

© www.soinside.com 2019 - 2024. All rights reserved.