jfversluis / Plugin.Maui.Audio

Plugin.Maui.Audio provides the ability to play audio inside a .NET MAUI application
MIT License
261 stars 46 forks source link

the first 5 time not sound #133

Open robytomasi opened 1 month ago

robytomasi commented 1 month ago

in a MAUI app the first 5 times no sound is played. After that everything is ok

bijington commented 1 month ago

You might need to give a bit more detail than that. Do you have a sample of the code that you can show us? What platform is this on? Etc

robytomasi commented 1 month ago

Good morning we are developing a small maui app that must work on android smartphones for reading barcodes. we wanted the smartphone to beep every time it is read the result is that during the first 5 scans the app does not beep. After the first 5 instead it starts to work almost regularly in fact randomly every now it does not beep

we attach code

best regards

    protected    void BarcodesDetected(object sender, BarcodeDetectionEventArgs e)

    {

        try 

        {

            ClsRighe myRighe = new ClsRighe();

            string Codice;

            string Qt;

            object a = new object();

            EventArgs b = new EventArgs();

            if (!isBarcodeProcessed)

            {

                isBarcodeProcessed = true;

                var result = e.Results?.FirstOrDefault();

                if (result is null) return;

                 Dispatcher.DispatchAsync(async () =>

                {

                    await PausaAsync(a, b);

                    Codice = result.Value;

                    Qt = "1";

                    ClsRiga Riga = new ClsRiga(Codice, Qt);

                    ClsGlobal.Righe.Add(Riga);

                    lstRighe.ItemsSource = null;

                    lstRighe.ItemsSource = ClsGlobal.Righe.Righe;

                                                                                          PlayBeep();

                    //await PlayBeep();

                    Vibration.Default.Vibrate();

                });

            }

            //LblErrore.Text = "";

            //LblErrore.Text = LblErrore.Text + "-ok";

        }

        catch (Exception ex) 

        {

            LblErrore.Text = LblErrore.Text + "rrore";

        }       

    }

                              private void BtnScan_Clicked(object sender, EventArgs e)

                              {

                                              isBarcodeProcessed = false;

                              }

                               public async Task PlayBeep()

                                           {

                                                            AudioPlayerViewModel Audio = new AudioPlayerViewModel();

                                                           Audio.PlayAudio();

                                            }

===================================================================================================================

xaml

                                            <?xml version="1.0" encoding="utf-8" ?>

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"

         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

         xmlns:zxing="clr-namespace:ZXing.Net.Maui.Controls;assembly=ZXing.Net.MAUI.Controls"

         x:Class="BarcodeZXingRedth.MainPage"

         Loaded="ContentPage_Loaded">

<ScrollView>

    <VerticalStackLayout

        Padding="30,0"

        Spacing="25">

        <Label x:Name="LblErrore" Text="dddd"/>

        <VerticalStackLayout>

            <zxing:CameraBarcodeReaderView

                  x:Name="cameraBarcodeReaderView"

                  BarcodesDetected="BarcodesDetected" VerticalOptions="Center" />

        </VerticalStackLayout>

        <VerticalStackLayout>

            <Label Text="" />

            <Button 

                x:Name="BtnScan"

                Text="SCAN"

                Clicked="BtnScan_Clicked"/>

            <Button 

                 x:Name="BtnPulisci"

                 Text="Pulisci"

                 Clicked="BtnPulisci_Clicked"/>

            <Label Text="" />

        </VerticalStackLayout>

        <VerticalStackLayout  >

            <ListView 

             x:Name="lstRighe"  

               HorizontalOptions="Center" >

                <ListView.ItemTemplate>

                    <DataTemplate>

                        <ViewCell>

                            <Grid Padding="2">

                                <Grid.RowDefinitions>

                                    <RowDefinition Height="Auto" />

                                </Grid.RowDefinitions>

                                <Grid.ColumnDefinitions>

                                    <ColumnDefinition Width="200" />

                                    <ColumnDefinition Width="50" />

                                </Grid.ColumnDefinitions>

                                <Entry Grid.Column="0"

                                      BackgroundColor="LightSkyBlue"

                                      Text="{Binding Codice}"

                                      IsReadOnly="True"/>

                                <Entry 

                                     Grid.Column="1"

                                     BackgroundColor="LightSkyBlue"

                                     Text="{Binding Qt}"

                                     IsReadOnly="False"/>

                            </Grid>

                        </ViewCell>

                    </DataTemplate>

                </ListView.ItemTemplate>

            </ListView>

        </VerticalStackLayout>

    </VerticalStackLayout>

</ScrollView>

===================================================================================================================

in MauiProgram

using Microsoft.Extensions.Logging;

using Plugin.Maui.Audio;

using ZXing.Net.Maui;

using ZXing.Net.Maui.Controls;

....

public static class MauiProgram

{

    public static MauiApp CreateMauiApp()

    {

        var builder = MauiApp.CreateBuilder();

        builder

            .UseMauiApp<App>()

            .UseBarcodeReader()

            .AddAudio()

            .ConfigureFonts(fonts =>

            {

                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");

                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");

            });

if DEBUG

                           builder.Logging.AddDebug();

endif

        return builder.Build();

    }

===================================================================================================================

AudioPlayerViewModel Class

using Plugin.Maui.Audio;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace BarcodeZXingRedth

{

public class AudioPlayerViewModel

{

    public async void PlayAudio()

    {

        var audioPlayer = AudioManager.Current.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("ScannerBeep.mp3"));

        audioPlayer.Play();

    }

}

}

Da: Shaun Lawrence @.> Inviato: giovedì 26 settembre 2024 14:14 A: jfversluis/Plugin.Maui.Audio @.> Cc: robytomasi @.>; Author @.> Oggetto: Re: [jfversluis/Plugin.Maui.Audio] the first 5 time not sound (Issue #133)

You might need to give a bit more detail than that. Do you have a sample of the code that you can show us? What platform is this on? Etc

— Reply to this email directly, view it on GitHub https://github.com/jfversluis/Plugin.Maui.Audio/issues/133#issuecomment-2376772918 , or unsubscribe https://github.com/notifications/unsubscribe-auth/BKH5VGXF75MTSLVCVIOOBQ3ZYP3ARAVCNFSM6AAAAABO4RHNXWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNZWG43TEOJRHA . You are receiving this because you authored the thread. https://github.com/notifications/beacon/BKH5VGT65DQIACZ4QGXJHG3ZYP3ARA5CNFSM6AAAAABO4RHNXWWGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTUNVKWTM.gif Message ID: @. @.> >

bijington commented 1 month ago

There is a chance that the audioPlayer that you are creating inside the PlayAudio method will be garbage collected because it becomes out of scope once the PlayAudio method completes. I would recommend that you keep a reference as a member variable rather than just a variable inside your method. I would further question whether you really need the AudioPlayerViewModel class at all and just keep a copy of IAudioPlayer inside the class handling the barcode detection.