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

相关内容

热门资讯

透视有挂!wpk辅助ai,wp... 透视有挂!wpk辅助ai,wpk模拟器,细节揭秘(有挂黑科技)1、下载好wpk辅助ai辅助软件之后点...
透视免费"hhpok... 透视免费"hhpoker哪个俱乐部靠谱"详细辅助黑科技教程(钻石可以看底牌技巧)关于hhpoker哪...
透视好牌!wepoker透视脚... 透视好牌!wepoker透视脚本,hhpoker德州挂真的有吗,安装教程(有挂教程);1、透视好牌!...
透视数据"wepok... 1、透视数据"wepoker辅助插件功能"详细辅助2025新版(插件下载透明)(UU poker、w...
透视工具!wepoker透视版... 透视工具!wepoker透视版下载,智星德州插件2024最新版,2025新版技巧(有挂解密)wepo...
透视了解"wepok... 透视了解"wepoker怎么下载游戏"详细辅助技巧教程(辅助分析器攻略)准备好在wepoker怎么下...
透视讲解!wepoker透视脚... 透视讲解!wepoker透视脚本苹果版,wepoker有人用过吗,分享教程(有挂细节);1)wepo...
透视教学"wepok... 透视教学"wepoker一直输的号能继续打吗"详细辅助攻略教程(私人辅助软件透视);支持2-10人实...
透视了解!智星菠萝辅助器,we... 透视了解!智星菠萝辅助器,wepoker开脚本视频,专业教程(有挂揭秘);1、智星菠萝辅助器系统规律...
透视规律"wepok... 透视规律"wepoker软件靠谱么"详细辅助线上教程(有插件教程);支持2-10人实时对战,虚拟庄家...