Type 'string' has no member 'playback' - ios

While trying to build a xylophone in xcode 10.1, using swift 4.2 on iOS12, i used a button to play a .wav file and entered the following code but the following error shows up:
"type 'String' has no member 'playback'"
func playSound() {
guard let url = Bundle.main.url(forResource: "soundName", withExtension: "mp3") else { return }
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
try AVAudioSession.sharedInstance().setActive(true)
/* The following line is required for the player to work on iOS 11. Change the file type accordingly*/
player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue)
/* iOS 10 and earlier require the following line:
player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileTypeMPEGLayer3) */
guard let player = player else { return }
player.play()
} catch let error {
print(error.localizedDescription)
}
}

I was looking around StackOverflow and I borrowed this piece of code which works, I don't know if its the conventional way of doing it though.
Swift 4.2
import AVFoundation
var player = AVAudioPlayer()
let url = URL(fileURLWithPath: Bundle.main.path(forResource: "note1", ofType: "wav")!)
do {
player = try AVAudioPlayer(contentsOf: url)
player.play()
} catch {
print("couldn't load file :(")
}

func playSound() {
let soundURL = Bundle.main.url(forResource: selectedSoundFileName, withExtension: "wav")
do{
audioPlayer = try AVAudioPlayer(contentsOf: soundURL!)
audioPlayer.play()
} catch {
}
}
This is another variation that you might be able to try! Let me know if this format works for you. I built a xylophone app myself a long time ago inside of a Udemy course. Pretty much would be easier to put the sound files directly into your project and run them through there, than to pull them from anywhere in my opinion.
this is the most "Swifty" way I could implement this for you.

If you click jump to definition on AVAudioSession Xcode will open the raw source file for the class and from there you can view the structure of each method in the class. This is helpful for viewing the structure of each function and for determining the data parameters of each function. Additionally there are annotations above each variation of the class functions compatible for different iOS deployment targets, indicated by the #available(iOS 11.0, *) annotations. We're focusing on the open func setCategory function in this class.
Helpful information in the raw source file
Allowed categories: AVAudioSessionCategoryPlayback
Allowed modes: AVAudioSessionModeDefault, AVAudioSessionModeMoviePlayback, AVAudioSessionModeSpokenAudio
Allowed options: None. Options are allowed when changing the routing policy back to Default
I edited the category and mode parameters in my function as follows:
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault, options: [.mixWithOthers, .allowAirPlay])
print("Playback OK")
try AVAudioSession.sharedInstance().setActive(true)
print("Session is Active")
} catch {
print(error)
}

Related

How can I play a sound in swift without stopping the playing music?

I'm using AVAudio to play a sound, but when I do so, the music (In the music app) stops.
var audioPlayer: AVAudioPlayer?
func playSound(sound: String, type: String) {
if let path = Bundle.main.path(forResource: sound, ofType: type) {
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
audioPlayer?.play()
} catch {
print("ERROR")
}
}
}
// Some code here
playSound(sound: "resume", type: "m4a")
I want to make the sound act like a notification soud and that the music keeps playing. Any way to do this?
Set your AVAudioSession to .duckOthers or .mixWithOthers:
(Before you play sounds):
do {
try AVAudioSession.sharedInstance()
.setCategory(.playback, options: .duckOthers)
try AVAudioSession.sharedInstance()
.setActive(true)
} catch {
print(error)
}
You will first have to define properties of an AVAudioSession. This lets you chose how to play the sound with the help of setCategory(_:mode:options:). What you need is this:
var audioPlayer: AVAudioPlayer?
func playSound(sound: String, type: String) {
if let path = Bundle.main.path(forResource: sound, ofType: type) {
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers])
try AVAudioSession.sharedInstance().setActive(true)
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
audioPlayer?.play()
} catch {
print("ERROR")
}
}
}
// Some code here
playSound(sound: "resume", type: "m4a")
Feel free to experiment with the setCategory function by passing different configuration options. You can also read more about mixWithOthers, but the key point is:
An option that indicates whether audio from this session mixes with audio from active sessions in other audio apps.

How to fix problem with AVAudioPlayer: "OpenFromDataSource failed"

I'm working on Xcode 10.1 and this is a very basic snippet of an audio playing app.
var audioplayer:AVAudioPlayer?
override func viewDidLoad() {
super.viewDidLoad()
let url = Bundle.main.url(forResource: "ringtone2", withExtension: "mp3")
guard url != nil else{
return
}
do{
audioplayer = try AVAudioPlayer(contentsOf: url!)
audioplayer?.play()
}
catch{
print("error")
}
}
It should play the ringtone tune but the code enters the catch block and the following is printed:
2019-06-20 15:29:11.573735+0530 letsplaymusic[1539:42595] 1410: Problem scanning for packets
2019-06-20 15:29:11.574033+0530 letsplaymusic[1539:42595] 1008: MPEGAudioFile::OpenFromDataSource failed
2019-06-20 15:29:11.574204+0530 letsplaymusic[1539:42595] 101: OpenFromDataSource failed
2019-06-20 15:29:11.574427+0530 letsplaymusic[1539:42595] 76: Open failed
error
You might be placing your audio file at the wrong place. Try placing your file at the same hierarchy level as your ViewController.swift file.
I'm able to play the sound using the same code you posted, however I can't see where you have put the audio file so I'm guessing that must be the issue.
1. Make Sure .Mp3 file copy in Bundle and confirm with Log
2. player.prepareToPlay() - required function to play the resources
func playAudioFile(_ url: URL) {
print("playing \(url)")
do {
self.player = try AVAudioPlayer(contentsOf: url)
player.prepareToPlay()
player.volume = 1.0
player.play()
} catch {
self.player = nil
print(error.localizedDescription)
print("AVAudioPlayer init failed")
}
}
Happy Coding... :)

handleRemoteNowPlayingActivity is not called

How to initiate call of handleRemoteNowPlayingActivity function.
I try to play an audio file with AVAudioPlayer in my iPhone app and I expect that this function will be called on watches.
Maybe I should make some additional call to trigger this function?
I cannot find any additional information about this method except what is said in the apple documentation.
Maybe someone faced this problem and will be able to help me.
Code of initialization audio player.
#IBAction func play(_ sender: Any) {
let url = URL(string: Bundle.main.path(forResource: "music", ofType: "mp3") ?? "")!
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: .mixWithOthers)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(contentsOf: url)
} catch let error {
print(error)
}
player?.play()
}
I have found the answer.
If you want to initiate handleRemoteNowPlayingActivity in your watch app, you should
play some audio/video on your device
setup audio session as shown above
and call beginReceivingRemoteControlEvents().

iOS Xode : AVAudioPlayer is not playing WAV file correctly

I have a WAV file with tones at around 18kHz. The audio is 16-bit PCM mono.
I am using the following function to play the file:
func playSound(name: String) {
let documents = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let path = documents.appending("/").appending(name)
let url = NSURL.fileURL(withPath: path)
do {
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.prepareToPlay()
player.play()
} catch let error {
print(error.localizedDescription)
}
}
When I play it on my iOS 10.2 device I hear a series of tones between 1000Hz to 10000Hz. I've analyzed the rendered audio by capturing it and a frequency plot shows that the original content at 18kHz is there, but there are also tones present between 1000Hz to 10000Hz. When I play the same WAV file with VLC or any other desktop audio player, I don't hear the tones (which is expected since they're located around 18kHz). I suspect that the code above isn't loading the data correctly or that the player isn't properly initialized, so I need a seasoned iOS veteran who can tell me what I'm doing wrong.
Thank you in advance.
Check the following code it might work for you
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("button-09", ofType: "wav"))
println(alertSound)
// Removed deprecated use of AVAudioSessionDelegate protocol
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
var error:NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
audioPlayer.prepareToPlay()
audioPlayer.play()
}
}

How can I allow background music to continue playing while my app still plays its sounds while using Swift

I created an app and I am attempting to allow the user to continue to listen to their music while playing my game, but whenever they hit "play" and the ingame sounds occur it will stop the background music. I am developing on iOS using Swift. Here is a piece of the code that initiates the ingame sounds.
func playSpawnedDot() {
var alertSound: NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("spawnDot", ofType: "mp3")!)!
var error:NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
audioPlayer.prepareToPlay()
if volumeBool {
audioPlayer.play()
}
}
You need to set the AVAudioSession category, with one of the following value: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/index.html (AVAudioSession Class Reference).
The default value is set to AVAudioSessionCategorySoloAmbient. As you can read :
[...] using this category implies that your app’s audio is nonmixable—activating your session will interrupt any other audio sessions which are also nonmixable. To allow mixing, use the AVAudioSessionCategoryAmbient category instead.
You have to change the category, before you play your sound. To do so :
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
You don't need to call those line each time you play the sound. You might want to do it only once.
Swift 4 version:
try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
try? AVAudioSession.sharedInstance().setActive(true)
This is how I do it in Swift 3.0
var songPlayer : AVAudioPlayer?
func SetUpSound() {
if let path = Bundle.main.path(forResource: "TestSound", ofType: "wav") {
let filePath = NSURL(fileURLWithPath:path)
songPlayer = try! AVAudioPlayer.init(contentsOf: filePath as URL)
songPlayer?.numberOfLoops = -1 //logic for infinite loop
songPlayer?.prepareToPlay()
songPlayer?.play()
}
let audioSession = AVAudioSession.sharedInstance()
try!audioSession.setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.duckOthers) //Causes audio from other sessions to be ducked (reduced in volume) while audio from this session plays
}
You can see more of AVAudioSessionCategoryOptions here: https://developer.apple.com/reference/avfoundation/avaudiosessioncategoryoptions
Here's what I am using for Swift 2.0:
let sess = AVAudioSession.sharedInstance()
if sess.otherAudioPlaying {
_ = try? sess.setCategory(AVAudioSessionCategoryAmbient, withOptions: .DuckOthers)
_ = try? sess.setActive(true, withOptions: [])
}
Please note that you can replace .DuckOthers with [] if you don't want to lower background music and instead play on top to it.
Since they can't seem to make up their minds from version to version. Here it is in Swift 5.0
do{
try AVAudioSession.sharedInstance().setCategory(.ambient)
try AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)
} catch {
NSLog(error.localizedDescription)
}
lchamp's solution worked perfectly for me, adapted for Objective-C:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
**
Updated for Swift 3.0
**
The name of the sound I am playing is shatter.wav
func shatterSound() {
if let soundURL = Bundle.main.url(forResource: "shatter", withExtension: "wav") {
var mySound: SystemSoundID = 0
AudioServicesCreateSystemSoundID(soundURL as CFURL, &mySound)
AudioServicesPlaySystemSound(mySound);
}
}
Then where ever you want to play the sound call
shatterSound()
If you want to play an alert sound:
public func playSound(withFileName fileName: String) {
if let soundUrl = Bundle.main.url(forResource: fileName, withExtension: "wav") {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, with:[.duckOthers])
try AVAudioSession.sharedInstance().setActive(true)
var soundId: SystemSoundID = 0
AudioServicesCreateSystemSoundID(soundUrl as CFURL, &soundId)
AudioServicesAddSystemSoundCompletion(soundId, nil, nil, { (soundId, clientData) -> Void in
AudioServicesDisposeSystemSoundID(soundId)
do {
// This is to unduck others, make other playing sounds go back up in volume
try AVAudioSession.sharedInstance().setActive(false)
} catch {
DDLogWarn("Failed to set AVAudioSession to inactive. error=\(error)")
}
}, nil)
AudioServicesPlaySystemSound(soundId)
} catch {
DDLogWarn("Failed to create audio player. soundUrl=\(soundUrl) error=\(error)")
}
} else {
DDLogWarn("Sound file not found in app bundle. fileName=\(fileName)")
}
}
And if you want to play music:
import AVFoundation
var audioPlayer:AVAudioPlayer?
public func playSound(withFileName fileName: String) {
if let soundUrl = Bundle.main.url(forResource: fileName, withExtension: "wav") {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, with:[.duckOthers])
try AVAudioSession.sharedInstance().setActive(true)
let player = try AVAudioPlayer(contentsOf: soundUrl)
player.delegate = self
player.prepareToPlay()
DDLogInfo("Playing sound. soundUrl=\(soundUrl)")
player.play()
// ensure the player does not get deleted while playing the sound
self.audioPlayer = player
} catch {
DDLogWarn("Failed to create audio player. soundUrl=\(soundUrl) error=\(error)")
}
} else {
DDLogWarn("Sound file not found in app bundle. fileName=\(fileName)")
}
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
self.audioPlayer?.stop()
do {
// This is to unduck others, make other playing sounds go back up in volume
try AVAudioSession.sharedInstance().setActive(false)
} catch {
DDLogWarn("Failed to set AVAudioSession inactive. error=\(error)")
}
}
For Swift (Objective-C like this too)
you can use this link for best answer and if you don't have any time for watching 10 minutes the best action is that you just copy below code in your AppDelegate in didFinishLaunchingWithOptions and then select your project's target then go to Capabilities and at last in Background modes check on Audio, AirPlay and Picture in Picture
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(AVAudioSessionCategoryPlayback)
}
catch {
}
}
I didn't think that just setting the AVAudioSession to AVAudioSessionCategoryOptions.duckOthers would work, but it did. Here is my full code to help the rookies like myself.
Swift 4 - Full Example:
var audioPlayer = AVAudioPlayer()
func playSound(sound: String){
let path = Bundle.main.path(forResource: sound, ofType: nil)!
let url = URL(fileURLWithPath: path)
let audioSession = AVAudioSession.sharedInstance()
try!audioSession.setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.duckOthers)
do {
audioPlayer = try AVAudioPlayer(contentsOf: url)
audioPlayer.play()
} catch {
print("couldn't load the file")
}
}
I still need to figure out setActive (I was looking at this blog post) but the audio stops ducking when I leave the app, so it works for my app.
Swift 5 Version based on lchamp`s answer.
try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.ambient)
try? AVAudioSession.sharedInstance().setActive(true)
This works perfectly for me on iOS 14.4 and Swift 5. It's a bit different answer from others by using the .duckOthers option (and you can also mix directly with sound if you'd like with .mixWithOthers), but it works perfectly while music plays. It will lower the volume of the music, play the "beep" sound, and then raise the music volume back up to normal. It also captures error data using Google Firebase Crashlytics if there is an issue, and tries to raise the volume to normal even on an error.
This code will also work perfectly on the first, and all other, plays of your sound without it stopping the music ever.
func playSound() {
do {
if let path = Bundle.main.path(forResource: "beep", ofType: "mp3") {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: .duckOthers)
try AVAudioSession.sharedInstance().setActive(true)
let filePath = NSURL(fileURLWithPath:path)
songPlayer = try AVAudioPlayer.init(contentsOf: filePath as URL)
songPlayer?.numberOfLoops = 0
songPlayer?.prepareToPlay()
songPlayer?.play()
try AVAudioSession.sharedInstance().setActive(false)
}
} catch (let error) {
try? AVAudioSession.sharedInstance().setActive(false)
Crashlytics.crashlytics().setCustomValue(error.localizedDescription, forKey: "audio_playback_error")
}
}
An important thing the others answers do not have is that you need to call false with the .notifyOthers flag when deactivating:
try AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation)
The reason for this is that other apps playing music in the background will know when to turn their audio back on when you deactivate yours. Otherwise your background music won't turn back on if you turned off your session.

Resources