2011-03-09 145 views
3

我需要從Android設備上的麥克風增加語音,錄音機。 我試圖從AudioRecord讀取緩衝區,然後將它寫入到AudioTrack ...它工作,但延遲,因爲最小緩衝區大小,返回的BU方法AudioRecord.getMinBufferSize與44100頻率是4480字節。路由語音緩衝從麥克風到揚聲器在Android中

任何想法?
謝謝。

回答

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(); 
     } 
    } 
} 
+0

這是什麼?這是MediaPlayer使用的簡單例子...不幫助我 – 2011-03-09 15:24:07

1

我有這樣的代碼

AudioRecord and AudioTrack latency

但它恰好有20ms的延遲,我需要去解決它, 上面的代碼似乎扮演的東西,但沒有麥克風輸入,它工作?

謝謝!

1

我注意到沒有線程代碼。我會建議嘗試線程化錄製和回放方面的內容,看看是否能更好地避免延遲。從麥克風一個螺紋中填入緩衝區,並將其讀出到另一個揚聲器中。通過某些操作(例如清除緩衝區溢出)來避免緩衝區溢出和欠載。理論上,應該跟上另一個。

相關問題