tencent cloud

文档反馈

最后更新时间:2024-06-07 11:20:49
    Flutter本文档主要介绍如何使用 TRTC Flutter SDK 实现自定义音频原数据获取。

    获取音频原数据

    Flutter TRTC SDK 提供两种音频原数据的获取方式:
    Native 接入。
    直接使用 Flutter 的 Dart 接口。
    由于将高频且庞大的音频原数据从 Native 传输到 Dart 层会耗费较多性能, 我们推荐使用 Native 接入,获取音频原数据。

    Native 接入

    具体接入过程和接入效果可使用 demo 体验。
    1. 在 Native 层监听音频原数据,获取音频原数据。
    java
    swift
    void enableTRTCAudioFrameDelegate() {
    TRTCCloud.sharedInstance(getApplicationContext()).setAudioFrameListener(new AudioFrameListener());
    result.success("");
    }
    
    void disableTRTCAudioFrameDelegate() {
    TRTCCloud.sharedInstance(getApplicationContext()).setAudioFrameListener(null);
    result.success("");
    }
    
    class AudioFrameListener implements TRTCCloudListener.TRTCAudioFrameListener {
    @Override
    public void onCapturedAudioFrame(TRTCCloudDef.TRTCAudioFrame trtcAudioFrame) {
    // TODO
    }
    
    @Override
    public void onLocalProcessedAudioFrame(TRTCCloudDef.TRTCAudioFrame trtcAudioFrame) {
    // TODO
    }
    
    @Override
    public void onRemoteUserAudioFrame(TRTCCloudDef.TRTCAudioFrame trtcAudioFrame, String s) {
    // TODO
    }
    
    @Override
    public void onMixedPlayAudioFrame(TRTCCloudDef.TRTCAudioFrame trtcAudioFrame) {
    // TODO
    }
    
    @Override
    public void onMixedAllAudioFrame(TRTCCloudDef.TRTCAudioFrame trtcAudioFrame) {
    // TODO
    }
    
    @Override
    public void onVoiceEarMonitorAudioFrame(TRTCCloudDef.TRTCAudioFrame trtcAudioFrame) {
    // TODO
    }
    }
    let listener = AudioFrameProcessListener()
    func enableTRTCAudioFrameDelegate() {
    TRTCCloud.sharedInstance().setAudioFrameDelegate(listener)
    result(nil)
    }
    
    func disableTRTCAudioFrameDelegate() {
    TRTCCloud.sharedInstance().setAudioFrameDelegate(nil)
    result(nil)
    }
    
    class AudioFrameProcessListener: NSObject, TRTCAudioFrameDelegate {
    func onCapturedAudioFrame(_ frame: TRTCAudioFrame) {
    //MARK: TODO
    }
    func onLocalProcessedAudioFrame(_ frame: TRTCAudioFrame) {
    // MARK: TODO
    }
    
    func onRemoteUserAudioFrame(_ frame: TRTCAudioFrame, userId: String) {
    // MARK: TODO
    }
    
    func onMixedAllAudioFrame(_ frame: TRTCAudioFrame) {
    // MARK: TODO
    }
    
    func onMixedPlay(_ frame: TRTCAudioFrame) {
    // MARK: TODO
    }
    
    func onVoiceEarMonitorAudioFrame(_ frame: TRTCAudioFrame) {
    // MARK: TODO
    }
    }
    2. 使用 Method Channel 实现开始/停止获取音频原数据。
    2.1 在 Dart 层实现获取音频原数据的开始/停止接口。
    final channel = MethodChannel('TRCT_FLUTTER_EXAMPLE');
    void enableAudioFrame() async {
    await channel.invokeMethod('enableTRTCAudioFrameDelegate');
    }
    
    void disableAudioFrame() async {
    await channel.invokeMethod('disableTRTCAudioFrameDelegate');
    }
    2.2 在 Native 层实现获取音频原数据的开始/停止接口。
    java
    swift
    public class MainActivity extends FlutterActivity {
    private static final String channelName = "TRCT_FLUTTER_EXAMPLE";
    private MethodChannel channel;
    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
    super.configureFlutterEngine(flutterEngine);
    channel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), channelName);
    channel.setMethodCallHandler(((call, result) -> {
    switch (call.method) {
    case "enableTRTCAudioFrameDelegate":
    enableTRTCAudioFrameDelegate();
    break;
    case "disableTRTCAudioFrameDelegate":
    disableTRTCAudioFrameDelegate();
    break;
    default:
    break;
    }
    }));
    }
    }
    @UIApplicationMain
    @objc class AppDelegate: FlutterAppDelegate {
    var channel: FlutterMethodChannel?
    override func application(_ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    guard let controller = window?.rootViewController as? FlutterViewController else {
    fatalError("Invalid root view controller")
    }
    channel = FlutterMethodChannel(name: "TRCT_FLUTTER_EXAMPLE", binaryMessenger: controller.binaryMessenger)
    channel?.setMethodCallHandler({ [weak self] call, result in
    guard let self = self else { return }
    switch (call.method) {
    case "enableTRTCAudioFrameDelegate":
    self.enableTRTCAudioFrameDelegate()
    break
    case "disableTRTCAudioFrameDelegate":
    self.disableTRTCAudioFrameDelegate()
    break
    default:
    break
    }
    })
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    }

    Flutter 层接口接入

    目前 Flutter Dart 接口仅支持 onCapturedAudioFrame 接口的使用。具体使用方法如下:
    TRTCCloud trtcCloud = (await TRTCCloud.sharedInstance())!;
    
    // 开启音频原数据获取
    final audioFrameListener = TRTCAudioFrameListener( onCapturedAudioFrame: (audioFrame) {
    // TODO } ); trtcCloud.setAudioFrameListener(audioFrameListener);
    // 停止音频原数据获取
    trtcCloud.setAudioFrameListener(null);
    
    联系我们

    联系我们,为您的业务提供专属服务。

    技术支持

    如果你想寻求进一步的帮助,通过工单与我们进行联络。我们提供7x24的工单服务。

    7x24 电话支持