Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAudioSession setCategory Swift 4.2 iOS 12 - Play Sound on Silent

To play sound even on Silent mode I use to use below method. But how it's not working.

// Works on Swift 3  
do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch {
    print(error)
}

How to get it work in 4.2 / iOS 12?

In newer version we need to set mode and options.

try AVAudioSession.sharedInstance().setCategory(
    <#T##category:AVAudioSession.Category##AVAudioSession.Category#>,
    mode: <#T##AVAudioSession.Mode#>, 
    options: <#T##AVAudioSession.CategoryOptions#>)`
like image 897
Nitesh Avatar asked Jun 24 '18 13:06

Nitesh


4 Answers

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)
}
like image 165
Rhythmic Fistman Avatar answered Nov 20 '22 00:11

Rhythmic Fistman


As a workaround, you can call the Objective-C AVAudioSession.setCategory: method with the NSObject.performSelector::

if #available(iOS 10.0, *) {
    try! AVAudioSession.sharedInstance().setCategory(.playback, mode: .moviePlayback)
}
else {
    // Workaround until https://forums.swift.org/t/using-methods-marked-unavailable-in-swift-4-2/14949 isn't fixed
    AVAudioSession.sharedInstance().perform(NSSelectorFromString("setCategory:error:"), with: AVAudioSession.Category.playback)
}

If you want to set the category and options for iOS 9 and earlier then use:

AVAudioSession.sharedInstance().perform(NSSelectorFromString("setCategory:withOptions:error:"), with: AVAudioSession.Category.playback, with:  [AVAudioSession.CategoryOptions.duckOthers])

UPDATE:

Issue is fixed in Xcode 10.2.

like image 39
ricardopereira Avatar answered Nov 19 '22 23:11

ricardopereira


For Swift 4.2 in iOS 12, it is simply:

try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [])
like image 20
Chewie The Chorkie Avatar answered Nov 20 '22 01:11

Chewie The Chorkie


Update for Xcode 10.2:

Apple finally fix this issue in Xcode 10.2. 
So no need to add these workaround code anymore if you use Xcode 10.2 or newer version.

But you also could refer this code for any problem like this.

You can use an objective-c category to help to fix this issue.

Create a AVAudioSession+Swift.h:

@import AVFoundation;

NS_ASSUME_NONNULL_BEGIN

@interface AVAudioSession (Swift)

- (BOOL)swift_setCategory:(AVAudioSessionCategory)category error:(NSError **)outError NS_SWIFT_NAME(setCategory(_:));
- (BOOL)swift_setCategory:(AVAudioSessionCategory)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError **)outError NS_SWIFT_NAME(setCategory(_:options:));

@end

NS_ASSUME_NONNULL_END

With a AVAudioSession+Swift.m:

#import "AVAudioSession+Swift.h"

@implementation AVAudioSession (Swift)

- (BOOL)swift_setCategory:(AVAudioSessionCategory)category error:(NSError **)outError {
    return [self setCategory:category error:outError];
}
- (BOOL)swift_setCategory:(AVAudioSessionCategory)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError **)outError {
    return [self setCategory:category withOptions:options error:outError];
}

@end

Then import the "AVAudioSession+Swift.h" in your <#target_name#>-Bridging-Header.h

#import "AVAudioSession+Swift.h"

Result is you can call the method in swift like before.

do {
    try AVAudioSession.sharedInstance().setCategory(.playback)
    try AVAudioSession.sharedInstance().setCategory(.playback, options: [.mixWithOthers])
    try AVAudioSession.sharedInstance().setActive(true)
} catch {
    print(error)
}
like image 15
Galvin Avatar answered Nov 20 '22 01:11

Galvin