我目前正在开发一个 iPhone 应用程序,它只播放 .mp4 的音轨。播放器开始播放,但我听不到任何声音。
这是代码:
NSURL *videoUrl = [NSURL URLWithString:@"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
AVMutableComposition* composition = [AVMutableComposition composition];
AVMutableCompositionTrack *track = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVURLAsset* videoAsset = [AVURLAsset URLAssetWithURL:videoUrl options:nil];
AVAssetTrack *audioTrack = [[videoAsset tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0];
NSError *error = nil;
BOOL success = [track insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:audioTrack atTime:kCMTimeZero error:&error];
if (!success)
{
NSLog(@"error: %@", error);
}
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:composition];
self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
[self.player play];