开发者

AVAssetWriter / AVAudioPlayer Conflict?

开发者 https://www.devze.com 2023-03-22 06:25 出处:网络
Weeks ago, I posted this thread regarding problems I was having with AVAssetWriter: AVAssetWriter Woes

Weeks ago, I posted this thread regarding problems I was having with AVAssetWriter: AVAssetWriter Woes

Further research seems to lead to a conflict using AVAssetWriter while playing audio with AVAudioPlayer or, really, any audio system. I tried with OpenAL as well.

Here's the background:

  • Using AVAssetWriter to write frames to a video from an image or set of images开发者_StackOverflow works fine UNTIL [AVAudioPlayer play] is called.

  • This only happens on the device, not the sim.

  • The error occurs when attempting to create a pixel buffer from CVPixelBufferPoolCreatePixelBuffer.

  • Once the audio starts playing, the AVAssetWriterInputPixelBufferAdaptor.pixelBufferPool which existed before suddely becomes nil.

You can download the representative project here: http://www.mediafire.com/?5k7kqyvtbfdgdgv

Comment out AVAudioPlayer play and it will work on the device.

Any clues are appreciated.


I've found the solution to this issue.

If you want to have AVAudioPlayer and AVAssetWriter behave correctly together, you must have and audio session category that is 'mixable'.

You can use a category that is mixable like AVAudioSessionCategoryAmbient.

However, I needed to use AVAudioSessionCategoryPlayAndRecord.

You can set any category to be mixable by implementing this:

OSStatus propertySetError = 0;

UInt32 allowMixing = true;

propertySetError = AudioSessionSetProperty (
                       kAudioSessionProperty_OverrideCategoryMixWithOthers,  // 1
                       sizeof (allowMixing),                                 // 2
                       &allowMixing                                          // 3
                   );


This above answer is in complete. It doesn't work. Do this instead

// Setup to be able to record global sounds (preexisting app sounds)
    NSError *sessionError = nil;
    if ([[AVAudioSession sharedInstance] respondsToSelector:@selector(setCategory:withOptions:error:)])
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDuckOthers error:&sessionError];
    else
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];

    // Set the audio session to be active
    [[AVAudioSession sharedInstance] setActive:YES error:&sessionError];

//then call your asset writer 
movieWriter = [[AVAssetWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(480.0, 640.0)];
0

精彩评论

暂无评论...
验证码 换一张
取 消