Skip to content

事前準備

まず依存関係をインストールし、次に2つのxcframeworkをxcodeにコピーします。

依存関係のインストール

Swift Package Manager

Package.swift の dependencies に追加します:

swift
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

続いて、以下のコマンドを実行します:

bash
carthage update --use-xcframeworks

このコマンドにより、Carthage/Build フォルダにビルド済みのXCFrameworkが生成されます。
xcodeの General -> Frameworks, Libraries, and Embedded Content を開き、すべてのXCFrameworkをドラッグして追加してください。

CocoaPods

Podfile を編集します

ruby
target 'TargetName' do
# Uncomment the next line if you're using Swift
# use_frameworks!
    pod 'SwarmCloudKit', '~> 3.0'
end

続いて、以下のコマンドを実行します:

bash
$ pod install

手動インポート

ビルド済みのXCFrameworkをダウンロード

frameworkのコピー

まず解凍し、SwarmCloudKit.xcframework と datachannel_wrapper.xcframework を General -> Frameworks, Libraries, and Embedded Content にドラッグしてください

統合方法

SwarmCloudKitの導入

プロジェクトの AppDelegate.m ファイルでヘッダーファイルをインポートします:

swift
import SwarmCloudKit
objective-c
#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
    )
    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
    [P2pEngine setupWithToken:YOUR_TOKEN config:config];  // replace with your own token
    return YES;
}

YOUR_TOKEN はユーザーを識別するための文字列です。自分のtokenに置き換えてください。こちらをクリックして Appid の登録方法と token の取得方法を確認してください。

アドレスの変換

コード内でAVPlayerをインスタンス化した後(他の任意の動画プレイヤーでも構いません)、まずURLをSWCP2pEngineに渡し、その後変換されたローカルURLをプレイヤーに渡します:

swift
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]];

これだけで、あなたのプレイヤーはすでにP2P機能を備えました!

サンプル

完全なサンプルプログラムはこちら。

P2Pが機能しない場合のトラブルシューティング手順

よくある質問を参照してください

粤ICP备18075581号