视频不在设备上播放,但在播放 ov 模拟器
Posted
技术标签:
【中文标题】视频不在设备上播放,但在播放 ov 模拟器【英文标题】:Video not playing on device but playing ov simulator 【发布时间】:2015-02-02 08:21:15 【问题描述】:我真的坚持这一点,我已经合并了视频(保存在 xcode 中的 mp4 类型和 mov - 来自用户照片库)和音频(保存在 xcode 中的 mp3 类型)。在模拟器上它工作正常并显示视频和音频,但在设备上它不显示视频只播放声音。这里是合并的代码:` AVMutableComposition* mixComposition = [AVMutableComposition composition];
//Audio
//Now first load your audio file using AVURLAsset. Make sure you give the correct path of your videos.
NSURL *audio_url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:_video.musicFileName ofType:@"mp3"]];
AVURLAsset *audioAsset = [[AVURLAsset alloc] initWithURL:audio_url options:nil];
CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
//Now we are creating the first AVMutableCompositionTrack containing our audio and add it to our AVMutableComposition object.
AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];
//Video
AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
NSString *fistPartMovieFilename = @"firstpart";
NSString *lastPartMovieFilename = @"lastpart";
//[_video.selectedVideosFileName insertObject:fistPartMovieFilename atIndex:0];
//[_video.selectedVideosFileName addObject:lastPartMovieFilename];
NSMutableArray *timeRanges = [NSMutableArray arrayWithCapacity:_video.selectedVideosFileName.count];
NSMutableArray *tracks = [NSMutableArray arrayWithCapacity:_video.selectedVideosFileName.count];
NSLog(@"_video.selectedVideosFileName %@",_video.selectedVideosFileName);
for (int i=0; i<[_video.selectedVideosFileName count]; i++)
AVURLAsset *assetClip;
NSString *fileName = [_video.selectedVideosFileName objectAtIndex:i];
if ([[_video.selectedVideosFileName objectAtIndex:i] isKindOfClass:[NSURL class]])
assetClip = [AVAsset assetWithURL:[_video.selectedVideosFileName objectAtIndex:i]];
else
assetClip = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithString:fileName] ofType:@"mp4"]] options:nil];
AVAssetTrack *clipVideoTrack = [[assetClip tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[timeRanges addObject:[NSValue valueWithCMTimeRange:CMTimeRangeMake(kCMTimeZero, assetClip.duration)]];
[tracks addObject:clipVideoTrack];
NSLog(@"timeRanges %@",timeRanges);
NSLog(@"tracks %@",tracks);
NSLog(@"assetClip %@",assetClip);
NSLog(@"clipVideoTrack %@",clipVideoTrack);
NSError *error = nil;
BOOL sucsess = [compositionVideoTrack insertTimeRanges:timeRanges
ofTracks:tracks
atTime:kCMTimeZero
error:&error];
if (!sucsess || error)
NSLog(@"video error %@", error);
我在尝试设备时遇到的错误是:视频错误 错误域=AVFoundationErrorDomain 代码=-11800 “操作无法完成” UserInfo=0x1704707c0 NSUnderlyingError=0x174247050 "操作无法完成。(OSStatus 错误 -12780.)", NSLocalizedFailureReason=发生未知错误 (-12780), NSLocalizedDescription=操作无法完成
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *outputFilePath = [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Zagoori-%d.mov",arc4random() % 1000]];
NSURL *outputFileUrl = [NSURL fileURLWithPath:outputFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath])
[[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];
//Now create an AVAssetExportSession object that will save your final video at specified path.
AVAssetExportSession *_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition
presetName:AVAssetExportPresetLowQuality];
NSLog(@"supportedFileTypes %@", [_assetExport supportedFileTypes]);
_assetExport.outputFileType = @"com.apple.quicktime-movie";
_assetExport.outputURL = outputFileUrl;
[_assetExport exportAsynchronouslyWithCompletionHandler:^
dispatch_async(dispatch_get_main_queue(), ^
switch ([_assetExport status])
case AVAssetExportSessionStatusExporting:
NSLog(@"Export in progress ");
case AVAssetExportSessionStatusFailed:
NSLog(@"Export failed (%ld): %@", (long)[[_assetExport error] code], [[_assetExport error] localizedFailureReason]);
[_assetExport cancelExport];
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export canceled");
break;
case AVAssetExportSessionStatusCompleted:
NSLog(@"Export done");
[self exportDidFinish:_assetExport];
// NSLog (@"finished writing %f", [dtStartDate timeIntervalSinceNow]);
break;
case AVAssetExportSessionStatusWaiting:
NSLog (@"AVAssetExportSessionStatusWaiting");
break;
case AVAssetExportSessionStatusUnknown:
NSLog (@"AVAssetExportSessionStatusUnknown");
break;
);
];
-(void)loadMoviePlayer:(NSURL*)moviePath
_moviePlayerController = [[MPMoviePlayerController alloc]
initWithContentURL:moviePath];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(introMovieFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayerController];
_moviePlayerController.view.hidden = NO;
_moviePlayerController.view.frame = CGRectMake(0, 0, _videoView.frame.size.width,
_videoView.frame.size.height);
_moviePlayerController.view.backgroundColor = [UIColor clearColor];
_moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
_moviePlayerController.fullscreen = NO;
[_moviePlayerController prepareToPlay];
[_moviePlayerController readyForDisplay];
[_moviePlayerController setControlStyle:MPMovieControlStyleDefault];
_moviePlayerController.shouldAutoplay = YES;
[_videoView addSubview:_moviePlayerController.view];
[_videoView setHidden:NO];
我真的不知道问题可能是什么,请帮助! :)
【问题讨论】:
【参考方案1】:所以我发现了问题,它是由 AVURLAsset 实例引起的
由于 AVURLAsset *assetClip 在 for 循环内部,它在外部无效,我提取的轨道也无效。
我将assetClip 保存在一个数组中,这解决了问题。
【讨论】:
以上是关于视频不在设备上播放,但在播放 ov 模拟器的主要内容,如果未能解决你的问题,请参考以下文章
迅速。在模拟器上播放良好的 mp3 文件,但在设备上播放时没有音量。 AVPlayer
通过 iOS (Swift) 中的 AVAudioPlayerNode 播放缓冲区时出现可听故障 *在模拟器中工作,但不在设备上
AVPlayer 无法在 iOS8 上播放 NSDocuments 目录中的视频