Android Oboe库:是否可以从2个录音设备同时录制2个音频流?
创始人
2024-08-14 21:00:48
0

是的,Android Oboe库可以同时录制来自两个录音设备的两个音频流。以下是一个示例代码,展示了如何使用Oboe库在两个录音设备上同时录制音频流:

首先,在build.gradle文件中添加Oboe库的依赖:

implementation 'com.google.oboe:oboe:1.5.0'

然后,在您的代码中使用以下代码片段:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.oboe.AudioInputStream;
import com.google.oboe.AudioOutputStream;
import com.google.oboe.AudioStreamCallback;
import com.google.oboe.AudioStreamConfiguration;
import com.google.oboe.Oboe;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private AudioInputStream mInputStream1;
    private AudioInputStream mInputStream2;
    private AudioOutputStream mOutputStream1;
    private AudioOutputStream mOutputStream2;

    private Button mStartButton;
    private boolean mIsRecording = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mStartButton = findViewById(R.id.button_start);
        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mIsRecording) {
                    stopRecording();
                } else {
                    startRecording();
                }
            }
        });
    }

    private void startRecording() {
        mIsRecording = true;
        mStartButton.setText("Stop Recording");

        // Open the input streams for two recording devices
        mInputStream1 = new AudioInputStream.Builder()
                .setDirection(AudioInputStream.Direction.INPUT)
                .setSharingMode(AudioInputStream.SharingMode.EXCLUSIVE)
                .setPerformanceMode(AudioInputStream.PerformanceMode.LOW_LATENCY)
                .setCallback(new AudioStreamCallback() {
                    @Override
                    public void onAudioReady(AudioInputStream audioInputStream, final int numFrames) {
                        // Handle the incoming audio data from the first input stream
                        // Process the audio data as needed
                    }
                })
                .build();

        mInputStream2 = new AudioInputStream.Builder()
                .setDirection(AudioInputStream.Direction.INPUT)
                .setSharingMode(AudioInputStream.SharingMode.EXCLUSIVE)
                .setPerformanceMode(AudioInputStream.PerformanceMode.LOW_LATENCY)
                .setCallback(new AudioStreamCallback() {
                    @Override
                    public void onAudioReady(AudioInputStream audioInputStream, final int numFrames) {
                        // Handle the incoming audio data from the second input stream
                        // Process the audio data as needed
                    }
                })
                .build();

        // Open the output streams for recording
        mOutputStream1 = new AudioOutputStream.Builder()
                .setDirection(AudioOutputStream.Direction.OUTPUT)
                .setSharingMode(AudioOutputStream.SharingMode.EXCLUSIVE)
                .setPerformanceMode(AudioOutputStream.PerformanceMode.LOW_LATENCY)
                .setChannelCount(2) // Set the number of channels according to your requirements
                .setFormat(AudioFormat.FORMAT_PCM_16)
                .setSampleRate(AudioFormat.SAMPLE_RATE_44100)
                .setCallback(new AudioStreamCallback() {
                    @Override
                    public void onAudioReady(AudioOutputStream audioOutputStream, final int numFrames) {
                        // Write the audio data to the first output stream
                        // Process the audio data as needed
                    }
                })
                .build();

        mOutputStream2 = new AudioOutputStream.Builder()
                .setDirection(AudioOutputStream.Direction.OUTPUT)
                .setSharingMode(AudioOutputStream.SharingMode.EXCLUSIVE)
                .setPerformanceMode(AudioOutputStream.PerformanceMode.LOW_LATENCY)
                .setChannelCount(2) // Set the number of channels according to your requirements
                .setFormat(AudioFormat.FORMAT_PCM_16)
                .setSampleRate(AudioFormat.SAMPLE_RATE_44100)
                .setCallback(new AudioStreamCallback() {
                    @Override
                    public void onAudioReady(AudioOutputStream audioOutputStream, final int numFrames) {
                        // Write the audio data to the second output stream
                        // Process the audio data as needed
                    }
                })
                .build();

        // Start the input and output streams
        mInputStream1.open();
        mInputStream2.open();
        mOutputStream1.open();
        mOutputStream2.open();
    }

    private void stopRecording() {
        mIsRecording = false;
        mStartButton.setText("Start Recording");

        // Close the input and output streams
        mInputStream1.close();
        mInputStream2.close();
        mOutputStream1.close();
        mOutputStream2.close();

        mInputStream1 = null;
        mInputStream2

相关内容

热门资讯

实操分享!蜀山四川小程序作弊视... 实操分享!蜀山四川小程序作弊视频,友友联盟辅助软件下载,盘点开挂辅助教程(有挂猫腻);无需打开直接搜...
重大推荐!免费天天贵阳辅助工具... 您好:免费天天贵阳辅助工具这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户...
一分钟了解!游戏茶苑辅助器,新... 一分钟了解!游戏茶苑辅助器,新518互游脚本下载,关于开挂辅助教程(有挂工具);无需打开直接搜索加(...
必备科技!微信小程序免费黑科技... 必备科技!微信小程序免费黑科技,衢州都莱辅助软件,推荐开挂辅助教程(有人有挂);无需打开直接搜索加薇...
科技揭秘!游戏辅助器免费版,创... 科技揭秘!游戏辅助器免费版,创思维激k辅助下载,详细开挂辅助教程(了解有挂);无需打开直接搜索加薇1...
玩家必看教程!熊猫四川辅助,天... 玩家必看教程!熊猫四川辅助,天天爱柳州有没有辅助器,了解开挂辅助教程(发现有挂);无需打开直接搜索加...
实测交流!丰城双剑必胜辅助,中... 实测交流!丰城双剑必胜辅助,中至赣牌圈插件,盘点开挂辅助教程(讲解有挂);无需打开直接搜索加薇136...
一起来探讨!微信小程序微乐游戏... 您好:创思维激k看底牌辅助软件这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
重大通报!传送屋透视,拱趴大菠... 重大通报!传送屋透视,拱趴大菠萝辅助,传授开挂辅助教程(存在有挂)1、下载安装好拱趴大菠萝辅助,进入...
科普分享!江湖悠悠手游多开辅助... 科普分享!江湖悠悠手游多开辅助,开心庄园辅助,关于开挂辅助教程(有挂详情);无需打开直接搜索加(薇:...