adrianstevens / Xamarin-Plugins

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

it crash after play voice #78

Closed overstartup closed 2 years ago

overstartup commented 3 years ago

it works fine in the simulator but after installing the app on the device, it crashing on the play .mp3 file.

report-2517906897329959999-eed857eb-ef26-4e28-90cc-04183a2f304f.txt

this is my class:

    public static class AudioService
    {
        public static ISimpleAudioPlayer playerBG =   Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.CreateSimpleAudioPlayer(); 
        public static ISimpleAudioPlayer playerSLId = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();

        public static async void PlayBackground()
        { 
                playerBG = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();
                playerBG.Loop = true;

                playerBG.Load(GetStreamFromFile("sound1.mp3"));
                playerBG.Play();

        }
        public static async void init(String filename)
        { 
                playerSLId = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();
                await Task.Run(async () =>
                {
                    await PlayAudio();
                });

                async Task PlayAudio()
                {
                    playerSLId = CrossSimpleAudioPlayer.Current;
                    string url = filename;
                    using var httpClient = new HttpClient();
                    var fileStream = await httpClient.GetStreamAsync(url);
                    playerSLId.Load(fileStream);
                    playerSLId.Play(); 
                }

        }

        static Stream GetStreamFromFile(string filename)
        {
            var assembly = typeof(App).GetTypeInfo().Assembly;
            var stream = assembly.GetManifestResourceStream("appnameV1." + filename);
            return stream;
        }

    }
SimpleAudioPlayerImplementation.OnPlaybackEnded (System.Object sender, System.EventArgs e)
Java.Lang.IllegalStateException: Exception of type 'Java.Lang.IllegalStateException' was thrown.
JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args)
JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters)
MediaPlayer.Prepare ()
SimpleAudioPlayerImplementation.OnPlaybackEnded (System.Object sender, System.EventArgs e)
MediaPlayer+IOnCompletionListenerImplementor.OnCompletion (Android.Media.MediaPlayer mp)
MediaPlayer+IOnCompletionListenerInvoker.n_OnCompletion_Landroid_media_MediaPlayer_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_mp)
(wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.115(intptr,intptr,intptr)
java.lang.IllegalStateException
android.media.MediaPlayer._prepare(Native Method)
android.media.MediaPlayer.prepare MediaPlayer.java:1196
mono.android.media.MediaPlayer_OnCompletionListenerImplementor.n_onCompletion(Native Method)
mono.android.media.MediaPlayer_OnCompletionListenerImplementor.onCompletion()
android.media.MediaPlayer$EventHandler.handleMessage MediaPlayer.java:2813
android.os.Handler.dispatchMessage Handler.java:106
android.os.Looper.loop Looper.java:209
android.app.ActivityThread.main ActivityThread.java:5900
java.lang.reflect.Method.invoke(Native Method)
java.lang.reflect.Method.invoke Method.java:372
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run ZygoteInit.java:1005
com.android.internal.os.ZygoteInit.main ZygoteInit.java:800
adrianstevens commented 2 years ago

Please provide device make/model and the OS version - closing issue but please feel free to reopen with additional details