I am trying to play simple audio from http url using AVAudioPlayer however getting weird error . Here is the code
func playSound() {
do {
let url = URL(string: "http://<domain>/stream")
var player = AVAudioPlayer() //tied to initialize it as AVAudioPlayer! too
player = try AVAudioPlayer(contentsOf: url!)
player.prepareToPlay()
player.play()
} catch let error as NSError {
print(error.description)
}}
Error:
AddInstanceForFactory: No factory registered for id <CFUUID 0x6000030f4b00> F8BB1C28-BAE8-11D6-9C31-00039315CD46
Error Domain=NSOSStatusErrorDomain Code=2003334207 "(null)"
You have to make player as global variable:
var player = AVAudioPlayer()
// ...
func playSound() { // ...
Related
As you may see, I'm trying to play audio obtained from a url. Where is the mistake?
The error seems to suggest that the audio from the url is not good...
//let play_url = "https://opus-server.herokuapp.com/asd.wav"
let play_url = "http://www.largesound.com/ashborytour/sound/brobob.mp3"
var audioPlayer: AVAudioPlayer?
#IBAction func play(_ sender: Any) {
let url = URL(string: play_url)!
do {
audioPlayer = try AVAudioPlayer(contentsOf: url)
audioPlayer?.play()
} catch {
print(error)
}
}
I expect AVAudioPlayer to play the audio. Instead, there is an error printed: Error Domain=NSOSStatusErrorDomain Code=2003334207 "(null)"
I am trying to trigger a function when a song finishes playing. Here is my audio player class:
import AVFoundation
public class SKTAudio {
public var backgroundMusicPlayer: AVAudioPlayer?
public func playBackgroundMusic(filename: String) {
let url = Bundle.main.url(forResource: filename, withExtension: nil)
if (url == nil) {
print("Could not find file: \(filename)")
return
}
var error: NSError? = nil
do {
backgroundMusicPlayer = try AVAudioPlayer(contentsOf: url!)
} catch let error1 as NSError {
error = error1
backgroundMusicPlayer = nil
}
if let player = backgroundMusicPlayer {
player.delegate = self
player.prepareToPlay()
player.play()
} else {
print("Could not create audio player: \(error!)")
}
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer,
// Do stuff
}
}
However, the code won't compile, and the line player.delegate = self returns this error:
Cannot assign value of type 'SKTAudio' to type
'AVAudioPlayerDelegate?'
And gives the option to:
Insert ' as! AVAudioPlayerDelegate'
This compiles, but, upon running, crashes and returns the following in console:
Could not cast value of type 'Scene.SKTAudio' (0x102ebf7b0) to
'AVAudioPlayerDelegate'
Where playBackgroundMusic() is called within Scene. I have no clue what to do at this point, I've tried to follow various other posts but nobody seems to face an issue with the player.delegate = self line. Thank you
here is my code of how im playing my mp3 from a live url i just cannot find any updated information for swift to use avplayer not avaudioplayer.. thank you in advance to all comments and helpers
var playerItem:AVPlayerItem?
var player = AVPlayer()
var error : NSError?
var sucess : Bool?
do{
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback,withOptions: .DefaultToSpeaker)
_ = try AVAudioSession.sharedInstance().setActive(true)
sucess = true
} catch let NotSucessError as NSError{error = NotSucessError
sucess = false}
if !sucess! {
NSLog("Fail to set audio session. Error:\(error)")
}
let url = NSURL(string: trackURL)
playerItem = AVPlayerItem(URL: url!)
player = AVPlayer(playerItem: playerItem!)
player.play()
I've been trying to check if the AVAudioPlayer is currently playing, but when I try to do it in anyway it crashes.
I'm using stop button to stop the audio but when there is no audio playing it crashes.
How is it possible to check it in my stopaudio function ?
First I defined :
var audioPlayer = AVAudioPlayer()
I have stop button :
func stopaudio(sender: AnyObject){
audioPlayer.stop()
}
I tried to check like this :
if audioPlayer.playing{
audioPlayer.stop()
}
I am using the following code to play the audio :
var soundURL: NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("9", ofType: "m4a")!)!
var error:NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: &error)
audioPlayer.prepareToPlay()
audioPlayer.play()
The problem was because the AVAudioPlayer hasn't been initialized yet, so what i did to fix it is to initialize it and then stop it.
var soundURL: NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("013", ofType: "m4a")!)!
var error:NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: &error)
audioPlayer.stop()
Declare a function in the ViewController class:
func tryAudio() {
do {
try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: path!))
} catch {
//handle errors
}
}
call tryAudio() to initialize the player before player.stop()
I've got a simple audio player that works great. I'm playing a remote URL MP3. My code:
#IBAction func playAudio(sender: AnyObject) {
let url = self.productAudio
let playerItem = AVPlayerItem( URL:NSURL( string:url ) )
player = AVPlayer(playerItem:playerItem)
player.
player.rate = 1.0;
player.play()
}
But the audio is outputed by the loud speaker. How can I play the audio through the ear speaker?
Swift 5.0
let session = AVAudioSession.sharedInstance()
do{
try session.setCategory(.playAndRecord)
try session.overrideOutputAudioPort(AVAudioSession.PortOverride.none)
try session.setActive(true)
} catch {
print ("\(#file) - \(#function) error: \(error.localizedDescription)")
}
Finally I found the answer...
I had to modify my Audio Session, and override the port. This is the code before the player code:
let session = AVAudioSession.sharedInstance()
var error: NSError?
session.setCategory(AVAudioSessionCategoryPlayAndRecord, error: &error)
session.overrideOutputAudioPort(AVAudioSessionPortOverride.None, error: &error)
session.setActive(true, error: &error)