Issues with DefaultToSpeaker (and AVAudioRecorder and AVAudioSequencer)

478 views
Skip to first unread message

Adam Hoyle

unread,
Feb 19, 2016, 1:23:02 PM2/19/16
to AudioKit Users Group
Hi All,

I’m having a bit of trouble with something, and just wanted to see if anyone has had a similar issue and already worked around it (or has alternative suggestions).

I’ve got a demo working well with 3 modes:
- a click track with a tempo adjustment
- recording via the mic on top of the click track
- playing back the recorded audio.

On the iPhone when the headphones are unplugged all of the audio is routed through the earpiece speaker (and so is very quiet), but I’d like it to come out of the main speaker so they can hear it (it doesn’t matter if the click is recorded)

The structure is:
- The click track is a MIDI file played via AKSequencer (which is using AVAudioSequencer behind the scenes).
- I have an AKMixer which has the AKSequencer and an AKAudioPlayer (for playing back the recorded audio)
- Then there is a class based on Aure’s example that wraps AVAudioRecorder, but isn’t plugged into the output.

I have tweaked the AudioKit.start method to use:

try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions:AVAudioSessionCategoryOptions.DefaultToSpeaker)


If I do this with headphones plugged in all is fine.
If I do this with the headphones unplugged then the AKSequencer / AVAudioSequencer doesn’t start and from then on it all goes a bit wrong.

Fwiw I’m not particularly wedded to using AKSequencer / AVAudioSequencer, but it seemed like an interesting thing to experiment with - so any tweaks or alternative suggestions on structure are gratefully received!

I’m just about to start adding and removing and debugging to see if I can work out how to make it work - but wanted to ask here, to see if I’m doing anything obviously wrong or anyone has any pointers or experience. I’ll report back if/when I manage to work around this issue so it can benefit someone in the future.

Thanks in advance,

Adam


Adam Hoyle

unread,
Feb 20, 2016, 12:53:08 PM2/20/16
to AudioKit Users Group
Hi All,

Just to keep everyone updated...

I think I have tracked down the root cause of my issue - when I add the AVAudioSessionCategoryOptions.DefaultToSpeaker and have headphones NOT plugged in, then the audio engine starts and then almost immediately stops again, which means I get errors in my graph because the engine isn’t running! D’oh.

I’ve got a short-term hack in place to restart the engine if it’s found to be stopped (which I work out a more elegant way of dealing with this), however… 

When I play my AKSequencer (AVAudioSequencer) after the AVAudioEngine has stopped and restarted I can hear a tone in the background (I have my AVAudioSequencer using an AKSampler with a metronome sound, so when it’s stopped and restarted I can hear the metronome sound and a tone). Anyone have any idea why that’s happening, or more to the point how I can stop it?

Aure - fwiw I’m thinking of adding the DefaultToSpeaker option into AudioKit, as I doubt I’m the only one who will want this functionality, so if you have any steer or pointers or notes or warnings then do please let me know!

Best,

Adam

Aurelius Prochazka Ph.D.

unread,
Feb 20, 2016, 1:51:48 PM2/20/16
to AudioKit Users Group
Seems like you're the first one running into these issues, so thank you, we'd love to see a pull request from you that addresses these problems.

Thanks!
Aure

John Groenhof

unread,
Apr 19, 2016, 12:25:52 AM4/19/16
to AudioKit Users Group
Hey Adam, have you had any luck with the tone issue? I ran into this myself and my fix right now is to reload the wav files into the sampler when AudioKit restarts. The files I'm using are small so this doesn't cause any lag for me. I noticed that this issue doesn't happen with the SequencerTracks sample app, which uses EXS24 files. I'm having this issue with wav files. 

John
Reply all
Reply to author
Forward
0 new messages