异步 ASR 方式


是的,Microsoft Cognitive Services Speech SDK 支持异步 ASR(自动语音识别)。

异步 ASR 方式

1. 单次识别(异步)

SpeechConfig speechConfig = SpeechConfig.fromSubscription("YourKey", "YourRegion");
AudioConfig audioConfig = AudioConfig.fromWavFileInput("audio.wav");
SpeechRecognizer recognizer = new SpeechRecognizer(speechConfig, audioConfig);

// 异步提交,返回 Future
Future<SpeechRecognitionResult> task = recognizer.recognizeOnceAsync();

// 非阻塞,可以做其他事情
System.out.println("Recognition in progress...");

// 需要结果时再 get()
SpeechRecognitionResult result = task.get();
System.out.println("Result: " + result.getText());

recognizer.close();
speechConfig.close();

2. 连续识别(事件驱动,完全异步)

SpeechRecognizer recognizer = new SpeechRecognizer(speechConfig, audioConfig);

// 注册回调,结果异步回调通知
recognizer.recognized.addEventListener((s, e) -> {
    if (e.getResult().getReason() == ResultReason.RecognizedSpeech) {
        System.out.println("Recognized: " + e.getResult().getText());
    }
});

recognizer.recognizing.addEventListener((s, e) -> {
    System.out.println("Recognizing: " + e.getResult().getText()); // 中间结果
});

recognizer.canceled.addEventListener((s, e) -> {
    System.out.println("Canceled: " + e.getReason());
});

// 启动连续识别(非阻塞)
recognizer.startContinuousRecognitionAsync().get();

// ... 等待音频输入 ...

recognizer.stopContinuousRecognitionAsync().get();

3. 结合 CompletableFuture(更现代的异步写法)

Future<SpeechRecognitionResult> future = recognizer.recognizeOnceAsync();

CompletableFuture.supplyAsync(() -> {
    try {
        return future.get();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}).thenAccept(result -> {
    System.out.println("Async result: " + result.getText());
}).exceptionally(ex -> {
    System.err.println("Error: " + ex.getMessage());
    return null;
});

两种模式对比

模式 方法 适用场景
单次识别 recognizeOnceAsync() 短语音、单句识别
连续识别 startContinuousRecognitionAsync() 长音频、实时流式识别

推荐:长音频或实时场景用连续识别,它天然异步且支持中间结果回调,更适合生产环境使用。



扫描二维码,在手机上阅读
收藏

批量转录Batch REST API

分销系统后端实现计划

评 论
请登录后再评论