在Android中将语音缓冲区从麦克风路由到扬声器

时间:2011-03-09 10:35:12

标签: android audiorecord

我需要在Android设备上增加语音,麦克风上的录音机。 我尝试从AudioRecord读取缓冲区,然后将其写入AudioTrack ...它有效,但有延迟,因为最小缓冲区大小,返回bu方法AudioRecord.getMinBufferSize的频率如44100是4480字节。

任何想法?
感谢。

3 个答案:

答案 0 :(得分:1)

我有这段代码

AudioRecord and AudioTrack latency

但碰巧有20ms的延迟,我需要解决它, 上面的代码看起来有些东西,但没有麦克风输入,它有用吗?

谢谢!

答案 1 :(得分:1)

我注意到没有线程代码。我建议尝试线程记录和回放方面,看看是否更好地避免了延迟。从麦克风一个线程填充缓冲区,并将其读出到另一个线程中的扬声器。通过某些操作处理这些情况(例如清除缓冲区溢出),避免缓冲区溢出和欠载。理论上,人们应该跟上另一个。

答案 2 :(得分:0)

package org.example.audio;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class AudioDemo extends Activity implements OnClickListener {
    private static final String TAG = "AudioDemo";
    private static final String isPlaying = "Media is Playing"; 
    private static final String notPlaying = "Media has stopped Playing"; 

    MediaPlayer player;
    Button playerButton;

    public void onClick(View v) {
        Log.d(TAG, "onClick: " + v);
        if (v.getId() == R.id.play) {
            playPause();
        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //player = MediaPlayer.create(this, R.raw.robotrock);
        player.setLooping(false); // Set looping

        // Get the button from the view
        playerButton = (Button) this.findViewById(R.id.play);
        playerButton.setText(R.string.stop_label);
        playerButton.setOnClickListener(this);

        // Begin playing selected media
        demoPlay();

        // Release media instance to system
        player.release();
    }

    @Override
    public void onPause() {
        super.onPause();
        player.pause();
    }

    // Initiate media player pause
    private void demoPause(){
        player.pause();
        playerButton.setText(R.string.play_label);
        Toast.makeText(this, notPlaying, Toast.LENGTH_LONG).show();
        Log.d(TAG, notPlaying);
    }

    // Initiate playing the media player
    private void demoPlay(){
        player.start();
        playerButton.setText(R.string.stop_label);
        Toast.makeText(this, isPlaying, Toast.LENGTH_LONG).show();
        Log.d(TAG, isPlaying);
    }

    // Toggle between the play and pause
    private void playPause() {
        if(player.isPlaying()) {
            demoPause();
        } else {
            demoPlay();
        }   
    }
}