Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DJMYK opened this issue Dec 16, 2018 · 7 comments

Comments

@DJMYK
Copy link

DJMYK commented Dec 16, 2018

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
Copy link

gonz01d commented Apr 17, 2019

@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
Copy link

gonz01d commented Apr 17, 2019

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
Copy link
Author

DJMYK commented May 20, 2019

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

@UdaraAlwis
Copy link

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

@sethom
Copy link

sethom commented Mar 5, 2020

This is happening to me with wav files.

@vecalion
Copy link

vecalion commented Sep 24, 2020

Hey, did anyone find a workaround for this issue?

@pooran
Copy link

pooran commented May 29, 2023

Do we have an ETA to fix this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants