adrianstevens / Xamarin-Plugins

Cross-platform Plugins for Xamarin, Xamarin.Forms and Windows
https://www.nuget.org/packages/Xam.Plugin.SimpleAudioPlayer/
MIT License
133 stars 53 forks source link

Load(Stream audioStream) error on iOS Could not initialize an instance of the type 'AVFoundation.AVAudioPlayer'. #42

Open DJMYK opened 5 years ago

DJMYK commented 5 years ago

Hello, I have a small problem, Using the library, when I try to use the Load() method with a byte [] instead of an .mp3 file, an exception occurs, only in iOS.

I use the library in the following way.

private void LoadAudio(byte[] audio)
{
    try
    {
        player.Load(new MemoryStream(audio);
    }
    catch(Exception ex)
    {
        Debug.WriteLine(ex.Message);
    }
}

This is the full error:

Could not initialize an instance of the type 'AVFoundation.AVAudioPlayer': the native 'initWithData:error:' method returned nil. It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false.

This is the stacktrace:

Foundation.NSObject.InitializeHandle (System.IntPtr handle, System.String initSelector) [0x000a8] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.11/src/Xamarin.iOS/Foundation/NSObject2.cs:504 at AVFoundation.AVAudioPlayer..ctor (Foundation.NSData data, System.IntPtr outError) [0x0001c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.11/src/Xamarin.iOS/AVFoundation/AVAudioPlayer.g.cs:89 at AVFoundation.AVAudioPlayer.FromData (Foundation.NSData data) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.11/src/Xamarin.iOS/AVFoundation/AVAudioPlayer.cs:133

Version: iOS 9.3.5

gonz01d commented 5 years ago

@DJMYK I also had this issue and have determined it is due to the actual source mp3 being played.

I've not looked in depth but I compared the Diminished.mp3 from the SimpleAudioPlayer sample project with my own and it would seem the absence of either Loudness Info: or sound check volume normalization gain data could be the cause.

Whether this is resolvable using the AVAudioSession.SharedInstance().Setxxxx methods I've not determined as yet or you may be able to modify your source mp3's themselves to enable playback :

Working file:

File:           Diminished.mp3
File type ID:   MPG3
Num Tracks:     1
----
Data format:     2 ch,  44100 Hz, '.mp3' (0x00000000) 0 bits/channel, 0 bytes/packet, 1152 frames/packet, 0 bytes/frame
                no channel layout.
estimated duration: 205.766525 sec
audio bytes: 8230661
audio packets: 7877
bit rate: 320000 bits per second
packet size upper bound: 1053
maximum packet size: 1045
audio data file offset: 4215
optimized
Loudness Info:
    sound check info                 :
        sc ave perceived power coeff     : "548 551 "
        sc max perceived power coeff     : "4371 4394 "
        sc peak amplitude msec           : "96107 96107 "
        sc max perceived power msec      : "91277 91277 "
        sc peak amplitude                : "32768 32768 "

sound check volume normalization gain: -1.00 dB
----

Failing file:

File:           01_she_caught_the_katy.mp3
File type ID:   MPG3
Num Tracks:     1
----
Data format:     2 ch,  44100 Hz, '.mp3' (0x00000000) 0 bits/channel, 0 bytes/packet, 1152 frames/packet, 0 bytes/frame
                no channel layout.
estimated duration: 253.074275 sec
audio bytes: 10122971
audio packets: 9688
bit rate: 320000 bits per second
packet size upper bound: 1052
maximum packet size: 1045
audio data file offset: 61436
optimized
----
gonz01d commented 5 years ago

Having looked further at other working mp3's it's possible the audio data file offset: may be relevant as my non working file has a significantly higher value (61436) than my working mp3's.

DJMYK commented 5 years ago

Thank you @gonz01d , I will review what you tell me, hoping to find a solution soon.

UdaraAlwis commented 5 years ago

Having the same issue here. Thanks @gonz01d for the well analyzed response. Appreciate it. :) Keep it up!

sethom commented 4 years ago

This is happening to me with wav files.

vecalion commented 3 years ago

Hey, did anyone find a workaround for this issue?

pooran commented 1 year ago

Do we have an ETA to fix this issue?