Iphone 播放电影会停止avcapturesession录制
我有一个iOS应用程序,可以在后台录制来自前置摄像头的视频,并且工作正常。但是现在我尝试同时播放一段短的mp4,使用MPMoviePlayerController播放会停止捕获会话 我尝试了AVPlayer,结果是一样的。 我还设置了[[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayAndRecord错误:nil]; 还是不走运。有没有人面对并解决了同样的问题。 谢谢你的建议 使用ios5sdkIphone 播放电影会停止avcapturesession录制,iphone,ios,mpmovieplayercontroller,avcapturesession,Iphone,Ios,Mpmovieplayercontroller,Avcapturesession,我有一个iOS应用程序,可以在后台录制来自前置摄像头的视频,并且工作正常。但是现在我尝试同时播放一段短的mp4,使用MPMoviePlayerController播放会停止捕获会话 我尝试了AVPlayer,结果是一样的。 我还设置了[[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayAndRecord错误:nil]; 还是不走运。有没有人面对并解决了同样的问题。 谢谢你的建议 使用ios5sdk #impo
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *recButton=[[UIButton alloc] initWithFrame:CGRectMake(10,200, 200,40)] ;
recButton.backgroundColor = [UIColor blackColor];
[recButton addTarget:self action:@selector(startRecording) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:recButton];
isRecording=NO;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(void) viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBarHidden = YES;
}
-(void) viewWillDisappear:(BOOL)animated
{
self.navigationController.navigationBarHidden = NO;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
} else {
return YES;
}
}
#pragma mark video playing
-(void) startRecording
{
if (isRecording) {
[self stopVideoRecording];
isRecording=NO;
}
else
{
[self initCaptureSession];
[self startVideoRecording];
isRecording=YES;
}
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"new"
ofType:@"mov"]];
[self playMovieAtURL:url];
}
-(void) playMovieAtURL: (NSURL*) theURL {
player =
[[MPMoviePlayerController alloc] initWithContentURL: theURL ];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
player.scalingMode = MPMovieScalingModeAspectFill;
player.controlStyle = MPMovieControlStyleNone;
[player prepareToPlay];
// Register for the playback finished notification
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(myMovieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: player];
[player.view setFrame: self.view.bounds];
[self.view addSubview:player.view];
// Movie playback is asynchronous, so this method returns immediately.
[player play];
}
// When the movie is done, release the controller.
-(void) myMovieFinishedCallback: (NSNotification*) aNotification
{
MPMoviePlayerController* theMovie = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver: self
name: MPMoviePlayerPlaybackDidFinishNotification
object: theMovie];
[player.view removeFromSuperview];
[self stopVideoRecording];
}
#pragma mark -
#pragma mark recording
-(void) initCaptureSession
{
NSLog(@"Setting up capture session");
captureSession = [[AVCaptureSession alloc] init];
//----- ADD INPUTS -----
NSLog(@"Adding video input");
//ADD VIDEO INPUT
AVCaptureDevice *VideoDevice = [self frontFacingCameraIfAvailable ];
//[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (VideoDevice)
{
NSError *error;
videoInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:VideoDevice error:&error];
if (!error)
{
if ([captureSession canAddInput:videoInputDevice])
[captureSession addInput:videoInputDevice];
else
NSLog(@"Couldn't add video input");
}
else
{
NSLog(@"Couldn't create video input");
}
}
else
{
NSLog(@"Couldn't create video capture device");
}
//ADD AUDIO INPUT
NSLog(@"Adding audio input");
AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
NSError *error = nil;
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error];
if (audioInput)
{
[captureSession addInput:audioInput];
}
//----- ADD OUTPUTS ---
//ADD MOVIE FILE OUTPUT
NSLog(@"Adding movie file output");
movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
// Float64 TotalSeconds = 60; //Total seconds
// int32_t preferredTimeScale = 30; //Frames per second
// CMTime maxDuration = CMTimeMakeWithSeconds(TotalSeconds, preferredTimeScale); //<<SET MAX DURATION
// movieFileOutput.maxRecordedDuration = maxDuration;
movieFileOutput.minFreeDiskSpaceLimit = 1024 * 1024; //<<SET MIN FREE SPACE IN BYTES FOR RECORDING TO CONTINUE ON A VOLUME
if ([captureSession canAddOutput:movieFileOutput])
[captureSession addOutput:movieFileOutput];
//SET THE CONNECTION PROPERTIES (output properties)
[self CameraSetOutputProperties]; //(We call a method as it also has to be done after changing camera)
//----- SET THE IMAGE QUALITY / RESOLUTION -----
//Options:
// AVCaptureSessionPresetHigh - Highest recording quality (varies per device)
// AVCaptureSessionPresetMedium - Suitable for WiFi sharing (actual values may change)
// AVCaptureSessionPresetLow - Suitable for 3G sharing (actual values may change)
// AVCaptureSessionPreset640x480 - 640x480 VGA (check its supported before setting it)
// AVCaptureSessionPreset1280x720 - 1280x720 720p HD (check its supported before setting it)
// AVCaptureSessionPresetPhoto - Full photo resolution (not supported for video output)
NSLog(@"Setting image quality");
[captureSession setSessionPreset:AVCaptureSessionPresetMedium];
if ([captureSession canSetSessionPreset:AVCaptureSessionPreset640x480]) //Check size based configs are supported before setting them
[captureSession setSessionPreset:AVCaptureSessionPreset640x480];
//----- START THE CAPTURE SESSION RUNNING -----
[captureSession startRunning];
}
//********** CAMERA SET OUTPUT PROPERTIES **********
- (void) CameraSetOutputProperties
{
AVCaptureConnection *CaptureConnection=nil;
//SET THE CONNECTION PROPERTIES (output properties)
NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"5.0.0" options: NSNumericSearch];
if (order == NSOrderedSame || order == NSOrderedDescending) {
// OS version >= 5.0.0
CaptureConnection = [movieFileOutput connectionWithMediaType:AVMediaTypeVideo];
// if (CaptureConnection.supportsVideoMinFrameDuration)
// CaptureConnection.videoMinFrameDuration = CMTimeMake(1, CAPTURE_FRAMES_PER_SECOND);
// if (CaptureConnection.supportsVideoMaxFrameDuration)
// CaptureConnection.videoMaxFrameDuration = CMTimeMake(1, CAPTURE_FRAMES_PER_SECOND);
// if (CaptureConnection.supportsVideoMinFrameDuration)
// {
// // CMTimeShow(CaptureConnection.videoMinFrameDuration);
// // CMTimeShow(CaptureConnection.videoMaxFrameDuration);
// }
} else {
// OS version < 5.0.0
CaptureConnection = [self connectionWithMediaType:AVMediaTypeVideo fromConnections:[movieFileOutput connections]];
}
//Set landscape (if required)
if ([CaptureConnection isVideoOrientationSupported])
{
AVCaptureVideoOrientation orientation = AVCaptureVideoOrientationPortrait;// AVCaptureVideoOrientationLandscapeRight; //<<<<<SET VIDEO ORIENTATION IF LANDSCAPE
[CaptureConnection setVideoOrientation:orientation];
}
//Set frame rate (if requried)
//CMTimeShow(CaptureConnection.videoMinFrameDuration);
//CMTimeShow(CaptureConnection.videoMaxFrameDuration);
}
- (void) startVideoRecording
{
//Create temporary URL to record to
NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:outputPath])
{
NSError *error;
if ([fileManager removeItemAtPath:outputPath error:&error] == NO)
{
//Error - handle if requried
NSLog(@"file remove error");
}
}
//Start recording
[movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];
}
-(void) stopVideoRecording
{
[movieFileOutput stopRecording];
}
//********** DID FINISH RECORDING TO OUTPUT FILE AT URL **********/
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput
didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
fromConnections:(NSArray *)connections
error:(NSError *)error
{
NSLog(@"didFinishRecordingToOutputFileAtURL - enter");
BOOL RecordedSuccessfully = YES;
if ([error code] != noErr)
{
// A problem occurred: Find out if the recording was successful.
id value = [[error userInfo] objectForKey:AVErrorRecordingSuccessfullyFinishedKey];
if (value)
{
RecordedSuccessfully = [value boolValue];
}
}
if (RecordedSuccessfully)
{
//----- RECORDED SUCESSFULLY -----
NSLog(@"didFinishRecordingToOutputFileAtURL - success");
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:outputFileURL])
{
[library writeVideoAtPathToSavedPhotosAlbum:outputFileURL
completionBlock:^(NSURL *assetURL, NSError *error)
{
if (error)
{
NSLog(@"File save error");
}
else
{
recordedVideoURL=assetURL;
}
}];
}
else {
NSString *assetURL=[self copyFileToDocuments:outputFileURL];
if(assetURL!=nil)
{
recordedVideoURL=[NSURL URLWithString:assetURL];
}
}
}
}
- (NSString*) copyFileToDocuments:(NSURL *)fileURL
{
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss"];
NSString *destinationPath = [documentsDirectory stringByAppendingFormat:@"/output_%@.mov", [dateFormatter stringFromDate:[NSDate date]]];
NSError *error;
if (![[NSFileManager defaultManager] copyItemAtURL:fileURL toURL:[NSURL fileURLWithPath:destinationPath] error:&error]) {
NSLog(@"File save error %@", [error localizedDescription]);
return nil;
}
return destinationPath;
}
- (AVCaptureConnection *)connectionWithMediaType:(NSString *)mediaType fromConnections:(NSArray *)connections
{
for ( AVCaptureConnection *connection in connections ) {
for ( AVCaptureInputPort *port in [connection inputPorts] ) {
if ( [[port mediaType] isEqual:mediaType] ) {
return connection;
}
}
}
return nil;
}
- (AVCaptureDevice *)frontFacingCameraIfAvailable
{
// look at all the video devices and get the first one that's on the front
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *captureDevice = nil;
for (AVCaptureDevice *device in videoDevices)
{
if (device.position == AVCaptureDevicePositionFront)
{
captureDevice = device;
break;
}
}
// couldn't find one on the front, so just get the default video device.
if ( ! captureDevice)
{
captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
return captureDevice;
}
#pragma mark -
@end
#导入“ViewController.h”
@界面视图控制器()
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
UIButton*recButton=[[UIButton alloc]initWithFrame:CGRectMake(10200200,40)];
recButton.backgroundColor=[UIColor blackColor];
[recButton addTarget:self action:@selector(startRecording)for ControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:recButton];
isRecording=否;
}
-(无效)视图卸载
{
[超级视频下载];
//释放主视图的所有保留子视图。
}
-(无效)视图将显示:(BOOL)动画
{
self.navigationController.navigationBarHidden=是;
}
-(无效)视图将消失:(BOOL)已设置动画
{
self.navigationController.navigationBarHidden=否;
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone){
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}否则{
返回YES;
}
}
#布拉格标记视频播放
-(无效)开始记录
{
如果(isRecording){
[自动停止录像];
isRecording=否;
}
其他的
{
[自启动捕获会话];
[自启动视频录制];
isRecording=是;
}
NSURL*url=[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@“新建”
类型:@“mov”]];
[自拍电影:url];
}
-(无效)播放电影自然:(NSURL*)URL{
玩家=
[[MPMoviePlayerController alloc]initWithContentURL:theURL];
[[AVAudioSession sharedInstance]设置类别:AVAudioSessionCategoryPlayAndRecord错误:无];
player.scalingMode=MPMovieScalingModeAspectFill;
player.controlStyle=MPMovieControlStyleNone;
[玩家准备玩];
//注册播放完成通知
[[NSNotificationCenter defaultCenter]
addObserver:self
选择器:@selector(myMovieFinishedCallback:)
名称:MPMoviePlayerPlaybackDidFinishNotification
对象:玩家];
[player.view setFrame:self.view.bounds];
[self.view addSubview:player.view];
//电影播放是异步的,因此此方法立即返回。
[玩家游戏];
}
//电影结束后,释放控制器。
-(作废)myMovieFinishedCallback:(NSNotification*)通知
{
MPMoviePlayerController*theMovie=[aNotification object];
[[NSNotificationCenter defaultCenter]
移除观察者:自我
名称:MPMoviePlayerPlaybackDidFinishNotification
对象:theMovie];
[player.view从SuperView移除];
[自动停止录像];
}
#布拉格标记-
#杂注标记记录
-(无效)初始捕获会话
{
NSLog(@“设置捕获会话”);
captureSession=[[AVCaptureSession alloc]init];
//-----添加输入-----
NSLog(“添加视频输入”);
//添加视频输入
AVCaptureDevice*VideoDevice=[self-FrontFacingCameraiAvailable];
//[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if(视频设备)
{
n错误*错误;
videoInputDevice=[AVCaptureDeviceInputDeviceInputWithDevice:VideoDevice错误:&错误];
如果(!错误)
{
if([captureSession canAddInput:videoInputDevice])
[captureSession addInput:videoInputDevice];
其他的
NSLog(@“无法添加视频输入”);
}
其他的
{
NSLog(@“无法创建视频输入”);
}
}
其他的
{
NSLog(@“无法创建视频捕获设备”);
}
//添加音频输入
NSLog(“添加音频输入”);
AVCaptureDevice*audioCaptureDevice=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
n错误*错误=nil;
AVCaptureDeviceInput*audioInput=[AVCaptureDeviceInputDeviceInputWithDevice:AudioCaptureDeviceError:&error];
中频(音频输入)
{
[捕获会话附加输入:音频输入];
}
//-----增加产出---
//添加电影文件输出
NSLog(@“添加电影文件输出”);
movieFileOutput=[[AVCaptureMovieFileOutput alloc]init];
//Float64 TotalSeconds=60;//总秒数
//int32\u t preferredTimeScale=30;//每秒帧数
//CMTime maxDuration=CMTimeMakeWithSeconds(TotalSeconds,preferredTimeScale);//我也有这个问题,我不知道如何解决,但我知道问题在这里:
[captureSession addInput:audioInput];
如果你删除这行代码,它将工作良好,我认为这是音频混音或一些音频问题
我仍然在寻找答案
我在这里找到了答案:,它有效
但是你记得添加AudioToolbox.framework
,可能对你有帮助