Monotouch ios - audio player returns zero on initialization
Ok, I have had this problem for almost a month now. Some audio files cannot be played. It just returns an error like this:
Failed to initialize instance of type "AVFoundation.AVAudioPlayer": built-in method initWithContentsOfURL: error: 'returned nil.
Here's the code to initialize AudioPlayer:
NSData data = new NSData();
if (AppSession.IsConnected ()) {/*Just a checker if connection is available or not*/
if (uri != null && uri.ToString().Length > 0) {
data = await LoadData (uri);
}
} else { data = null; }
string saveFile = FolderPath(uri, "playthrough");
data.Save(saveFile, false);
NSUrl fileUrl = NSUrl.FromString(saveFile);
audioplayer = AVAudioPlayer.FromUrl(fileUrl);
audioplayer.NumberOfLoops = 0;
audioplayer.Volume = 1.5f;
audioplayer.PrepareToPlay();
audioplayer.Play();
For LoadData:
public static async Task<NSData> LoadData (NSUrl url)
{
NSData data = new NSData ();
if (url.ToString ().Contains ("http")) {
var httpClient = new HttpClient ();
Task<byte[]> contentsTask = httpClient.GetByteArrayAsync (url.ToString ());
var contents = await contentsTask;
data = NSData.FromArray(contents);
} else { data = NSData.FromUrl (url); }
return data;
}
For FolderPath:
public static string FolderPath(NSUrl url, string fileName)
{
string[] dotSplitter = url.ToString ().Split (new char[]{ '.' }, 4);
string ext = "";
if (dotSplitter.Length == 4) {
switch (dotSplitter [3]) {
case "wav": ext = ".wav"; break;
case "mp3": ext = ".mp3"; break;
case "3gpp": ext = ".3gpp"; break;
case "mp4": ext = ".mp4"; break;
}
} else {
switch (dotSplitter [0]) {
case "wav": ext = ".wav"; break;
case "mp3": ext = ".mp3"; break;
case "3gpp": ext = ".3gpp"; break;
case "mp4": ext = ".mp4"; break;
}
}
return Path.Combine(TMPDir(), fileName + ext);
}
And here are the files I'm using to test the sound:
- http://files.parsetfss.com/6ea4a3c5-a4e2-463f-8374-247d5db0fbd5/tfss-c7db3001-b7b0-465d-b59b-233c1fe568ec-filtered_427201531308PM_song.wav
- http://files.parsetfss.com/6ea4a3c5-a4e2-463f-8374-247d5db0fbd5/tfss-c4426fba-ea52-4764-9fb6-6b9f10aba89f-filtered_27042015154318_song.wav
So yes. I've done dozens of research, google, experiment and tears for this, but nothing worked for me. Any solutions for this?
source to share
Full exception you should get:
System.Exception: Failed to initialize instance of type "MonoTouch.AVFoundation.AVAudioPlayer": built-in initWithContentsOfURL: error :: nil. This condition can be ignored by setting the MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure parameter to false.
Following the advice above (use an overload that accepts NSError
) and one of the exception message, you will end up with something like:
MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure = false;
NSError error;
using (var url = NSUrl.FromString ("http://files.parsetfss.com/6ea4a3c5-a4e2-463f-8374-247d5db0fbd5/tfss-c7db3001-b7b0-465d-b59b-233c1fe568ec-filtered_427201531308PM_song.wav"))
using (var player = AVAudioPlayer.FromUrl (url, out error)) {
Console.WriteLine (error);
}
You'll get:
The operation could not be completed. (OSStatus bug 2003334207.)
which is a very common mistake for AVAudioPlayer
. I also get the same error if you are using NSData
(local file). However AVPlayer
loads the url just fine ...
source to share