Tag: avfoundation

获取缩略图的video快照

我正在使用苹果提供的AVCam代码从iPhone摄像头录制video。 录像后,它被保存到照片库。 然后加载一个新的视图,在这里我需要有一个video的图像缩略图。 我有一个video的path: file://localhost/private/var/mobile/Applications/ED45DEFC-ABF9-4A5E-9102-21680CC1448E/tmp/output.mov 我似乎无法确定如何将video的第一帧作为缩略图使用。 任何帮助将非常感激,并感谢您的时间。 编辑 我结束了使用这个,我不知道为什么它横向返回图像? – (UIImage*)loadImage { AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidURL options:nil]; AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset]; NSError *err = NULL; CMTime time = CMTimeMake(1, 60); CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err]; NSLog(@"err==%@, imageRef==%@", err, imgRef); return [[UIImage alloc] initWithCGImage:imgRef]; }

使用AVAssetReader从远程资产读取(stream)

我的主要目标是从服务器stream式传输video,并在stream式传输时逐帧剪切(以便OpenGL可以使用)。 为此,我使用了在互联网上随处可见的代码(我记得它来自Apple的GLVideoFrame示例代码): NSArray * tracks = [asset tracks]; NSLog(@"%d", tracks.count); for(AVAssetTrack* track in tracks) { NSLog(@"type: %@", [track mediaType]); initialFPS = track.nominalFrameRate; width = (GLuint)track.naturalSize.width; height = (GLuint)track.naturalSize.height; NSError * error = nil; // _movieReader is a member variable @try { self._movieReader = [[[AVAssetReader alloc] initWithAsset:asset error:&error] autorelease]; } @catch (NSException *exception) { NSLog(@"%@ — […]

AVPlayer停止播放,不再恢复

在我的应用程序中,我必须播放存储在Web服务器上的audio文件。 我正在使用AVPlayer 。 我有所有的播放/暂停控制,所有的代表和观察员工作得很好。 在播放小audio文件时,一切都很好。 当一个长audio文件播放时,它也开始播放罚款,但几秒钟后AVPlayer暂停播放(最有可能缓冲)。 问题是它不能自行恢复。 它保持暂停状态,如果我再次手动按播放button它再次平稳播放。 我想知道为什么AVPlayer不能自动恢复,如何在不用用户再次按下播放button的情况下再次恢复audio? 谢谢。

简单的屏幕截图:只logging在UIView的图层内出现的video?

这个SO答案解决了如何做一个UIView的屏幕截图。 我们需要类似的东西,而不是一个单一的图像,目标是产生超过60秒的UIView内出现的一切video – 概念上只logging该UIView的图层,忽略其他层。 我们的video应用在用户正在logging的任何位置叠加图层,最终目标是生成将这些图层与原始video合并的主video。 但是,使用AVVideoCompositionCoreAnimationTool将图层与原始video合并非常非常慢:导出60秒的video需要10-20秒。 我们发现结合两个video(即,仅使用不带AVVideoCompositionCoreAnimationTool )是非常快的:〜1秒。 希望是创build一个独立的图层video,然后将其与原始video结合使用AVMutableComposition. 斯威夫特的答案是理想的,但不是必需的。

AVAssetResourceLoaderDelegate方法不能在设备上工作

我一直在研究一个简单的AVPlayer来播放encryption的HLS媒体。 我正在使用AVAssetResourceLoaderDelegate处理密钥检索过程,因此可以使用有效的密钥来播放encryption的媒体。 该程序完美地在模拟器上工作,但在设备上完全不起作用 。 这里是代码: – (void) playUrlByAVPlayer:(NSString *) videoUrl { NSURL *streamURL = [NSURL URLWithString:videoUrl]; AVURLAsset *asset = [AVURLAsset URLAssetWithURL:streamURL options:nil]; [asset.resourceLoader setDelegate:self queue:dispatch_get_main_queue()]; self.playerItem = [AVPlayerItem playerItemWithAsset:asset]; self.player = [AVPlayer playerWithPlayerItem:self.playerItem]; self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player]; [self.playerLayer setFrame:self.view.frame]; [self.view.layer addSublayer:self.playerLayer]; [self.player play]; } 经过一些debugging,我意识到委托方法shouldWaitForLoadingOfRequestedResource从未在设备上调用。 我已阅读其他相关问题: AVAssetResourceLoaderDelegate没有被调用 AVAssetResourceLoaderDelegate – 只请求前两个字节? 我试着把所有的代码放在dispatch_async , dispatch_get_main_queue块中,但是解决我的问题没有任何运气。 目前我的代码不包含任何调度队列块。 对这个问题有什么想法?

你如何发布一个MTAudioProcessingTap?

MTAudioProcessingTap的头文件说,它的初始化和准备callback将通过unspare和finalizecallback进行平衡。 然而,在苹果的例子中 ,这些callback永远不会被调用(我添加了日志logging,所以我可以检查)。 头文件说,当对象被释放时它们将被调用。 在苹果公司的例子中,Tap被传递到audioMixInputParameters中的retain参数中,该参数被传递到audio混合中,不再公开访问: MTAudioProcessingTapRef audioProcessingTap; if (noErr == MTAudioProcessingTapCreate(kCFAllocatorDefault, &callbacks, kMTAudioProcessingTapCreationFlag_PreEffects, &audioProcessingTap)) { audioMixInputParameters.audioTapProcessor = audioProcessingTap; CFRelease(audioProcessingTap); audioMix.inputParameters = @[audioMixInputParameters]; _audioMix = audioMix; } 因此,我希望AudioMix负责在其自己的dealloc方法中释放它,并在发布关联的PlayerItem时释放AudioMix。 苹果公司的例子使用一个AVPlayer,只玩过一个项目,所以也许不需要直接释放任何东西。 但在我的情况下,我正在使用AVQueuePlayer,所以我不断传递新的AVPlayerItems。 我认为它泄露了我为每个玩家项目(以及相关的audio单元)创build的Taps,即使玩家项目被释放。 什么是正确的方式来释放MTAudioProcessingTap并得到它的无与伦比的callback触发,当我完成相关的播放器项目? 更新 :我发现它实际上仍然可以通过audio混合访问,但是像这样释放它并不会触发非预期和最终callback: ((AVMutableAudioMixInputParameters *)audioMix.inputParameters[0]).audioTapProcessor = nil; 这也不是: MTAudioProcessingTapRef audioProcessingTap = ((AVMutableAudioMixInputParameters *)audioMix.inputParameters[0]).audioTapProcessor; CFRelease(audioProcessingTap);

iPhone检测音量键按下。

我需要检测用户何时按下硬件音量键,(App Store安全方法)我已经尝试了一些没有运气的东西。 你知道如何实现这样的function吗? 目前我正在注册通知,但他们似乎并没有被调用。 这是我的代码: AudioSessionInitialize(NULL, NULL, NULL, NULL); NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; [notificationCenter addObserver:self selector:@selector(volumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil]; 接收方法是: -(void)volumeChanged:(NSNotification *)notification{ NSLog(@"YAY, VOLUME WAS CHANGED");} 任何提示将非常感谢。

将UIImage转换为CMSampleBufferRef

我正在使用AVFoundation进行video录制。 我必须裁剪video到320×280。 我得到的CMSampleBufferRef并使用下面的代码将其转换为UIImage。 CGImageRef _cgImage = [self imageFromSampleBuffer:sampleBuffer]; UIImage *_uiImage = [UIImage imageWithCGImage:_cgImage]; CGImageRelease(_cgImage); _uiImage = [_uiImage resizedImageWithSize:CGSizeMake(320, 280)]; CMSampleBufferRef croppedBuffer = /* NEED HELP WITH THIS */ [_videoInput appendSampleBuffer:sampleBuffer]; // _videoInput is a AVAssetWriterInput imageFromSampleBuffer:方法如下所示: – (CGImageRef) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer // Create a CGImageRef from sample buffer data { CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); CVPixelBufferLockBaseAddress(imageBuffer,0); // […]

在Xcode中设置前置摄像头

在Xcode中设置前置摄像头。 我怎样才能做到这一点? 在Xcode中设置前置摄像头。 AVCaptureSession *session = [AVCaptureSession new]; if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) [session setSessionPreset:AVCaptureSessionPreset640x480]; else [session setSessionPreset:AVCaptureSessionPresetPhoto]; AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; require( error == nil, bail ); isUsingFrontFacingCamera = NO; if ( [session canAddInput:deviceInput] ) [session addInput:deviceInput]; stillImageOutput = [AVCaptureStillImageOutput new]; [stillImageOutput addObserver:self forKeyPath:@"capturingStillImage" options:NSKeyValueObservingOptionNew context:AVCaptureStillImageIsCapturingStillImageContext]; […]

无法在iOS5中从CIImage创buildUIImage

我正在使用AVFoundation框架。 在我的示例缓冲区委托我有以下代码: -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{ CVPixelBufferRef pb = CMSampleBufferGetImageBuffer(sampleBuffer); CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pb]; self.imageView.image = [UIImage imageWithCIImage:ciImage]; } 我能够使用CIImage运行人脸检测器等,但它不显示在UIImageView … imageView保持白色。 关于这个问题的任何想法? 我正在使用以下来设置我的会话: self.session = [[AVCaptureSession alloc] init]; self.session.sessionPreset = AVCaptureSessionPreset640x480; self.videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil]; self.frameOutput = [[AVCaptureVideoDataOutput alloc] init]; self.frameOutput.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];