停止直播
停止当前 RTSP 或 JPEG 设备直播会话,并释放宿主 App 持有的播放器、转发器、图像和 UI 资源。
前提条件
- 目标设备符合
LiveStreamingAPI。 - 保留当前会话所创建展示资源的引用。
- 保证停止处理幂等,使页面关闭和显式停止操作能够共用同一清理路径。
API 参考
框架
AIBuds.xcframework
导入
- Swift
- Objective-C
import AIBuds
import AIBudsLiveStream#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>
#import <AIBudsLiveStream/AIBudsLiveStream-Swift.h>实例方法
- Swift
- Objective-C
/// Stop live streaming
func stopLiveStreaming()/// Stop live streaming
- (void)stopLiveStreaming;请参阅 stopLiveStreaming。
参数
该方法没有参数。
返回值
该方法没有返回值,也不接受完成回调。会话如何结束以及是否发生错误,请通过启动会话时传入的 sessionFinishHandler 获取。
使用示例
Demo 会先停止可选 RTMP 转发,再停止 RTSP 播放,然后清理设备会话和 UI 状态。设备指令和宿主资源具有独立生命周期。
- Swift
- Objective-C
func stopCurrentLiveStream() {
// Stop optional host-side relay and playback resources.
liveStreamer?.stopStreaming()
liveStreamer = nil
mediaPlayer?.stop()
// Stop the RTSP or JPEG session on the connected device.
(device as? LiveStreamingAPI)?.stopLiveStreaming()
currentRTSPAddress = nil
imageView.image = nil
setStreamingActive(false)
}- (void)stopCurrentLiveStream {
// Stop optional host-side relay and playback resources.
[self.liveStreamer stopStreaming];
self.liveStreamer = nil;
[self.mediaPlayer stop];
// Stop the RTSP or JPEG session on the connected device.
id<AIBudsLiveStreamingAPI> streamingDevice = (id<AIBudsLiveStreamingAPI>)self.device;
if ([streamingDevice conformsToProtocol:@protocol(AIBudsLiveStreamingAPI)]) {
[streamingDevice stopLiveStreaming];
}
self.currentRtspAddress = nil;
self.imageView.image = nil;
[self setStreamingActive:NO];
}错误处理
由于 stopLiveStreaming() 没有完成回调,本地清理必须可安全重复,并使用原会话的 finish 回调区分 isUserInitiatedStop 与意外中断。会话异常结束时记录或显示回调错误。
最佳实践
- 离开持有页面或结束功能时停止直播。
- 停止设备会话时同时释放 RTMP 转发器、播放器和保留的 RTSP 地址。
- 不要等待公开方法并未提供的停止完成回调。
- 保证 finish 回调处理幂等,因为它可能在本地清理后执行。
注意事项
- 同一个停止方法可结束 RTSP 或 JPEG 设备会话。
- 只停止
LiveStreamingPlayer不会停止设备会话。 - 只停止设备会话不会自动释放宿主持有的播放或转发对象。