Bug 14307 - AVAudioPlayer.FromUrl fails but returns an null error
Summary: AVAudioPlayer.FromUrl fails but returns an null error
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 6.4.4
Hardware: Macintosh All
: --- normal
Target Milestone: Untriaged
Assignee: Larry O'Brien
URL:
Depends on:
Blocks:
 
Reported: 2013-08-28 23:11 UTC by pierre
Modified: 2017-05-26 23:37 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description pierre 2013-08-28 23:11:14 UTC
The following code fails (audioPlayer == null) but returns an null error (loadError == null).

NSError loadError;
audioPlayer = AVAudioPlayer.FromUrl(new NSUrl(validParthToTheMusic), out loadError);

After having looked carefully to the sample I found the problem. By adding 'false' to the second argument of NSUrl constructor it works properly (audioPlayer  == null and loadError != null or audioPlayer != null and loadError == null).

NSError loadError;
audioPlayer = AVAudioPlayer.FromUrl(new NSUrl(validParthToTheMusic, false), out loadError);
Comment 1 Sebastien Pouliot 2013-08-29 09:39:59 UTC
Can you give us an example of the value for `validParthToTheMusic` ?

And where's the file it load is actually located ?
Comment 2 pierre 2013-08-29 21:21:10 UTC
Sorry for the lack of precision, after a check the bug seems to happen only on the iPhoneSimulator.

My file is loaded as Content.
Here is an example of path:

/Users/pierre/Library/Application Support/iPhone Simulator/6.1/Applications/16A89A50-94EE-4CAF-A4EF-B56B8DD54ED7/AudioTestsiOS.app/app_data/assets/audio/ae.wav"
Comment 3 Sebastien Pouliot 2013-09-03 10:30:07 UTC
My guess* is that your `NSUrl` instance is invalid, i.e. it's handle property is 0x0 (IntPtr.Zero). E.g.

   string file = Path.Combine (NSBundle.MainBundle.ResourcePath, "Hand.wav");

returns:

"/var/mobile/Applications/8F5973F0-D19F-4E55-A147-430CE28FFD83/monotouchtest.app/Hand.wav" 

on iOS devices, while it returns:

"/Users/poupou/Library/Application Support/iPhone Simulator/6.1/Applications/81F5B81E-B279-416D-BB63-671FB8B407FF/monotouchtest.app/Hand.wav"

on the iOS simulator.

The first one can be used with `initWithString:` (native), which is `NSUrl(string)` while the second can not.

Using `NSUrl(string,bool)` calls `initFileURLWithPath:isDirectory:` which works in both case (note the difference between initWith*String* and init*WithPath*).


So what this means it that you're effectively calling `initWithContentsOfURL:error:` (AVAudioPlayer.FromUrl) with a null `NSUrl` which is not allowed (and undefined). Sadly it looks like the native returns `nil` without setting the `NSError` parameter to anything useful.


* I can duplicate something like this (even if I can't be sure it's 100% identical to yours).


c.c. Larry for updating documentation (based on above). Note that [1] is currently bad.

[1] http://api.xamarin.com/?link=C%3aMonoTouch.Foundation.NSUrl.NSUrl(string%2cbool)
Comment 4 Sebastien Pouliot 2013-09-03 10:57:08 UTC
Note that `Class.ThrowOnInitFailure` which should check for `Handle == IntPtr.Zero`, does not work in this case. I filled bug #14399 about it.
Comment 5 Larry O'Brien 2017-05-26 23:37:54 UTC
Documentation updated in 5a5880ae920dfac98d58fb6f5246de912165bdd7