Trying to open an mp3 file in app documents

TL;DR C function can't open MP3 file in app's documents directory, am I missing any sort of permissions?

I am trying to create an app to play music through the BASS Audio Library for C/C++ and while I have it playing music, I cannot seem to have it open local files.

To create a stream from a file to play in this library, you use BASS_StreamCreateFile(); function, which you pass a URL to the file to use, but even thought I can verify the URL I passing is correct and the file is in the files app, it throws error code 2 "Cannot open file"

However, when I use BASS_StreamCreateURL(); and pass in a URL from the internet, it works perfectly, so I have to assume the problem has something to do with file permissions.

Here is the C function in which I am creating these streams

int createStream(const char* url) {

    //HSTREAM stream = BASS_StreamCreateURL("https://vgmsite.com/soundtracks/legend-of-zelda-ocarina-of-time-original-sound-track/fticxozs/68%20-%20Gerudo%20Valley.mp3", 0, 0, NULL, 0);
    HSTREAM stream = BASS_StreamCreateFile(false, url, 0, 0, 0);


    if (stream == 0) {
        printf("Error at createStream, error code: %i\n", BASS_ErrorGetCode());
        return 0;
    } else {
        return stream;
    }
}

In the commented out line is the working Stream created from a URL

And here is the URL I am passing in

guard let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else { return }

gerudoValleyURL = documentsURL.appendingPathComponent("GerudoValley.mp3")

stream = createStream(gerudoValleyURL.absoluteString)

I can confirm that the MP3 "GerudoValley.mp3" is in the app's documents directory in the files app.

Is there anything I could do to allow this C file to open to open MP3's form the App's documents directory? The exact MP3 from that link is already there.

Answered by kevink2023 in 701851022

Fixed it! The library had a problem with the file URL, but by just using the method .standardizedFileURL on the URL I got in the documents directory, the library accepted it and worked perfectly.

So basically,

changing this:

stream = createStream(gerudoValleyURL.path)

to this:

stream = createStream(gerudoValleyURL.standardizedFileURL.path)

Fixed everything.

So note to self, use standardized URLs when working with external libraries. Seems pretty obvious but hey I'm learning.

Accepted Answer

Fixed it! The library had a problem with the file URL, but by just using the method .standardizedFileURL on the URL I got in the documents directory, the library accepted it and worked perfectly.

So basically,

changing this:

stream = createStream(gerudoValleyURL.path)

to this:

stream = createStream(gerudoValleyURL.standardizedFileURL.path)

Fixed everything.

So note to self, use standardized URLs when working with external libraries. Seems pretty obvious but hey I'm learning.

Trying to open an mp3 file in app documents
 
 
Q