JPEG 图像直播
启动设备的 JPEG 直播模式,并渲染 SDK 提供的每个完整 JPEG 图像。
前提条件
- 设备已连接并就绪,且符合
LiveStreamingAPI。 supportsJPEGImageLiveStreaming为true。- UI 能够在不阻塞数据回调的情况下解码和替换图像。
API 参考
框架
AIBuds.xcframework
导入
- Swift
- Objective-C
import AIBuds
import UIKit#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>
#import <UIKit/UIKit.h>属性
- Swift
- Objective-C
/// Whether JPEG image live streaming is supported
var supportsJPEGImageLiveStreaming: Bool { get }/// Whether JPEG image live streaming is supported.
@property(nonatomic, readonly) BOOL supportsJPEGImageLiveStreaming;实例方法
- Swift
- Objective-C
/// Start JPEG image live streaming
/// - Parameters:
/// - sessionStartCompletionHandler: Session start completion handler
/// - jpegDataReceivedHandler: JPEG data received handler
/// - sessionFinishHandler: Session finish handler
func startJPEGImageLiveStreaming(
withSessionStartCompletionHandler sessionStartCompletionHandler:
AIBudsLiveStreamingSessionStartCompletionHandler?,
jpegDataReceivedHandler: AIBudsLiveStreamingJpegDataReceivedHandler?,
sessionFinishHandler: AIBudsLiveStreamingSessionFinishHandler?
)/// Start JPEG image live streaming
/// - Parameters:
/// - sessionStartCompletionHandler: Session start completion handler
/// - jpegDataReceivedHandler: JPEG data received handler
/// - sessionFinishHandler: Session finish handler
- (void)
startJPEGImageLiveStreamingWithSessionStartCompletionHandler:
(AIBudsLiveStreamingSessionStartCompletionHandler _Nullable)sessionStartCompletionHandler
jpegDataReceivedHandler:
(AIBudsLiveStreamingJpegDataReceivedHandler _Nullable)
jpegDataReceivedHandler
sessionFinishHandler:
(AIBudsLiveStreamingSessionFinishHandler _Nullable)
sessionFinishHandler;请参阅 startJPEGImageLiveStreaming。
回调值
| Handler | 值 | 含义 |
|---|---|---|
sessionStartCompletionHandler | success, error | 报告直播会话是否启动。 |
jpegDataReceivedHandler | jpegData | 以 Data / NSData 提供一个完整 JPEG 图像。 |
sessionFinishHandler | isUserInitiatedStop, error | 报告会话结束结果,以及是否由用户主动停止。 |
返回值
该方法没有直接返回值。媒体和生命周期结果通过 Handler 提供。
使用示例
- Swift
- Objective-C
guard let streamingDevice = device as? LiveStreamingAPI,
streamingDevice.supportsJPEGImageLiveStreaming
else { return }
streamingDevice.startJPEGImageLiveStreaming(
withSessionStartCompletionHandler: { success, error in
DispatchQueue.main.async {
self.setStreamingActive(success)
if let error { self.show(error) }
}
},
jpegDataReceivedHandler: { jpegData in
// Decode away from the UI update, then assign the image on the main queue.
guard let image = UIImage(data: jpegData) else { return }
DispatchQueue.main.async {
self.imageView.image = image
}
},
sessionFinishHandler: { userStopped, error in
DispatchQueue.main.async {
self.setStreamingActive(false)
self.handleFinish(userStopped: userStopped, error: error)
}
}
)id<AIBudsLiveStreamingAPI> streamingDevice = (id<AIBudsLiveStreamingAPI>)self.device;
if (![streamingDevice conformsToProtocol:@protocol(AIBudsLiveStreamingAPI)] ||
!streamingDevice.supportsJPEGImageLiveStreaming) {
return;
}
__weak typeof(self) weakSelf = self;
[streamingDevice
startJPEGImageLiveStreamingWithSessionStartCompletionHandler:^(BOOL success, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf setStreamingActive:success];
if (error)
[weakSelf showError:error];
});
}
jpegDataReceivedHandler:^(NSData *jpegData) {
UIImage *image = [UIImage imageWithData:jpegData];
if (!image)
return;
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.imageView.image = image;
});
}
sessionFinishHandler:^(BOOL userStopped, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf setStreamingActive:NO];
[weakSelf handleFinishUserStopped:userStopped error:error];
});
}];错误处理
- 拒绝无效 JPEG 数据,并保留最后一个有效图像。
- 将
sessionStartCompletionHandler失败视为会话启动失败。 - 在
sessionFinishHandler中区分用户停止与意外中断,并在存在时使用error。 - 不要在 SDK 提供的回调顺序之外推断丢帧、帧率或顺序保证。
最佳实践
- 避免保留所有 JPEG 帧;替换当前显示图像或使用有界缓冲区。
- 保持图像解码和 UI 赋值轻量,避免回调积压。
- 新会话开始时清理帧计数器和展示状态。
- 离开功能时调用
stopLiveStreaming(),不要只隐藏图像视图。
注意事项
- 公开 SDK 没有规定固定 JPEG 分辨率或帧率。
- JPEG 模式不需要
AIBudsLiveStream播放类。 jpegDataReceivedHandler提供压缩 JPEG 数据,而不是原始像素缓冲区。