事前準備
まず依存関係をインストールし、次に2つのxcframeworkをxcodeにコピーします。
依存関係のインストール
Swift Package Manager
Package.swift の dependencies に追加します:
dependencies: [
.package(url: "https://github.com/swarm-cloud/SwarmCloudKit.git", branch: "main")
]Carthage
Cartfile を編集します:
binary "https://cdn.swarmcloud.net/apple/SwarmCloudKit.json" ~> 3.6.0
binary "https://cdn.swarmcloud.net/apple/Datachannel.json" ~> 1.0.232続いて、以下のコマンドを実行します:
carthage update --use-xcframeworksこのコマンドにより、Carthage/Build フォルダにビルド済みのXCFrameworkが生成されます。
xcodeの General -> Frameworks, Libraries, and Embedded Content を開き、すべてのXCFrameworkをドラッグして追加してください。
CocoaPods
Podfile を編集します
target 'TargetName' do
# Uncomment the next line if you're using Swift
# use_frameworks!
pod 'SwarmCloudKit', '~> 3.0'
end続いて、以下のコマンドを実行します:
$ pod install手動インポート
ビルド済みのXCFrameworkをダウンロード
frameworkのコピー
まず解凍し、SwarmCloudKit.xcframework と datachannel_wrapper.xcframework を General -> Frameworks, Libraries, and Embedded Content にドラッグしてください
統合方法
SwarmCloudKitの導入
プロジェクトの AppDelegate.m ファイルでヘッダーファイルをインポートします:
import SwarmCloudKit#import <SwarmCloudKit/SwarmCloudKit-Swift.h>SWCP2pEngineの初期化
プロジェクトの AppDelegate.m の application:didFinishLaunchingWithOptions: メソッド内で初期化します:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let config = P2pConfig(
trackerZone: .Europe // Set HongKong or USA if you changed zone
)
P2pEngine.setup(token: YOUR_TOKEN, config: config) // replace with your own token
return true
}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
P2pConfig* config = [P2pConfig defaultConfiguration];
config.trackerZone = TrackerZoneEurope; // Set HongKong or USA if you changed zone
[P2pEngine setupWithToken:YOUR_TOKEN config:config]; // replace with your own token
return YES;
}YOUR_TOKEN はユーザーを識別するための文字列です。自分のtokenに置き換えてください。こちらをクリックして Appid の登録方法と token の取得方法を確認してください。
アドレスの変換
コード内でAVPlayerをインスタンス化した後(他の任意の動画プレイヤーでも構いません)、まずURLをSWCP2pEngineに渡し、その後変換されたローカルURLをプレイヤーに渡します:
let parsedUrl = P2pEngine.shared.parseStreamUrl("https://your_stream.m3u8")
_player = AVPlayer.init(url: URL(string: parsedUrl)!)NSString *parsedUrl = [[P2pEngine sharedInstance] parseStreamUrl:@"https://your_stream.m3u8"];
_player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:parsedUrl]];これだけで、あなたのプレイヤーはすでにP2P機能を備えました!
サンプル
完全なサンプルプログラムはこちら。
P2Pが機能しない場合のトラブルシューティング手順
よくある質問を参照してください
