Skip to content

准备工作

先安装依赖,然后将两个 xcframework 拷贝到xcode中。

安装依赖

CocoaPods

编辑 Podfile

ruby
target 'TargetName' do
# Uncomment the next line if you're using Swift
# use_frameworks!
    pod 'P2pEngine-iOS', '= 3.1.2'
end
target 'TargetName' do
# Uncomment the next line if you're using Swift
# use_frameworks!
    pod 'P2pEngine-iOS', '= 3.1.2'
end

然后,运行如下的命令:

bash
$ pod install
$ pod install

Carthage

编辑 Cartfile:

binary "https://web3lab.b-cdn.net/apple/SwarmCloudKit.json" ~> 3.1.2
binary "https://web3lab.b-cdn.net/apple/Datachannel.json" ~> 1.0.190
binary "https://web3lab.b-cdn.net/apple/SwarmCloudKit.json" ~> 3.1.2
binary "https://web3lab.b-cdn.net/apple/Datachannel.json" ~> 1.0.190

然后,运行如下的命令:

bash
carthage update --use-xcframeworks
carthage update --use-xcframeworks

这个命令会在 Carthage/Build 文件夹中生成编译好的XCFramework。
依次打开xcode的 General -> Frameworks, Libraries, and Embedded Content,将所有 XCFramework 拖进来。

Swift Package Manager

在 Package.swift 的 dependencies 中添加:

swift
dependencies: [
    .package(url: "https://github.com/swarm-cloud/SwarmCloudKit.git", branch: "main")
]
dependencies: [
    .package(url: "https://github.com/swarm-cloud/SwarmCloudKit.git", branch: "main")
]

手动导入

下载预编译好的 XCFramework

拷贝 framework

先解压,然后将 SwarmCloudKit.xcframework 和 datachannel_wrapper.xcframework 拖入 General -> Frameworks, Libraries, and Embedded Content

集成方法

SDK通过本地代理伺服器拦截数据请求的方式来进行P2P缓存和传输,所以需要在项目的info.plist中允许HTTP请求:

xml
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

引入 SwarmCloudKit

在工程的 AppDelegate.m 文件导入头文件:

swift
import SwarmCloudKit
import SwarmCloudKit
objective-c
#import <SwarmCloudKit/SwarmCloudKit-Swift.h>
#import <SwarmCloudKit/SwarmCloudKit-Swift.h>

初始化 SWCP2pEngine

在工程 AppDelegate.mapplication:didFinishLaunchingWithOptions: 方法中初始化:

swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    let config = P2pConfig(
        trackerZone: .Europe,       // Set HongKong or USA if you changed zone
        playlistTimeOffset: 0.0
    )
    P2pEngine.setup(token: YOUR_TOKEN, config: config)   // replace with your own token
    return true
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    let config = P2pConfig(
        trackerZone: .Europe,       // Set HongKong or USA if you changed zone
        playlistTimeOffset: 0.0
    )
    P2pEngine.setup(token: YOUR_TOKEN, config: config)   // replace with your own token
    return true
}
objective-c
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    P2pConfig* config = [P2pConfig defaultConfiguration];
    config.trackerZone = TrackerZoneEurope;     // Set HongKong or USA if you changed zone
    config.playlistTimeOffset = 0.0;
    [P2pEngine setupWithToken:YOUR_TOKEN config:config];  // replace with your own token
    return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    P2pConfig* config = [P2pConfig defaultConfiguration];
    config.trackerZone = TrackerZoneEurope;     // Set HongKong or USA if you changed zone
    config.playlistTimeOffset = 0.0;
    [P2pEngine setupWithToken:YOUR_TOKEN config:config];  // replace with your own token
    return YES;
}

其中 YOUR_TOKEN 是用于标识用户的字符串,请换成自己的token,点击这里查看如何注册 Appid 并获取 token

WARNING

如果媒体文件是由多个服务器生成的,为避免冲突,请在 P2pConfig 增加以下配置:

swift
config.useStrictHlsSegmentId = true
config.useStrictHlsSegmentId = true

转换地址

在代码中实例化AVPlayer之后(也可以是其他任何视频播放器),先将URL传给 SWCP2pEngine,之后将转化的本地URL传给播放器:

swift
let parsedUrl = P2pEngine.shared.parseStreamUrl("https://your_stream.m3u8")
_player = AVPlayer.init(url: URL(string: parsedUrl)!)
let parsedUrl = P2pEngine.shared.parseStreamUrl("https://your_stream.m3u8")
_player = AVPlayer.init(url: URL(string: parsedUrl)!)
objective-c
NSString *parsedUrl = [[P2pEngine sharedInstance] parseStreamUrl:@"https://your_stream.m3u8"];
_player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:parsedUrl]];
NSString *parsedUrl = [[P2pEngine sharedInstance] parseStreamUrl:@"https://your_stream.m3u8"];
_player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:parsedUrl]];

就这么简单,你的播放器已经具备P2P能力了!

示例

获取完整的示例程序

P2P无效问题排查步骤

请参考常见问题

粤ICP备18075581号