Hey guys so for some reason when you mute the phone the audio is still coming from the app. Any ideas on how to fix?
private func myFunc() {
// Set up the audio session
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
fatalError("Unable to set the audio session because: \(error)")
}
guard
let audioURL = Bundle.main.url(forResource: "lololol", withExtension: "mp3"),
let player = try? AVAudioPlayer(contentsOf: audioURL)
else { fatalError("Unable to create player") }
if let aPlayer = audioPlayer {
if audioPlayer?.isPlaying == false {
audioPlayer = player
player.play()
}
} else {
audioPlayer = player
player.play()
}
}
I solve it by changing AVAudioSessionCategoryPlayback to AVAudioSessionCategoryAmbient.
Related
I'm using Xcode 7.3.1 with iOS 9 and having issue with playing sound by AVAudioPlayer. I've tried all possible solution out there and nothing works Here is my code
let mp3Url = NSBundle.mainBundle().URLForResource("BCA", withExtension:"mp3")!
var player = AVAudioPlayer()
do {
player = try AVAudioPlayer(contentsOfURL: mp3Url)
player.prepareToPlay()
player.play()
} catch let error as NSError {
print(error.description)
}
The sound won't play until I put a breakpoint at player.play() and using po player. Then the sound will be played.
Any idea will be helpful for me.
Try this :
var player: AVAudioPlayer?
func playSound(){
guard let url = Bundle.main.url(forResource: "alert_song", withExtension: "mp3") else {
print("MP3 resource not found.")
return
}
print("Music to play : \(String(describing: url))")
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.play()
} catch let error {
print(error.localizedDescription)
}
}
Try this :
var player = AVAudioPlayer() //global variables will otherwise be deadlock
let mp3Url = NSBundle.mainBundle().URLForResource("BCA", withExtension:"mp3")!
do {
player = try AVAudioPlayer(contentsOfURL: mp3Url)
player.play()
} catch let error as NSError {
print(error.description)
}
I used the function slowPlay to create an action that will play background music. I tried to do the reverse in stop to stop the music. But the music does not stop.
I just want to have a button that plays the music and stops the music. I'm using the code:
#IBAction func play(_ sender: Any) {
slowPlay
}
#IBAction func stop(_ sender: Any) {
slowStop
}
func slowPlay() {
do {
let ap = Bundle.main.path(forResource: "slowslow", ofType: "mp3")
try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: ap!) as URL)
} catch {
////
}
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(AVAudioSessionCategoryPlayback)
} catch {
}
player?.play()
}
func slowStop() {
do {
let ap = Bundle.main.path(forResource: "slowslow", ofType: "mp3")
try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: ap!) as URL)
} catch {
////
}
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(AVAudioSessionCategoryPlayback)
} catch {
}
player?.stop()
}
If you look at your code example you are creating a new instance of the player for both play and stop.
You need to create a class variable for your player, something like:
var player: AVAudioPlayer?
This will allow you to stop like this:
func slowStop() {
player?.stop()
}
Create a single instance for player like blow
var player: AVAudioPlayer?
func slowPlay() {
do {
let ap = Bundle.main.path(forResource: "slowslow", ofType: "mp3")
self.player = try AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: ap!) as URL)
} catch {
////
}
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(AVAudioSessionCategoryPlayback)
} catch {
//catch error here
}
self.player?.play()
}
func slowStop() {
self.player?.stop()
}
You can add validation to check whether player is valid or not before performing any action.
I am trying to play a sound but my current code bases the volume off of the ringtone volume. How to I change this to media volume?
var player: AVAudioPlayer?
...
func playSound(name: String) {
let url = Bundle.main.url(forResource: name, withExtension: "mp3")!
do {
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.prepareToPlay()
player.play()
} catch let error {
print(error.localizedDescription)
}
}
...
playSound(name: "baby")
Only set that before you put some url into the player.
try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
To play sound using media volume you should do like this:
func play(){
let path = Bundle.main.path(forResource: "Plop", ofType: "mp3")!
let url = URL(fileURLWithPath: path)
do {
let sound = try AVAudioPlayer(contentsOf: url)
bombSoundEffect = sound
sound.play()
} catch {
// couldn't load file :(
}
}
Currently this is the code I'm using to play my sound during the button press.
var player: AVAudioPlayer?
let url = Bundle.main.url(forResource: "Sound/yatch", withExtension: "mp3")!
do {
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.prepareToPlay()
player.play()
} catch let error {
print(error.localizedDescription)
}
Im trying to get this audio to run over whats currently playing (etc Spotify, pandora, Music). How would I go about doing this?
Set the audio session of your application to play over the currently playing audio:
try? AVAudioSession.sharedInstance().setActive(true)
try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
According to the AVAudioSession headers, AVAudioSessionCategoryAmbient will play audio with music, etc.
/* Use this category for background sounds such as rain, car engine noise, etc.
Mixes with other music. */
public let AVAudioSessionCategoryAmbient: String
Swift 5.3 Update
guard let url = Bundle.main.url(forResource: "INSERT_FILENAME", withExtension: "wav") else { return }
do {
try AVAudioSession.sharedInstance().setCategory(.ambient, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
audioPlayer = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.wav.rawValue)
audioPlayer.volume = 1
audioPlayer.play()
} catch let error {
print(error.localizedDescription)
}
i try to make iOS app to play some funny sound put when i click play no sound are out from speaker here is the code :
#IBAction func slow(sender: UIButton) {
if let filePath = NSBundle.mainBundle().URLForResource("io", withExtension: "mp3") {
let _a = try! AVAudioPlayer(contentsOfURL: filePath)
// Play
_a.play()
}else
{
print("error")
}
}
there is no error but i can't hear any sound at all
for how that face this problem
#IBAction func slow(sender: UIButton) {
if let path = NSBundle.mainBundle().pathForResource("io", ofType:"mp3") {
let fileURL = NSURL(fileURLWithPath: path)
player = try! AVAudioPlayer(contentsOfURL: fileURL)
player.prepareToPlay()
player.play()
}
else
{
print("error")
}
}
}