Playing sounds in Xcode while playing music from apps - swift

I am currently using the following code to play music within my Xcode project. However, when I play music, other music apps (eg: spotify) stop playing. I understand there is AVAudioSessionCategoryAmbient, but i am not to sure how to set this function within my code.
How do i play music within my app without stopping music from other apps that are currently playing?
Any help would be much appreciated :D
Thanks!
import UIKit
import AVFoundation
let appDelegate = UIApplication.shared.delegate as! AppDelegate
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
/* MUSIC */
var musicIsPlaying: Bool = false
var bgMusicLoop: AVAudioPlayer!
func playBackgroundMusic(fileName: String, withExtenstion fileExtension: String) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
if appDelegate.musicIsPlaying == false {
appDelegate.musicIsPlaying = true
let filePath: String = Bundle.main.path(forResource: fileName, ofType: fileExtension)!
if (appDelegate.bgMusicLoop != nil) {
appDelegate.bgMusicLoop.stop()
}
appDelegate.bgMusicLoop = nil
do {
appDelegate.bgMusicLoop = try AVAudioPlayer(contentsOf: NSURL.fileURL(withPath: filePath))
} catch {
print(error)
}
//A negative means it loops forever
appDelegate.bgMusicLoop.numberOfLoops = -1
appDelegate.bgMusicLoop.prepareToPlay()
appDelegate.bgMusicLoop.play()
}
}

Add try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient) to your code.
AVAudioSessionCategoryAmbient "plays sounds that add polish or interest but are not essential to the app’s use."

Related

Sound not playing in simulator, playing during debug in Xcode

I'm trying to face with the book "Intro to app development" by apple. I'm stuck on exercise AnimalSounds.
There is a SimpleSound class provided by the book, and our job is to use a sound object to reproduce a sound.
The simulator does not play any sound.
The audio files are in the build bundle and are found by the class.
I checked the simulator with other apps (e.g. youtube on safari) and sound works.
SimpleSound class (provided by example, not written by me)
import Foundation
import AudioToolbox
class SimpleSound {
private var soundID: SystemSoundID = 0
public init(named name: String) {
if let soundURL = soundURL(forName: name) {
let status = AudioServicesCreateSystemSoundID(soundURL as CFURL, &soundID)
if status != noErr {
print("Unable to create sound at URL: '\(name)'")
soundID = 0
}
}
}
public func play() {
if soundID != 0 {
print("Playing sound \(soundID)")
AudioServicesPlaySystemSound(soundID)
//AudioServicesPlaySystemSound(1001)
}
}
private func soundURL(forName name: String) -> URL? {
let fileExtensions = ["m4a", "wav", "mp3", "aac", "adts", "aif", "aiff", "aifc", "caf", "mp4"]
for fileExtention in fileExtensions {
if let soundURL = Bundle.main.url(forResource: name, withExtension: fileExtention) {
return soundURL
}
}
print("Unable to find sound file with name '\(name)'")
return nil
}
deinit {
if soundID != 0 {
AudioServicesDisposeSystemSoundID(soundID)
}
}
}
Code used in my viewController (written by me) in a button tapped action:
let meowSound = SimpleSound(named: "meow")
meowSound.play()
When debugging from Xcode line by line, when the function play() is invoked, the sound is reproduced. When app is running in simulator (with no debugging) or in actual iPhone sound does not play.
If standard system sound 1001 is uncommented, sound is played for both simulator and iPhone.
Any ideas?

Audio not playing in SwiftUI

I'm having an issue getting an audio file to play when the function is called.
I am using an AVAudioPlayer to try and play the file following the instructions form here:
https://www.hackingwithswift.com/example-code/media/how-to-play-sounds-using-avaudioplayer
After the button is pressed in the view, it calls a func to play the sound, but from what I can tell, nothing is played. There are no errors thrown, and the file is found. The app also uses a speech synthesizer when a button is pushed, and that plays fine.
I looked around stack overflow and followed the instructions from here:
Where to place code for audio playback in a SwiftUI app
But still the audio is not played when the button is pushed.
Here is the func:
func playSound() {
var sound = AVAudioPlayer()
if let path = Bundle.main.path(forResource: "TryAgain", ofType: "wav") {
do {
sound = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
print("Playing sound")
sound.play()
} catch {
print( "Could not find file")
}
}
Here is the class:
class Player: BindableObject {
let willChange = PassthroughSubject<Player, Never>()
var isPlaying: Bool = false {
willSet {
willChange.send(self)
}
}
func playSound() {
var sound = AVAudioPlayer()
if let path = Bundle.main.path(forResource: "TryAgainWav", ofType: "wav") {
do {
sound = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
print("Playing sound")
sound.play()
} catch {
print( "Could not find file")
}
}
}
}
Update: Not sure if this helps, but I built this in with IB instead of SwiftUI and noticed the same message is printed in the console when I click the button to play the audio file:
2019-07-22 11:29:41.075568-0700 PlaySoundPrac[13952:46549155] [plugin] AddInstanceForFactory: No factory registered for id F8BB1C28-BAE8-11D6-9C31-00039315CD46
Any help would be greatly appreciated
I'm pretty new myself, but I'll do my best. What happens if you move the declaration of AVAudioPlayer outside the function? For example:
import Combine
class Player: ObservableObject {
var sound: AVAudioPlayer!
let willChange = PassthroughSubject<Player, Never>()
var isPlaying: Bool = false {
willSet {
willChange.send(self)
}
}
func playSound() {
if let path = Bundle.main.path(forResource: "TryAgainWav", ofType: "wav") {
do {
sound = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
print("Playing sound")
sound.play()
} catch {
print( "Could not find file")
}
}
}
}
Try this out and let me know if it works or not! Thanks.
I had a problem that audio was not played on iOS 13 Simulator in Xcode 11.5 while it was okay in Xcode SwiftUI preview and physical device.

How to play a video in a macOS application with swift?

I'm trying to get a video I have in my project to play in my macOS application using swift. Here is my code:
import Cocoa
import AVKit
import AVFoundation
class ViewController: NSViewController {
var player : AVPlayer!
var avPlayerLayer : AVPlayerLayer!
#IBOutlet weak var videoPlayer: AVPlayerView!
override func viewDidLoad() {
super.viewDidLoad()
guard let path = Bundle.main.path(forResource: "Background", ofType:"mp4") else {
debugPrint("Not found")
return
}
let playerURL = AVPlayer(url: URL(fileURLWithPath: path))
let playerView = AVPlayerView()
playerView.player = playerURL
playerView.player?.play()
}
}
I also have an AVPlayerView in my storyboard that I have connected to my viewcontroller.
It builds and runs fine but when I run it nothing shows up. I just get a black screen.
Any help is appreciated. Thanks!
The problem is this line:
let playerView = AVPlayerView()
Instead of configuring the player view that is in your interface, you make a whole new separate player view — and then you throw it away.

Apple Music & AVAudioEngine in Swift

How can we access songs in the Apple Music library with AVAudioPlayerNode/AVAudioEngine for playback and processing?
I have asked this question in Apple forum.
"Apple Music" may refer to:
the music streaming service
its iOS client (known as "Music")
its macOS client, which doubles as a local media management and playback app (formerly known as iTunes, known as "Music" as of macOS Catalina).
Due to DRM restrictions it is not possible to play tracks from the Apple Music catalogue downloaded onto your device from the Apple Music macOS or iOS clients. However you can play audio files you own and which you've synced onto your device using the macOS Music app or the Finder app, as follows:
Add the NSAppleMusicUsageDescription key to your Info.plist file, and its corresponding value
Setup your AVAudioSession and AVAudioEngine
Find the URL of the media item you want to play (you can use MPMediaPickerController like in the example below or you can make your own MPMediaQuery)
Create an AVAudioFile from that URL
Create an AVAudioPlayerNode set to play that AVAudioFile
Connect the player node to the engine's output node
import UIKit
import AVFoundation
import MediaPlayer
class ViewController: UIViewController {
let engine = AVAudioEngine()
override func viewDidLoad() {
super.viewDidLoad()
let mediaPicker = MPMediaPickerController(mediaTypes: .music)
mediaPicker.allowsPickingMultipleItems = false
mediaPicker.showsItemsWithProtectedAssets = false // These items usually cannot be played back
mediaPicker.showsCloudItems = false // MPMediaItems stored in the cloud don't have an assetURL
mediaPicker.delegate = self
mediaPicker.prompt = "Pick a track"
present(mediaPicker, animated: true, completion: nil)
}
func startEngine(playFileAt: URL) {
do {
try AVAudioSession.sharedInstance().setCategory(.playback)
let avAudioFile = try AVAudioFile(forReading: playFileAt)
let player = AVAudioPlayerNode()
engine.attach(player)
engine.connect(player, to: engine.mainMixerNode, format: avAudioFile.processingFormat)
try engine.start()
player.scheduleFile(avAudioFile, at: nil, completionHandler: nil)
player.play()
} catch {
assertionFailure(String(describing: error))
}
}
}
extension ViewController: MPMediaPickerControllerDelegate {
func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
guard let item = mediaItemCollection.items.first else {
print("no item")
return
}
print("picking \(item.title!)")
guard let url = item.assetURL else {
return print("no url")
}
dismiss(animated: true) { [weak self] in
self?.startEngine(playFileAt: url)
}
}
func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController) {
dismiss(animated: true, completion: nil)
}
}

How to play the same sound overlapping with AVAudioPlayer?

This code plays the sound when the button is tapped but cancels the previous if it is pressed again. I do not want this to happen I want the same sound to overlap when repeatedly pressed. I believe it might be due to using the same AVAudioPlayer as I have looked on the internet but I am new to swift and want to know how to create a new AVAudioPlayer everytime the method runs so the sounds overlap.
func playSound(sound:String){
// Set the sound file name & extension
let soundPath = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource(sound, ofType: "mp3")!)
do {
//Preperation
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch _{
}
do {
try AVAudioSession.sharedInstance().setActive(true)
}
catch _ {
}
//Play the sound
var error:NSError?
do{
audioPlayer = try AVAudioPlayer(contentsOfURL: soundPath)
}catch let error1 as NSError {
error = error1
}
audioPlayer.prepareToPlay()
audioPlayer.play()
}
To play two sounds simultaneously with AVAudioPlayer you just have to use a different player for each sound.
In my example I've declared two players, playerBoom and playerCrash, in the Viewcontroller, and I'm populating them with a sound to play via a function, then trigger the play at once:
import AVFoundation
class ViewController: UIViewController {
var playerBoom:AVAudioPlayer?
var playerCrash:AVAudioPlayer?
override func viewDidLoad() {
super.viewDidLoad()
playerBoom = preparePlayerForSound(named: "sound1")
playerCrash = preparePlayerForSound(named: "sound2")
playerBoom?.prepareToPlay()
playerCrash?.prepareToPlay()
playerBoom?.play()
playerCrash?.play()
}
func preparePlayerForSound(named sound: String) -> AVAudioPlayer? {
do {
if let soundPath = NSBundle.mainBundle().pathForResource(sound, ofType: "mp3") {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
return try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: soundPath))
} else {
print("The file '\(sound).mp3' is not available")
}
} catch let error as NSError {
print(error)
}
return nil
}
}
It works very well but IMO is not suitable if you have many sounds to play. It's a perfectly valid solution for just a few ones, though.
This example is with two different sounds but of course the idea is exactly the same for two identic sounds.
I could not find a solution using just AVAudioPlayer.
Instead, I have found a solution to this problem with a library that is built on top of AVAudioPlayer.
The library allows same sounds to be played overlapped with each other.
https://github.com/adamcichy/SwiftySound