N
O
D
E
M
E
D
I
A
Thinking
首页
产品
文档
博客
订单
文档
快速接入-SwiftUI
2026年 5月 20日 下午3:43
# 简介 推荐现代App开发,使用声明式 SwiftUI进行编写,代码量更少,更漂亮,开发效率更高。 NodeMediaClient 4.0支持SwiftUI直接接入,性能完全一致。 ## 通过cocoapods或直接向项目导入NodeMediaClient.xcframework ``` # Uncomment the next line to define a global platform for your project # platform :ios, '9.0' target 'SwiftDemo' do # Comment the next line if you don't want to use dynamic frameworks use_frameworks! # Pods for ObjcDemo pod 'NodeMediaClient', '~> 4.0.12' end ``` ## 播放接入 ### 封装视图 ``` struct VideoViewWrapper: UIViewRepresentable { let nodePlayer: NodePlayer func makeUIView(context: Context) -> UIView { let view = UIView() view.backgroundColor = UIColor.black nodePlayer.attach(view) return view } func updateUIView(_ uiView: UIView, context: Context) { // Handle view updates if needed } } ``` ### 引用并播放 ``` struct LivePlayView: View { // State to manage the NodePlayer instance private var nodePlayer = NodePlayer(license: "") @State private var isPlaying = false @State private var videoURL: String = "rtmp://192.168.0.2/live/bbb" // UIView wrapper for NodePlayer rendering @State private var playerView: UIView? // Player options @State private var bufferTime: Float = 1000 @State private var scaleMode: Int = 1 @State private var volume: Float = 100.0 var body: some View { VStack(spacing: 20) { // Player rendering view Rectangle() .fill(Color.black) .frame(height: 240) .overlay( VideoViewWrapper(nodePlayer: nodePlayer) ) .cornerRadius(12) .padding(.horizontal) // Player options VStack(alignment: .leading, spacing: 16) { Text("Video URL:") .font(.headline) TextField("Enter video URL", text: $videoURL) .textFieldStyle(RoundedBorderTextFieldStyle()) // Buffer Time slider VStack(alignment: .leading, spacing: 8) { Text("缓冲时间 (Buffer Time): \(Int(bufferTime))ms") .font(.headline) Slider(value: $bufferTime, in: 0...3000, step: 100) .onChange(of: bufferTime) { _, newValue in updateBufferTime(Int(newValue)) } } // Scale Mode picker VStack(alignment: .leading, spacing: 8) { Text("缩放模式 (Scale Mode)") .font(.headline) Picker("Scale Mode", selection: $scaleMode) { Text("填充 (Fill)").tag(0) Text("适应 (Fit)").tag(1) Text("拉伸 (Stretch)").tag(2) } .pickerStyle(SegmentedPickerStyle()) .onChange(of: scaleMode) { _, newValue in updateScaleMode(newValue) } } // Volume slider VStack(alignment: .leading, spacing: 8) { Text("音量 (Volume): \(Int(volume))%") .font(.headline) Slider(value: $volume, in: 0...100, step: 1) .onChange(of: volume) { _, newValue in updateVolume(newValue) } } } .padding(.vertical, 8) .padding(.horizontal) .background(Color(.secondarySystemGroupedBackground)) .cornerRadius(12) .padding(.horizontal) // Control buttons HStack(spacing: 16) { Button(action: togglePlayback) { Text(isPlaying ? "Stop" : "Start") .frame(maxWidth: .infinity) } .buttonStyle(.borderedProminent) } .padding(.horizontal) Spacer() } .navigationTitle("直播播放") .background(Color(.systemGroupedBackground)) .onAppear { initializePlayer() } .onDisappear { // 页面消失时清理资源 cleanup() } } // MARK: - Private Methods /// 初始化NodePlayer private func initializePlayer() { // 设置初始播放参数 nodePlayer.scaleMode = scaleMode nodePlayer.bufferTime = Int(bufferTime) nodePlayer.volume = volume/100.0 } /// 切换播放状态 private func togglePlayback() { if isPlaying { // 停止播放 let status = nodePlayer.stop() if status == 0 { isPlaying = false print("Playback stopped") } else { print("Failed to stop playback: \(status)") } } else { // 开始播放 let status = nodePlayer.start(videoURL) if status == 0 { isPlaying = true print("Playback started") } else { print("Failed to start playback: \(status)") } } } /// 更新缓冲时间 private func updateBufferTime(_ newValue: Int) { nodePlayer.bufferTime = newValue } /// 更新缩放模式 private func updateScaleMode(_ newValue: Int) { nodePlayer.scaleMode = newValue } /// 更新音量 private func updateVolume(_ newValue: Float) { nodePlayer.volume = newValue / 100.0 } /// 清理资源 private func cleanup() { nodePlayer.stop() nodePlayer.detachView() print("Resources cleaned up") } } #Preview { LivePlayView() } ``` ## Demo下载 [SwiftDemo.zip](https://www.nodemedia.cn/doc/server/index.php?s=/api/attachment/visitFile&sign=8e48bc607dc4b8a508b364ece755d9ef "[SwiftDemo.zip")
嘿,我是小R,需要帮助随时找我哦
QQ客服:281269007
邮件支持
扫码加微信
回到顶部