Bob Barbara
Bob Barbara

Reputation: 45

NSUrl Not Initializing properly

I've placed 25 mp3 files in the "Music folder" in my Xamarin.ios project. I called the Play function and passed in the name of a mp3 file I want to play. I created a string object which contains the filePath. Then I create a another string object called "withFileName" containing the path and song name passed into the function. The withFileName variable looks correct.

When I execute the instruction ...

songURL = new NSUrl(withFileName);

I get the following exception...

System.Exception: Could not initialize an instance of the type 'Foundation.NSUrl': the native 'initWithString:' method returned nil. It is possible to ignore this condition by setting MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure to false. at Foundation.NSO…

I'm lost, need some help.

code snippet:

public void Play(String song)
    {

        String dir = Directory.GetCurrentDirectory();
        String filePath = Path.Combine(dir, "Music");

        String withFileName = String.Format("{0}/{1}", filePath, song);

        NSUrl songURL = null;  

        try
        {

            songURL = new NSUrl(withFileName);
        }
        catch (Exception e)
        {
            string msg = e.Message;  
        }

        NSError err;

        _audioPlayer = new AVAudioPlayer( songURL,"Song", out err );
        _audioPlayer.Play();

    }

Upvotes: 0

Views: 1577

Answers (1)

Ax1le
Ax1le

Reputation: 6641

It seems You can get the correct path of file embeded in the project. Then if you want to get the file's url you should use:

new NSUrl(filePath, false);
//or
NSUrl.FromFilename(filePath)

Upvotes: 2

Related Questions