Android中的录音和流媒体

时间:2014-09-11 06:02:30

标签: android audio-streaming audio-recording

我正在开发一个Android应用程序。我想完成以下功能。 我将使用手机的内置麦克风进行录制,同时我希望通过手机的扬声器或耳机播放录制的音频。 这可行吗?如果是的话,请帮助我。

3 个答案:

答案 0 :(得分:3)

这是一个简单的录制和播放应用程序 使用Android AudioRecordAudioTrack

  

<强>设计

     

录制的音频被写入缓冲区并从同一缓冲区播放。此机制在按钮控制的循环(使用Android线程)中运行。


代码

private String TAG = "AUDIO_RECORD_PLAYBACK";
private boolean isRunning = true;
private Thread m_thread;               /* Thread for running the Loop */

private AudioRecord recorder = null;
private AudioTrack track = null;

int bufferSize = 320;                  /* Buffer for recording data */
byte buffer[] = new byte[bufferSize];

/* Method to Enable/Disable Buttons */
private void enableButton(int id,boolean isEnable){
    ((Button)findViewById(id)).setEnabled(isEnable);
}

GUI有两个按钮 START STOP 启用按钮:

enableButton(R.id.StartButton,true);
enableButton(R.id.StopButton,false);

/* Assign Button Click Handlers */
((Button)findViewById(R.id.StartButton)).setOnClickListener(btnClick);
((Button)findViewById(R.id.StopButton)).setOnClickListener(btnClick);

映射OnClickListener的START和STOP按钮

private View.OnClickListener btnClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.StartButton:
        {
            Log.d(TAG, "======== Start Button Pressed ==========");
            isRunning = true;
            do_loopback(isRunning);
            enableButton(R.id.StartButton,false);
            enableButton(R.id.StopButton,true);
            break;
        }
        case R.id.StopButton:
        {
            Log.d(TAG, "======== Stop Button Pressed ==========");
            isRunning = false;
            do_loopback(isRunning);
            enableButton(R.id.StopButton,false);
            enableButton(R.id.StartButton,true);
            break;
        }
    }
}

启动线程:

private void do_loopback(final boolean flag) 
{
    m_thread = new Thread(new Runnable() {
        public void run() {
            run_loop(flag);
        }
    });
    m_thread.start();
}

初始化AudioRecord和AudioTrack的方法:

public AudioTrack findAudioTrack (AudioTrack track)
{
    Log.d(TAG, "===== Initializing AudioTrack API ====");
    int m_bufferSize = AudioTrack.getMinBufferSize(8000, 
            AudioFormat.CHANNEL_OUT_MONO, 
            AudioFormat.ENCODING_PCM_16BIT);

    if (m_bufferSize != AudioTrack.ERROR_BAD_VALUE) 
    {
        track = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, 
                AudioFormat.CHANNEL_OUT_MONO, 
                AudioFormat.ENCODING_PCM_16BIT, m_bufferSize, 
                AudioTrack.MODE_STREAM);

        if (track.getState() == AudioTrack.STATE_UNINITIALIZED) {
            Log.e(TAG, "===== AudioTrack Uninitialized =====");
            return null;
        }
    }
    return track;
}

public AudioRecord findAudioRecord (AudioRecord recorder)
{
    Log.d(TAG, "===== Initializing AudioRecord API =====");     
    int m_bufferSize = AudioRecord.getMinBufferSize(8000,
            AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT);

    if (m_bufferSize != AudioRecord.ERROR_BAD_VALUE) 
    {
        recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000, 
                AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_PCM_16BIT, m_bufferSize);

        if (recorder.getState() == AudioRecord.STATE_UNINITIALIZED) {
            Log.e(TAG, "====== AudioRecord UnInitilaised ====== ");
            return null;
        }
    }
    return recorder;
}

findAudioRecordfindAudioTrack的值可能会根据设备发生变化 请参阅this问题。


运行循环的代码:

public void run_loop (boolean isRunning)
{

    /** == If Stop Button is pressed == **/
    if (isRunning == false) {
        Log.d(TAG, "=====  Stop Button is pressed ===== ");

        if (AudioRecord.STATE_INITIALIZED == recorder.getState()){
            recorder.stop();
            recorder.release();
        }
        if (AudioTrack.STATE_INITIALIZED == track.getState()){
            track.stop();
            track.release();
        }
        return;
    }


    /** ======= Initialize AudioRecord and AudioTrack ======== **/
    recorder = findAudioRecord(recorder);
    if (recorder == null) {
        Log.e(TAG, "======== findAudioRecord : Returned Error! =========== ");
        return;
    }

    track = findAudioTrack(track);
    if (track == null) {
        Log.e(TAG, "======== findAudioTrack : Returned Error! ========== ");
        return;
    }

    if ((AudioRecord.STATE_INITIALIZED == recorder.getState()) &&
            (AudioTrack.STATE_INITIALIZED == track.getState()))
    {
        recorder.startRecording();
        Log.d(TAG, "========= Recorder Started... =========");
        track.play();
        Log.d(TAG, "========= Track Started... =========");
    } 
    else 
    {
        Log.d(TAG, "==== Initilazation failed for AudioRecord or AudioTrack =====");
        return;
    }

    /** ------------------------------------------------------ **/

    /* Recording and Playing in chunks of 320 bytes */
    bufferSize = 320;

    while (isRunning == true) 
    {
        /* Read & Write to the Device */
        recorder.read(buffer, 0, bufferSize);
        track.write(buffer, 0, bufferSize);

    }
    Log.i(TAG, "Loopback exit");
    return;
}

请在AndroidManifest.xml

中加入以下内容
<uses-permission android:name="android.permission.RECORD_AUDIO" >  </uses-permission>

通过使用相同的API从文件写入/读取也可以实现上述过程 为什么使用audioRecord而不是mediaRecorder - See here

本规范经过测试(在谷歌Nexus 5上)并且运行良好。

注意:如果您失败,请为recorder.readtrack.write添加一些错误检查代码。同样适用于findAudioRecordfindAudioTrack

答案 1 :(得分:0)

首先在onCreate方法,MediaRecorder类对象和要保存记录数据的文件路径中创建对象。

  String outputFile = Environment.getExternalStorageDirectory().
  getAbsolutePath() + "/myrecording.3gp";   // Define outputFile outside onCreate method

  MediaRecorder myAudioRecorder = new MediaRecorder(); // Define this outside onCreate method

  myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
  myAudioRecorder.setOutputFile(outputFile);

这三个功能你可以在任何按钮上调用它,以便播放Rec,停止Rec并启动Rec;

  public void start(View view){
  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();
   }
   start.setEnabled(false);
   stop.setEnabled(true);
   Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
  }

  public void stop(View view){

    myAudioRecorder.stop();
    myAudioRecorder.release();
    myAudioRecorder  = null;
    stop.setEnabled(false);
    play.setEnabled(true);
    Toast.makeText(getApplicationContext(), "Audio recorded successfully",
    Toast.LENGTH_LONG).show();
   }



  public void play(View view) throws IllegalArgumentException,   

    SecurityException, IllegalStateException, IOException{
    MediaPlayer m = new MediaPlayer();
    m.setDataSource(outputFile);
    m.prepare();
    m.start();
    Toast.makeText(getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show();

    }

答案 2 :(得分:0)

当我阅读开发人员文档here时,Android支持RTSP协议(用于实时流媒体)以及HTTP / HTTPS直播流草案协议。

还有一个例子here。您必须具备有关Streaming服务器的基础知识,例如Red5或Wowza。