I want Swift to implement a code that makes sound even in silent mode.

Asked 2 years ago, Updated 2 years ago, 38 views

I am currently creating an iOS app, but I am also worried about the process of making sounds even in silent mode.

Objective-C should be the following code.

AudioSessionInitialize (NULL, NULL, NULL, NULL);
UInt32 category = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
                        size of (UInt32),
                        & category);
AudioSessionSetActive=true

I don't know how to convert the above code to Swift.
I think it is the image below, but it seems that the setProperty method does not exist.

varaudioSession=AVAudioSession.sharedInstance()
audioSession.setProperty(...)
audioSession.setActive(true, error:nil)

Could someone please give me some advice?
Thank you for your cooperation.

ios swift

2022-09-30 18:16

1 Answers

I only checked the playground to the point where there are no errors, but

varaudioSession=AVAudioSession.sharedInstance()
audioSession.setCategory (AVAudioSessionCategoryPlayback, error:nil)
audioSession.setActive(true, error:nil)

If so, I think it will be the same.

AVAudioSessionCategoryPlayback and kAudioSessionCategory_MediaPlayback may look the same, but you may also want to specify AVAudioSessionCategoryOptionMixWithOthers to allow mixing in setCategoryWithOptions if necessary.


2022-09-30 18:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.