OSStatus error 2003334207 when using AVAudioPlayer - ios

I am trying to play an MP3 file (works when played via VLC/iTunes) when a button is pressed. Here is my code:
var audioPlayer: AVAudioPlayer!
#IBAction func playEpisode(sender: AnyObject) {
println("now playing")
let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
let data: CDEpisode = fetchedResultsController.objectAtIndexPath(indexPath!) as! CDEpisode
var err: NSError?
let url = NSURL(string: data.localPath)
println("The url is \(url)")
audioPlayer = AVAudioPlayer(contentsOfURL: url, error: &err)
if audioPlayer == nil {
if let e = err {
println(e.localizedDescription)
}
}
audioPlayer.delegate = self
audioPlayer.prepareToPlay()
audioPlayer.play()
}
Here is the log:
now playing
The url is Optional(file:///var/mobile/Containers/Data/Application/4747A71E-A63F-4EFC-B2DF-8B361361080B/Documents/serial-s01-e12.mp3)
The operation couldn’t be completed. (OSStatus error 2003334207.)
fatal error: unexpectedly found nil while unwrapping an Optional value
The EXC_BREAKPOINT happens on the audioPlayer.delegate = self.
Other threads on StackoOverflow do not help. Any ideas?
Thanks
Edit: I have tried passing a localURL to contentsOfURL (instead of a CDEpisode object) and it still fails.

This is probably caused by trying to load a file that doesn't exist. If that helps someone adding the call to url.checkResourceIsReachable() will log more descriptive message.
Example code:
do {
let url = URL(fileURLWithPath: dbObject.path)
let isReachable = try url.checkResourceIsReachable()
// ... you can set breaking points after that line, and if you stopped at them it means file exist.
} catch let e {
print("couldnt load file \(e.localizedDescription)")
}

It looks like your trying to unwrap a variable that has a nil value. You should safely unwrap your variables to prevent this.
if let data: CDEpisode = fetchedResultsController.objectAtIndexPath(indexPath!) as! CDEpisode
{
var err: NSError?
let url = NSURL(string: data.localPath)
println("The url is \(url)")
//rest of code
}
You will still need to figure out why it is returning nil but this is a safer way to unwrap variables and prevent crashing as there would need to be more context to resolve that issue.
Some questions to look into:
Are you sure the fetchedResultsController is returning an object at
all?
Are you sure it is of CDEpisode?

try this one
var player: AVAudioPlayer = AVAudioPlayer()
#IBAction func playX(_ sender: Any) {
let urlstring = "https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3"
let url = URL(string: urlstring)
let data = try! Data(contentsOf: url!)
player = try! AVAudioPlayer(data: data)
player.prepareToPlay()
player.volume = 1.0
player.play()
}

You're checking if audioPlayer is nil but then you go on to use it as if it wasn't anyway. You probably want something like:
if audioPlayer == nil {
if let e = err {
println(e.localizedDescription)
}
} else {
audioPlayer.delegate = self
audioPlayer.prepareToPlay()
audioPlayer.play()
}
And do something to actually handle the error case rather than just printing the error.

In my case I was having the same issue and I found out that I needed to set this before start recording
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
Hope it helps anyone

I also got this problem, after checking up the audio file url, found that it is stored in Cache directory. so audio player probably couldn't find audio file according to your "url".
please ensure, the url path is under Document directory.

As others have said, the audio player couldn't find the file.
This helped me: Document directory path change when rebuild application
Basically, you cannot load files with an absolute reference, as the sandbox environment regenerates the absolute file url each time. So you will need to add a small bit of code (see above) to get the correct urls to use.

Related

What is wrong with this URL for AVAudioPlayer in Swift?

I tried to create AVAudioPlayer like this
let url = URL(string: "https://storage.googleapis.com/preview-public/project/e45d3194bb7f4768984fd53acc833600/fa13293c27314b448a815ebd42176684/audio-gnvY.m4a")!
do {
let player = try AVAudioPlayer(contentsOf: url)
} catch (let err) {
print("err", err)
}
I get this error: Error Domain=NSOSStatusErrorDomain Code=2003334207 "(null)".
I also tried
do {
let _ = try url.checkResourceIsReachable()
} catch (let err) {
print("err", err)
}
and I get this error: Domain=NSCocoaErrorDomain Code=262 "The file couldn’t be opened because the specified URL type isn’t supported.".
I can't figure out what could possibly be wrong with the url.
Images load normally from same bucket although if I check checkResourceIsReachable() on image URL it returns same error. (But I load image with CGImageSourceCreateWithURL(url as CFURL which might not have same checks as AVAudioPlayer.) Anyway I'm lost. Any ideas?
AVAudioPlayer is for local file: URLs only. It doesn’t accept remote URLs, which would need to be streamed. For that, use AVPlayer.
i have demo in try to run this code audio is working perfect
let audiourl = URL(string: "https://storage.googleapis.com/preview-public/project/e45d3194bb7f4768984fd53acc833600/fa13293c27314b448a815ebd42176684/audio-gnvY.m4a")
let player = AVPlayer(url: audiourl!)
let playViewController = AVPlayerViewController()
playViewController.player = player
self.present(playViewController, animated: true){
playViewController.player!.play()
}
In AVAudioPlayer, URL is the path to a local file.
https://developer.apple.com/documentation/foundation/url

Using AVAudioPlayer with Dynamic URL in Swift 3 causing Thread Errors

I am new to Swift and making an audio app using AVAudioPlayer. I am using a remote URL mp3 file for the audio, and this works when it's static.
For my use case, I want to pull a URL for an mp3 file from a JSON array and then pass it into the AVAudioPlayer to run.
If I move the AVAudioPlayer block into the ViewDidLoad and make the mp3 file a static URL, it will run fine.
Then, when I move this code into my block that extracts an mp3 url from JSON, I can print the URL successfully. But when I pass it into my audio player, problems arise. Here's the code.
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://www.example.com/example.json")
URLSession.shared.dataTask(with:url!, completionHandler: {(data, response, error) in
guard let data = data, error == nil else { return }
let json: Any?
do{
json = try JSONSerialization.jsonObject(with: data, options: [])
}
catch{
return
}
guard let data_list = json as? [[String:Any]] else {
return
}
if let foo = data_list.first(where: {$0["episode"] as? String == "Example Preview"}) {
self.audiotest = (foo["audio"] as? String)!
print(self.audiotest) // this prints
// where i'm passing it into the audio player
if let audioUrl = URL(string: self.audiotest) {
// then lets create your document folder url
let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// lets create your destination file url
let destinationUrl = documentsDirectoryURL.appendingPathComponent(audioUrl.lastPathComponent)
//let url = Bundle.main.url(forResource: destinationUrl, withExtension: "mp3")!
do {
audioPlayer = try AVAudioPlayer(contentsOf: destinationUrl)
} catch let error {
print(error.localizedDescription)
}
} // end player
// ....
Specifically, I get an error Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value when clicking a play button IBAction that is connected to the audio player. Finally, that action function looks like this:
#IBAction func playPod(_ sender: Any) {
audioPlayer.play()
}
Do you know where I'm going wrong? I'm confused as to why I can't print the URL and also get a response that the URL is nil in the same block, but maybe that's an asynchronous thing.
The problem is that you didn't save the mp3 file to documents and trying to play it
this line
audioPlayer = try AVAudioPlayer(contentsOf: destinationUrl)
assumes that there is a saved mp3 file in that path , but acutally there is no files you appended the audio extension on the fly
besides for steaming audio from a remote server, use AVPlayer instead of AVAudioPLayer.
AVPlayer Documentation
Also try this with urls parsed from json
var urlStr = (foo["audio"] as? String)!
self.audiotest = urlStr.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)

Play/Pause Audio Button Swift

I'm new to swift and can't seem to figure this out. I am trying to have one button act as both a play button which loops audio and as a pause button if clicked again. I've tried using an if/else statement but I keep getting this error "fatal error: unexpectedly found nil while unwrapping an Optional value"
The code works without the if/else statement, however, in that case it loops indefinitely without a way to pause it.
Any suggestions?
var hihat16: AVAudioPlayer!
#IBAction func hat16(_ sender: Any) {
if hihat16.isPlaying == false {
let path = Bundle.main.path(forResource: "Hat 017-1.aif", ofType:nil)!
let url = URL(fileURLWithPath: path)
do {
let sound = try AVAudioPlayer(contentsOf: url)
hihat16 = sound
sound.play()
hihat16.numberOfLoops = -1
} catch {
// couldn't load file :(
}
}else{ hihat16.stop()}
Picture of the code and error message :
Try this.
Do it in your viewDidLoad
let path = Bundle.main.path(forResource: "Hat 017-1.aif", ofType:nil)!
let url = URL(fileURLWithPath: path)
do {
hihat16 = try AVAudioPlayer(contentsOf: url)
hihat16.numberOfLoops = -1
} catch {}
Then in your #IBAction you can do this.
if !hihat16.isPlaying {
hithat16.play()
}
else{ hithat.stop()}

How to play NSData from Core Data in AVAudioPlayer

I save mp3 file into Core Data as NSData binary data. Then I load this file with help of NSFetchedResultsController. Then I wrote following code but I got the error. How can I fix it?
fatal error: unexpectedly found nil while unwrapping an Optional value
var audioPlayer = AVAudioPlayer()
func play() {
if musicData != nil {
audioPlayer = AVAudioPlayer(data: musicData, error: nil)
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
audioPlayer.prepareToPlay()
audioPlayer.play()
}
}
UPDATE
I checked it and I saw that AVAudioPlayer is nil but I init this player in beginning of this class.
Still I changed code but I get the same error
The second method
var av = AVAudioPlayer()
func play() {
let object = fetchedResultsController.fetchedObjects?.first as! Entity
let songData = object.mp3
var urlData: NSURL!
songData.writeToURL(urlData, atomically: true)
av = AVAudioPlayer(contentsOfURL: urlData, error: nil)
av.prepareToPlay()
av.play()
}
I added the file type hint and it works for me.
func playMusic() {
let url = NSBundle.mainBundle().URLForResource("Music", withExtension: "mp3")!
let data = NSData(contentsOfURL: url)!
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
audioPlayer = AVAudioPlayer(data: data, fileTypeHint: AVFileTypeMPEGLayer3, error: nil)
audioPlayer.prepareToPlay()
audioPlayer.play()
}
Had the same problem with playing mp3 file from Core Data. There is another solution, which don't require you to make resources, but only file manager . Works in Swift 5:
final class AudioPlayer: AVPlayer, ObservableObject {
private func changeSong(with newSong: Song) { // struct Song has function findInDatabase, which can return saved data from CoreData (or error)
// trying to find song in CoreData
newSong.findInDatabase() { (result: Result<SavedSong, CoreDataError>) in
switch result {
case .success(let savedSong):
// SavedSong struct has variable 'file' which is of type Data
guard let songFile = savedSong.file, !songFile.isEmpty else {
// your error handling
return
}
guard var localURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else { return } // or error handling
localURL.appendPathComponent("song.mp3")
try! songFile.write(to: localURL) // saving song data in url
// now just playing from created URL
let nextPlayerItem = AVPlayerItem(url: localURL)
self.replaceCurrentItem(with: nextPlayerItem) // 'self' - is AVPlayer instance
self.play()
case .failure( _):
// error handling, for example try to play from URL
}
}
}
}

How to initialize AVAudioPlayer using Swift

I'm creating a game using Swift and SpriteKit in Xcode 6.1 and I can't
get AVAudioPlayer to initialize. I have an SKScene for my menu, and I
want to run music in my "Sounds" folder called "menu.mp3." However, it
crashes every time I run my game. My code is below:
let filePath:NSURL = NSURL(fileURLWithPath: "menu.mp3")!
var er:NSError?
let audioPlayer:AVAudioPlayer = AVAudioPlayer(contentsOfURL: filePath, error: &er)
if (er != nil) {
} else {
audioPlayer.play()
}
I put this in my initializer right after my super.init call. The error I get follows:
fatal error: unexpectedly found nil while unwrapping an Optional value
I've tried a lot of fixes, but I can't figure out what's going wrong.
I've added the AVFoundation framework and imported it to this scene,
but it continues to crash. Is there something wrong with my filePath
that makes my audioPlayer not initialize or something? I've tried
switching to other ways and making different parts optional, but I
can't get it to work. Thanks!
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as NSURL
func preparePlayer(name:String){
var error:NSError?
let fileUrl = documentsUrl.URLByAppendingPathComponent(name)
// or
// let fileUrl = NSBundle.mainBundle().bundleURL.URLByAppendingPathComponent(name)
let audioPlayer = AVAudioPlayer(contentsOfURL: fileUrl, error: &error)
if error == nil {
audioPlayer.play()
}
}
override func viewDidLoad() {
super.viewDidLoad()
preparePlayer("menu.mp3")
}

Resources