Note that there are some explanatory texts on larger screens.

plurals
  1. POPlaying video with AVPlayer and recording sound with AVAudioRecorder simultaneously
    primarykey
    data
    text
    <p>I was googling this problem for many days but I didn't find a solution for my simple problem.</p> <p>I'm trying to play a video while recording sound. Recording sound works fine without the video. And the video works fine without the recording.</p> <p>As soon as I put both together, the recording throws values which do not react on sound and the video is not playing anymore. By the way this happens only on my device (iPhone 4 / iOS5). In the simulator everything works fine.</p> <p>This is a reduced version of my code which is in a ViewController.</p> <pre><code>// Audio Recorder NSURL *nullUrl = [NSURL fileURLWithPath:@"/dev/null"]; NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithFloat:44100.0], AVSampleRateKey, [NSNumber numberWithInt:kAudioFormatAppleLossless], AVFormatIDKey, [NSNumber numberWithInt:1], AVNumberOfChannelsKey, [NSNumber numberWithInt:AVAudioQualityMax], AVEncoderAudioQualityKey, nil]; NSError *recorderError; _recorder = [[AVAudioRecorder alloc] initWithURL:nullUrl settings:settings error:&amp;recorderError]; if (_recorder) { [_recorder prepareToRecord]; _recorder.meteringEnabled = YES; [_recorder record]; _levelTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(levelTimerCallback:) userInfo:nil repeats:YES]; } // Video Player NSURL *url = [[NSBundle mainBundle] URLForResource:@"Video" withExtension:@"mp4" subdirectory:nil]; _avPlayer = [[AVPlayer playerWithURL:url] retain]; _avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:_avPlayer] retain]; _avPlayerLayer.frame = self.view.layer.bounds; [self.view.layer addSublayer:_avPlayerLayer]; [_avPlayer play]; </code></pre> <p>The recorder simply outputs a value which is read in the levelTimerCallback method. The video simply plays a short video.</p> <p>I figured out that I have to set a category for the AVAudioSession. If I implement this code after app start, the video is playing but the recorder still outputs the same values without reacting to sound.</p> <pre><code>NSError *error = nil; AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&amp;error]; [audioSession setActive:YES error:&amp;error]; </code></pre> <p>I could imagine that I might have to do something with AudioSessionSetProperty</p> <pre><code>UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord; AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &amp;sessionCategory); UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&amp;audioRouteOverride); </code></pre> <p>But that doesn't change anything. Do you have an idea what I am missing or how to set up the AudioSession properly?</p> <p>Thank you for every hint on that!</p> <p>Cheers, Raphael ;)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload