2016-10-18 4 views
0

두 개의 URL이AVAsset 개체를 만들 수 있나요? 하나는 오디오 용이고 다른 하나는 비디오 트랙 용입니까?별도의 동영상 및 오디오 URL이있는 AVAs

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이 AVAssets. \ –

+0

@Gagan_iOS 어떻게 이들 두 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]; 
관련 문제