0

我有一個播放一些背景音樂的應用程序,當某個按鈕被按下時,它會使用導航控制器推動一個新的視圖。該新視圖有一個MPMoviePlayerController和一個帶共享AudioSession的AVCaptureSession。在這個觀點被駁回之後,背景中的聲音相比之下真的很軟。這是什麼原因導致音量在演奏後變得很低調?MPMoviePlayerController導致所有其他音頻變軟(MP完成後)

NSError* error4 = nil; 
AVAudioSession* audioSession = [AVAudioSession sharedInstance]; 
if (![audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error4]) { 
    NSLog(@"AVAudioSession setCategory failed: %@", [error4 localizedDescription]); 
} 

// Set audio session property "allow mixing" to true so audio can be recorded while it is playing 
UInt32 allowMixing = true; 
OSStatus status = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixing), &allowMixing); 
if (status != kAudioSessionNoError) { 
    NSLog(@"AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers) failed: %ld", status); 
} 

// Activate the audio session 
error4 = nil; 
if (![audioSession setActive:YES error:&error4]) { 
    NSLog(@"AVAudioSession setActive:YES failed: %@", [error4 localizedDescription]); 
} 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 

NSString *proud = [[documentsDirectoryPath stringByAppendingPathComponent:@"imissyou"] stringByAppendingPathComponent:selectedCountry]; 

NSURL *movieURL = [[NSURL fileURLWithPath:proud] retain]; 
player = 

[[MPMoviePlayerController alloc] initWithContentURL: movieURL]; 
player.useApplicationAudioSession=YES; 

[player prepareToPlay]; 
player.controlStyle = MPMovieControlStyleNone; 
player.allowsAirPlay = NO; 
player.scalingMode = MPMovieScalingModeFill; 

player.view.frame = self.view.frame; 

[self.view insertSubview:player.view belowSubview:vImagePreview]; 

[player setFullscreen:YES animated:YES]; 

// ... 

[[NSNotificationCenter defaultCenter] 
addObserver:self 
selector:@selector(movieFinishedCallback:) 
name:MPMoviePlayerPlaybackDidFinishNotification 
object:player]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:player]; 


[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(moviePlayerWillExitFullscreen:) 
              name:MPMoviePlayerWillExitFullscreenNotification 
              object:player]; 


[player play]; 


session = [[AVCaptureSession alloc] init]; 
[session beginConfiguration]; 
session.sessionPreset = AVCaptureSessionPresetMedium; 

CALayer *viewLayer = self.vImagePreview.layer; 
NSLog(@"viewLayer = %@", viewLayer); 

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; 
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
captureVideoPreviewLayer.frame = self.vImagePreview.bounds; 
[captureVideoPreviewLayer setCornerRadius:14]; 
[captureVideoPreviewLayer setBorderWidth:3.0]; 
[captureVideoPreviewLayer setBorderColor:[[UIColor whiteColor] CGColor]]; 
[[vImagePreview layer] setCornerRadius:14]; 


[[vImagePreview layer] setBorderWidth:3.0]; 


[[vImagePreview layer] setBorderColor:[[UIColor whiteColor] CGColor]]; 

[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer]; 
[captureVideoPreviewLayer release]; 
AVCaptureDevice *device = [self frontFacingCameraIfAvailable]; 
NSError *error = nil; 
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; 
if (!input) { 
    // Handle the error appropriately. 
    NSLog(@"ERROR: trying to open camera: %@", error); 
} 

AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]; 
NSError *error2 = nil; 
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error2]; 






AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init]; 

NSString *archives = [documentsDirectoryPath stringByAppendingPathComponent:@"archives"]; 
NSString *editedfilename = [[selectedCountry lastPathComponent] stringByDeletingPathExtension]; 
NSString *datestring = [[editedfilename stringByAppendingString:@" "] stringByAppendingString:currentTime]; 
NSLog(@"%@", datestring); 
NSString *outputpathofmovie = [[archives stringByAppendingPathComponent:datestring] stringByAppendingString:@".mp4"]; 
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputpathofmovie]; 
[session addInput:input]; 
[session addInput:audioInput]; 
[session addOutput:movieFileOutput]; 
[session commitConfiguration]; 
[session startRunning]; 
[movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self]; 
[movieURL release]; 
[outputURL release]; 

回答

0

我明白了。我將類別設置爲PlayAndRecord。我把它改爲環境(也可以錄製播放音頻的時候音頻)和OverrideCategoryMixWithOthers的去除財產,只留下

OSStatus propertySetError = 0; 
UInt32 allowMixing = true; 
propertySetError |= AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck, sizeof(allowMixing), &allowMixing); 
1

音頻會話可能會「迴避」背景音樂。您可能希望將屬性「kAudioSessionProperty_OtherMixableAudioShouldDuck」設置爲false以禁用該屬性。

+0

我該如何去有關更改屬性?我將它添加到代碼中: 'OSStatus propertySetError = 0; UInt32 allowMixing = true; propertySetError | = AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck,sizeof(allowMixing),&allowMixing);' 我可以在仍使用OverrideCategoryMixWithOthers的情況下使用它嗎? – user717452

+0

更改allowMixing = false;我認爲你可以用它與其他財產,因爲它只是告訴它做什麼時,它混合,但我不積極。 – rooster117

+0

沒有工作,AVCaptureSession正在做的記錄搞砸了,並且在完成後仍然有音頻幾乎靜音。 – user717452

相關問題