2016-10-18 12 views
0

2つのURL()を使用してAVAssetオブジェクトを作成することはできますか?オーディオ用と動画用の2つです。別の動画と音声URL(iOS)を使用したAVAセット

私はAVMutableCompositionで試しましたが、ビデオ+オーディオの再生を開始する前にコンテンツ全体を最初にロードし、どこかにバッファリングしているようです。 AVCompositionのドキュメントでは、ファイルベースのアセットを組み合わせることができますが、私はURLベースのアセットを結合する方法が必要です。

コンテンツ全体を読み込む前に再生を開始するには、AVCompositionに設定できるオプションがありますか?

編集

これは私がそれを試みた方法です:あなたが使用

NSDictionary *urlAssetOptions = @{AVURLAssetPreferPreciseDurationAndTimingKey: [NSNumber numberWithBool:NO]}; 

AVMutableComposition *composition = [AVMutableComposition composition]; 


NSURL *audioUrl = [NSURL URLWithString:@"http://..."]; 
AVURLAsset *audioAsset = [AVURLAsset URLAssetWithURL:audioUrl options:urlAssetOptions]; 

AVMutableCompositionTrack *audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
[audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil]; 


NSURL *videoUrl = [NSURL URLWithString:@"http://..."]; 
AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:videoUrl options:urlAssetOptions]; 

AVMutableCompositionTrack *videoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil]; 


AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:composition]; 
+0

最も簡単なway..create 2 AVAssets。\ –

+0

@Gagan_iOSは、どのように私はこれら2つのAVAssetsとAVPlayerItemオブジェクトを作成する必要がありますか? – selcuksinan

答えて

0

ソリューションは、可変の組成物の作成を開始するために全体のコンテンツをロードする必要はありませんや構図の再生を開始しますあなたは作成しました。ただし、各ファイルの期間とトラックを決定するためにメディアファイルの一部を読み込む必要があります。

以下は、Googleが見つけたmp3ファイルとmp4ファイルにURLを使用して、変更可能な構図を作成してAVPlayerViewControllerに渡す作業コードです。コードを実行すると、再生がかなり早く始まることがわかりますが、ビデオタイムラインをジャンプすると、要求された時間だけデータをロードするのに時間がかかることがわかります。

NSURL *audioURL = [NSURL URLWithString:@"http://www.mfiles.co.uk/mp3-downloads/Toccata-and-Fugue-Dm.mp3"]; 
AVAsset *audioAsset = [AVAsset assetWithURL:audioURL]; 

NSURL *videoURL = [NSURL URLWithString:@"http://thv1.uloz.to/6/c/4/6c4b50308843dd29c9176cc2c4961155.360.mp4?fileId=20389770"]; 
AVAsset *videoAsset = [AVAsset assetWithURL:videoURL]; 

CMTime duration; 
if (CMTimeGetSeconds(audioAsset.duration) < CMTimeGetSeconds(videoAsset.duration)) { 
    duration = audioAsset.duration; 
} else { 
    duration = videoAsset.duration; 
} 

NSError *error; 

AVMutableComposition* mixAsset = [[AVMutableComposition alloc] init]; 

AVMutableCompositionTrack* audioTrack = [mixAsset addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
[audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, duration) ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error: &error]; 

AVMutableCompositionTrack* videoTrack = [mixAsset addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error: &error]; 

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:mixAsset]; 

AVPlayerViewController* playerController = [AVPlayerViewController new]; 
playerController.player = [AVPlayer playerWithPlayerItem:playerItem]; 

[self presentViewController:playerController animated:YES completion:nil]; 
関連する問題