iOS switch between playing sounds in background and not playing - ios

I am working on music app and it has feature of switching between playing/not playing music when app minimized.
How can I do it?
I use that code to enable playing in backgound, but how can I disable it?
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
} catch let error as NSError {
print(error.localizedDescription)
}

Related

AVFoundation adding audio input mutes audio playback

I have an app that records audio and video using AVFoundation. I want any audio playback from other apps (or the system) to continue while recording audio in my app, but when adding the input to the session an already playing audio from another app gets magically muted.
The corresponding code looks like this:
// set up audio device
if(m_audioenabled) {
print("Audio enabled")
if self.m_audioDevice != nil {
do {
let audioinput = try AVCaptureDeviceInput(device: self.m_audioDevice!)
if self.m_session.canAddInput(audioinput) {
self.m_session.addInput(audioinput)
}
} catch _ {
print("failed adding audio capture device as input to capture session")
}
}
m_audioDataOutput = AVCaptureAudioDataOutput()
m_audioDataOutput?.setSampleBufferDelegate(self, queue: self.m_captureSessionQueueAudio)
if self.m_session.canAddOutput(m_audioDataOutput!){
self.m_session.addOutput(m_audioDataOutput!)
}
}
If I comment out the call to canAddInput(...) the audio keeps playing, when I call it, audio playback gets muted.
How can I disable that behavior ?
Please note the migrating to another Audio-API is not an option.
Sounds like your AVAudioSession is non-mixable (the default, I think). Try activating a mixable audio session before you set up the AVCaptureSession:
let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.mixWithOthers])
try! session.setActive(true)

Audio recording stops when sound in other app is playing

We have functionality for background recording in app.
It is working fine when app in background mode but it is stop when we open other app and play sound in it.How can we continue recording even without stop recording when other app play sound.
Note: When Recording start at that time out app in background mode.
Call this method in appDelegate,
func audioSessionSettings() {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryRecord, with: AVAudioSessionCategoryOptions.mixWithOthers)
try AVAudioSession.sharedInstance().setActive(true)
} catch let error {
print(error.localizedDescription)
}
}

Mute sound from wkwebview when device ringer is switched off

I am using wkWebView to display web content and videos
When the user switch the device ringer off i want the webView sound to switch off to
I tried this:
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategorySoloAmbient)
try audioSession.setActive(true)
} catch _ {
print("error setting catagory!")
}
But it dont work - the ringer is off but music is still on

AVAudioEngine stops device's background audio

I am using an AVAudioEngine object, that has many AVAudioPlayerNodes attached to it. The audio all works fine, except it stops any audio that the iPhone is playing in the background (i. e. from iTunes or another music app).
When my app is opened, it stops any other background audio. Is there a way to allow background audio to continue to play? Even when my app is using AVAudioPlayerNodes to play audio itself?
Music App has it's own audioSession, that makes audio engine stops, i had that problem too, please restart after music app.
func stepB_startEngine(){
if engine.running == false {
do { try engine.start() }
catch let error {
print(error)
}
}
}
setup audioSettion also:
func setUpAudioSession(){
do {
try AVAudioSession.sharedInstance().setCategory(.ambient, options: .mixWithOthers)
try AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)
} catch let error {
print(error)
}
}

iPhone, record sounds from speaker and microphone simultaneously

I'm trying to record sounds from a speaker and microphone simultaneously in iPhone. I used AVAudioRecorder and AVAudioSession to do this, but it doesn't work: the only sound recorded is from the microphone.
I've already read articles about this on stackoverflow, like Record AVAudioPlayer output using AVAudioRecorder and similar questions and answers.
Are there any good references or classes to implement this? Thanks in advance!
if we want to use both of them at a time we need to set AVAudioSession's Category to AVAudioSessionCategoryAmbient.
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
} catch let error as NSError {
print(error)
}
do {
try AVAudioSession.sharedInstance().setActive(true)
} catch let error as NSError {
print(error)
}

Resources