Avaudiosession Setcategory Swift 4.2 iOS 12 - Play Sound on Silent

AVAudioSession setCategory Swift 4.2 iOS 12 - Play Sound on Silent

Her der Töne's comment shows you the new syntax, but you also need to activate the audio session after setCategory:

do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error)
}

AVAudioSession setCategory availability in Swift 4.2

iOS 10+

If you are targeting iOS 10+, just transition to the new API and use:

try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [])


Older iOS versions

When you try this for an app targeting an older iOS version (for example iOS 9) you will get an setCategory(_:mode:options:)' is only available on iOS 10.0 or newer Error.

This has been reported as an error in Apple's API and fixed in Xcode 10.2. For older Xcode versions (for example Xcode 10.1) there is a workaround I found. When you create an Objective-C helper as described you can still access the old API because it is still exposed for Objective-C.

Workaround 1: .perform() Method

If you want a quick inline fix without the error handling, you can call the Obj.-C API with the .perform() method:

if #available(iOS 10.0, *) {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
} else {
// Set category with options (iOS 9+) setCategory(_:options:)
AVAudioSession.sharedInstance().perform(NSSelectorFromString("setCategory:withOptions:error:"), with: AVAudioSession.Category.playback, with: [])

// Set category without options (<= iOS 9) setCategory(_:)
AVAudioSession.sharedInstance().perform(NSSelectorFromString("setCategory:error:"), with: AVAudioSession.Category.playback)
}

Workaround 2: Helper class method

Here are the steps how to do it right now if you want some more control over errors

  1. Create a new Objective-C file in my case AudioSessionHelper.m. When prompted if a Bridging Header File should be created, click Yes (If you don't already have one in your project)
  2. Create a new Header file AudioSessionHelper.h
  3. Insert Code
AudioSessionHelper.h
#ifndef AudioSessionHelper_h
#define AudioSessionHelper_h
#import <AVFoundation/AVFoundation.h>

@interface AudioSessionHelper: NSObject
+ (BOOL) setAudioSessionWithError:(NSError **) error;
@end

#endif /* AudioSessionHelper_h */
AudioSessionHelper.m
#import "AudioSessionHelper.h"
#import <Foundation/Foundation.h>

@implementation AudioSessionHelper: NSObject

+ (BOOL) setAudioSessionWithError:(NSError **) error {
BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:error];
if (!success && error) {
return false;
} else {
return true;
}
}
@end

  1. Insert your helper class into Bridging Header File
[PROJECT]-Bridging-Header.h
#import "AudioSessionHelper.h"

  1. Use it in your Swift project
if #available(iOS 10.0, *) {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
} else {
try AudioSessionHelper.setAudioSession()
}

This is a not beautiful and adds lots of unnecessary code and files to your project, so use it if you urgently want or must use Swift 4.2 on Xcode 10.1 right now. In all other cases you would be better off using Xcode 10.2.

Play Audio when device in silent mode - ios swift

Put this line before calling play() method of AVPlayer.

In Objective C

[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryPlayback
error: nil];

In Swift

do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch {
// report for an error
}

Swift 5

do {
try AVAudioSession.sharedInstance().setCategory(.playback)
} catch(let error) {
print(error.localizedDescription)
}

AVAudioSession setCategory .allowBluetooth causes crash

You are getting error code -50, which indicates invalid parameters.

This is because the .defaultToSpeaker option can only be used with the playAndRecord category:

You can set this option can only when using the playAndRecord category. It’s used to modify the category’s routing behavior so that audio is always routed to the speaker rather than the receiver if no other accessories, such as headphones, are in use.

So either remove this option or use a playAndRecord category.

AVAudioSession setCategory WithOptions

The options don't keep their state when a category change occurs.

The categoryOptions property of your applications AVAudioSession shared instance is assigned a value of 0 when there are no options set using either of the setCategory methods currently available.

For example this line -

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

is equivalent to -

AVAudioSessionCategoryOptions AVAudioSessionCategoryOptionsNone = 0;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionsNone error:nil];


Related Topics



Leave a reply



Submit