Play Sound in .NET MAUI with Plugin.Maui.Audio

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ค. 2024
  • If you need to play a small sound effect or just a song this plugin is for you! With Plugin.Maui.Audio it's very easy to just add one or multiple sounds to your application. Ideal for your audio feedback whenever the user taps on something or maybe you're building a game PEW PEW!
    💝 Join this channel to get access to perks:
    th-cam.com/channels/ral.html...
    Disclaimer: this channel is done on personal title, in my free time, and not officially affiliated with or endorsed by Microsoft (my employer) in any way. Opinions and views are my own.
    🛑 Don't forget to subscribe to my channel for more cool content: th-cam.com/users/GeraldVerslui...
    🐦 Follow me on Twitter: / jfversluis
    🤝 Join the Discord server: / discord
    🔗 Links
    Sample Code Repo: github.com/jfversluis/MauiPlu...
    Plugin.Maui.Audio Repo: github.com/jfversluis/Plugin....
    .NET MAUI Docs: docs.microsoft.com/dotnet/com...
    ⏱ Timestamps
    00:00 - Play Sound in .NET MAUI
    00:13 - What is Plugin.Maui.Audio?
    01:10 - Add NuGet Package
    01:54 - Use with Dependency Injection
    03:26 - Implement Playing a Sound
    06:04 - Explore AudioPlayer APIs
    07:44 - Prevent Memory Leaks!
    08:35 - Play a Sound The Easy Way
    09:12 - Bring Your Ideas!
    🎥 Video edited with Filmora
    🙋‍♂️ Also find my...
    Blog: blog.verslu.is
    Twitch: / jfversluis
    All the rest: jfversluis.dev
    #dotnetmaui #sound #audio #dotnet6 #maui
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 126

  • @DonovanBrown
    @DonovanBrown 11 หลายเดือนก่อน +4

    I really appreciate how you always start from File New Project and show how everything needs to be setup.

    • @jfversluis
      @jfversluis  11 หลายเดือนก่อน +1

      Hey Donovan! Good seeing you here! 😄 thanks so much for the kind words

  • @MarownIOM
    @MarownIOM ปีที่แล้ว +1

    That is exactly what I needed. It works great.

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      Perfect, glad to hear it!

  • @FunnyAnimals-mo8cu
    @FunnyAnimals-mo8cu ปีที่แล้ว +1

    Thanks Gerald, great content. I'm already making use of this.

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      Oh that’s great, thanks! Let us know if you need anything

  • @Otonium
    @Otonium ปีที่แล้ว +2

    Sound was always very useful, nice to have a plugin for MAUI!

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      Let us know what you think of you decide to try it!

  • @ericritter46
    @ericritter46 20 วันที่ผ่านมา

    I think you're right. This seems much more appropriate to use. Thanks so much, dude!

  • @user-et1qh3mk2u
    @user-et1qh3mk2u 6 หลายเดือนก่อน +2

    Hi. Thanks for the great plugin. In the video you recommend disposing of the IAudioPlayer to protect against memory leaks. I'm attempting to do this but the Dispose can't be used after calling the Play method as the class is still in use and causes a COMException on Windows. I've tried to use the PlaybackEnded event to tidy up the resource but on Windows the sender is a Windows.Media.Playback.MediaPlayer object so I can't use this as it isn't platform independent. The EventArgs parameter has no information relating to the IAudioPlayer that has just completed. How do you recommend tidying up the resource once the play back is complete?

  • @mbrotto1970
    @mbrotto1970 ปีที่แล้ว

    Great thanks. This works for MAUI hybrid as well. Much appreciated.

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      Great! Yeah most of the things out there should work in some way with Blazor Hybrid as well

  • @emmanueladebiyi2109
    @emmanueladebiyi2109 ปีที่แล้ว

    Super helpful. Thanks @Gerald

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      Glad to hear that! Thanks!

  • @PierreH1968
    @PierreH1968 ปีที่แล้ว +2

    That's great! it should be part of Microsoft.Maui.Media namespace. Thanks Gerald!

    • @jfversluis
      @jfversluis  ปีที่แล้ว +1

      That would be awesome if that happened! Thanks Pierre!

    • @PierreH1968
      @PierreH1968 ปีที่แล้ว

      @@jfversluis The API is clean , easy to use, and is implemented on all platforms. The only thing I see is that it is implicitly async (i.e. sounds play while async animations play) but it is not explicitly async (no await on the Play()) .
      Meaning Play() has to be called before awaits and cannot be part of a Task.WhenAll.... not a big deal, but since it's async anyway.

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      Hm if that’s something you’d like to see changed or added, make sure to open an issue so we can discuss!

  • @Conf-nh6mx
    @Conf-nh6mx ปีที่แล้ว

    Great! Thaks!

  • @quanyufeng579
    @quanyufeng579 ปีที่แล้ว

    thank u so much, now i can finish my assignmenmt!

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      Awesome! Good luck!

  • @MrDaveCoetzee
    @MrDaveCoetzee ปีที่แล้ว +2

    Thank you Gerald, nice tool. I am using it in a small app. I found that if you are going to use the player.Dispose() bit, you need to put in a delay first, otherwise you get an exception, i assume because its trying to discard the player while its still playing.

    • @jfversluis
      @jfversluis  ปีที่แล้ว +1

      Ah that is valuable information! If you don't mind, could you see if there is an issue for that on the repo or open one with all the details you might have around this? Then we can have a look. Thank you!

  • @iraqiroyale4483
    @iraqiroyale4483 10 หลายเดือนก่อน +3

    when looping the sound there is short delay before the sound replays can i delete that and make the sound replays faster without delay?

  • @harag9
    @harag9 ปีที่แล้ว +3

    Thanks for sharing this, however I'm surprised it's not in the main MAUI code as it's such an obvious thing to have.

  • @Mielesplayz
    @Mielesplayz ปีที่แล้ว

    Nice video, I am sure I will find a use for this.

    • @jfversluis
      @jfversluis  ปีที่แล้ว +1

      Awesome! Good luck!

  • @marcmognol
    @marcmognol ปีที่แล้ว

    Great. It will replace my own implementation ! Good job 👍

    • @jfversluis
      @jfversluis  ปีที่แล้ว +1

      Awesome! Let us know if you’re still missing something in comparison!

  • @datadude67
    @datadude67 ปีที่แล้ว +5

    Great work Gerald! I assume I can use a "Using" block around the player instead of calling the Dispose method correct? Also, do you think there would be any benefits of keeping a reference to streams that are used repeatedly (i.e. every 10 seconds over a period of 2 hours) rather than executing FileSystem.OpenAppPackage...?

    • @MarownIOM
      @MarownIOM ปีที่แล้ว +1

      that's what I did. so far no leaks. I put it in a service that loads the good.wav, bad.wav, question.wav and put that into DI.

    • @ErimlRGG
      @ErimlRGG 7 หลายเดือนก่อน +1

      I made it a ObservableProperty in my viewModel, initilize it my page and just used the Play method on the viewModel. That fixed not getting the sound after a few times since I replayed it every few seconds since I'm making a BattleShip game and using the sounds for the attacks

  • @danielbaraka3991
    @danielbaraka3991 ปีที่แล้ว

    Thanks for this it's really what I needed I think also you'll share the full music player where we can stream Audio in File Directory. Thanks!

  • @michaelbrooks5776
    @michaelbrooks5776 9 หลายเดือนก่อน

    Awesome video Gerald. Is it possible to use this plugin and stream real-time voice data like a push-to-talk walkie talkie? I know web-rtc and signal-r will come into play. Not sure if MAUI/MAUI Blazor has libraries/api's for this?

  • @Dave-FarFlyer
    @Dave-FarFlyer ปีที่แล้ว

    Thanks a lot Gerald! This is more than welcome!
    I guess that it's possible to do it also from the code in the ViewModel, how would you do that in a clean way?

    • @jfversluis
      @jfversluis  ปีที่แล้ว +1

      Absolutely! Check out the sample app we have in the plug-in repository that has a MVVM style sample

  • @mariocorreia7167
    @mariocorreia7167 9 หลายเดือนก่อน

    Hi Gerald I already learned some stuff with your videos and thank you for that! Can you make a video on how to change the notification sound on a
    API above 26, I'm unable to make a custom sound to sound when the notification appears, I always get the default sound.

  • @GuildOfCalamity
    @GuildOfCalamity ปีที่แล้ว

    Finally, thank you!
    Never understood how Xamarin supported audio and yet MAUI doesn't?

    • @jfversluis
      @jfversluis  ปีที่แล้ว +2

      On Xamarin it's only supported through a plugin as well and that simply wasn't ported to .NET MAUI yet, now it is!

  • @jacktheripper571
    @jacktheripper571 ปีที่แล้ว

    Maui is very exciting! I have a question; i want to have a background music on my app that will start right after the splash screen. Is there any way to do it now?

  • @tore28
    @tore28 7 หลายเดือนก่อน

    Very clear presented, thank you for creating such helpful functionality for cross platform apps ! Is there a widget also planned for this, to add UI control of audio files ?

    • @jfversluis
      @jfversluis  7 หลายเดือนก่อน +1

      Thank you! This is meant to be a non-visual control. I guess for a visual one you could look into MediaElement: th-cam.com/video/_sp4RG0I0x4/w-d-xo.html

  • @niharikasaraswat2181
    @niharikasaraswat2181 ปีที่แล้ว

    Hey @Gerald Can we detect signal strength updating value in xamarin forms using Telephony manager because I didn't find any event in this library related to this

  • @matrixugly
    @matrixugly 9 หลายเดือนก่อน

    yo! Thanks for the video. I came here from your other video on recording sound in Xamarin Forms (uses Plugin.AudioRecorder package I think?)
    I'm curious if that package works with MAUI apps.

    • @jfversluis
      @jfversluis  9 หลายเดือนก่อน

      Plugin.Maui.Audio now also records! Check the preview version

  • @deaasaleh3613
    @deaasaleh3613 ปีที่แล้ว

    Great 👍

  • @Mielesplayz
    @Mielesplayz ปีที่แล้ว +1

    Can you make a tutorial about Bluetooth connections?
    (I want to connect my smart watch to my application and read its data)

  • @haroldscott3402
    @haroldscott3402 11 หลายเดือนก่อน

    Great add on. Is there a way to declare it globally where I can turn it on with one button and stop & dispose with another button. If I declare in both buttons, it will work first time, but not the 2nd... it will start the 2nd time, but not stop the 2nd time.

  • @cassioinfo
    @cassioinfo ปีที่แล้ว

    Great work! What is the best way to use this plugin to play multiple audio files?

    • @jfversluis
      @jfversluis  ปีที่แล้ว +1

      Create multiple players :)

  • @joshuacamaioni1436
    @joshuacamaioni1436 ปีที่แล้ว

    Great video!! Will this also work on .Net Maui 7?

  • @ryandemboski6313
    @ryandemboski6313 6 หลายเดือนก่อน +1

    im a little confused on where we are supposed to put the player.Dispose(), when i put it in my playAudio method right after the player.Play() line i get a runtime error

  • @Danielkaas94
    @Danielkaas94 11 หลายเดือนก่อน

    Thanks! 🙏
    ❤🧡💛💚💙💜🤍

    • @jfversluis
      @jfversluis  11 หลายเดือนก่อน +1

      You’re welcome! My (Dutch?) friend!

    • @Danielkaas94
      @Danielkaas94 11 หลายเดือนก่อน

      @@jfversluis Danish! 🧀🧀

    • @jfversluis
      @jfversluis  11 หลายเดือนก่อน

      Aaah very good haha!

    • @Danielkaas94
      @Danielkaas94 11 หลายเดือนก่อน

      @@jfversluis I have a small issue with this, testing on windows, sounds play just fine, but on Android, it's like the end of the audio is cut short 🔜🔚

    • @Danielkaas94
      @Danielkaas94 11 หลายเดือนก่อน

      @@jfversluis When checking the value of IAudioPlayer.Duration, the precision are not identical campared we Windows and Android
      var player = audioManager.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("dswpnup.wav"));
      var duration = player.Duration;
      /*
      player.Duration
      Android: 0.532
      Windows: 0.5328798
      */

  • @mreirik2151
    @mreirik2151 ปีที่แล้ว

    Im creating a app for my familiy. Planning, calendar and more. It is Azure hosted grpc service, which uses streaming events for all users and mvvm databinding in front.
    Very nice to see this.
    I have 1 problem though, all my iOS input controls is unresponsive, but, android/windows works glitchy ok

  • @brenthebert472
    @brenthebert472 9 หลายเดือนก่อน

    Gerald, I would like to implement this code in my ViewModel. My audio clip is Binding from a json file. Any tips on doing this. I have a list of birds and then clicking on one to play ots chirping sound.

  • @jaimestuardo4655
    @jaimestuardo4655 5 หลายเดือนก่อน

    Hello.... do I need methods like these to play notification sounds? for example, if an error occurs in the application, an error sound should be played. The same for a success sounds.
    Regards
    Jaime

  • @sumquodsum6160
    @sumquodsum6160 ปีที่แล้ว

    That is exactly the kind of sound file I want to play.
    Now I need a "sound file picker" that will also work on at least Windows / Android and hopefully IOS. Any samples that let me pick a sound and ... play a sound from a filename or path or however it has to be done? While I am overall highly experienced I have no exposure to Android / IOS coding so I can't just write my own platform code... I don't think.

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      There is a file picker already available in .NET MAUI :)
      This video covers it for Xamarin.Forms but it should be largely the same for .NET MAUI th-cam.com/video/4gV9ZV61rn4/w-d-xo.html

  • @FuzzyDPozzy
    @FuzzyDPozzy 22 วันที่ผ่านมา

    so you created this plugin? I am wondering just curious how to do you create these types of plugins is c# is used or another programming language?

    • @jfversluis
      @jfversluis  22 วันที่ผ่านมา

      I did! I have a whole video about how to do it yourself, maybe that helps: th-cam.com/video/ZCQrlGT7MhI/w-d-xo.html

  • @MrShotkey
    @MrShotkey ปีที่แล้ว

    Thank you Gerald.. how can we record audio in .net maui, and if its not yet available when it it will be?

    • @jfversluis
      @jfversluis  ปีที่แล้ว +1

      We're thinking about adding that here as well. No timeline yet, thanks!

  • @odnalroemosiaw746
    @odnalroemosiaw746 ปีที่แล้ว

    Thanks very much for this code package (201 download). I am working on an app that requires sound and the ability to drag one object to another. Now I resolved sound issue need to resolved the drag issue. The DragGestureRecognizer takes too long to be recognized needs to implement something like swipe but dragging an object to any location on the screen. open for suggestion.

  • @raju145inYT
    @raju145inYT 11 หลายเดือนก่อน

    Thank you Gerald, is it possible play same audio in andirod auto. any references will be helpful.

    • @jfversluis
      @jfversluis  11 หลายเดือนก่อน

      Maybe this will help th-cam.com/video/nNkVxegb2oU/w-d-xo.html

  • @r.gopalakrishnan6926
    @r.gopalakrishnan6926 ปีที่แล้ว

    👌

  • @daveplatt6492
    @daveplatt6492 ปีที่แล้ว

    Any news around a video player? I need one now, and can't find any. Tx

    • @jfversluis
      @jfversluis  ปีที่แล้ว +1

      Working on MediaElement in the .NET MAUI Community Toolkit. I’ll try to release an early preview video this week

  • @user-zq7ug4bj6s
    @user-zq7ug4bj6s ปีที่แล้ว

    great work gerald but how can we record audio using maui

    • @jfversluis
      @jfversluis  ปีที่แล้ว +1

      We're working on that to include it in this project, but in the meantime: www.nuget.org/packages/Plugin.Maui.AudioRecorder there are some more others

  • @yousefsayed5909
    @yousefsayed5909 ปีที่แล้ว

    Excellent, how do I play a selected file using the file picker

    • @jfversluis
      @jfversluis  ปีที่แล้ว +1

      Use file picker, open the stream, use that with this plugin and voila!

    • @yousefsayed5909
      @yousefsayed5909 ปีที่แล้ว

      selecctedFile.OpenReadAsync()

  • @hallythejester1234
    @hallythejester1234 2 หลายเดือนก่อน

    Hey, so I am a little confused as to why the method for the audio player needs to be async. Reason being is, well, what part of the code you showed us guarantees that the player will finish playing before it’s disposed? Y’know? The thing about async programming is it gives the compiler the ability to execute multiple lines of code simultaneously… I am worried that disposing the audio directly after it is played may be a race condition error waiting to happen

    • @jfversluis
      @jfversluis  2 หลายเดือนก่อน

      Typically you will want to play a sound as a result or play music even, it wouldn’t be great if that blocked the ui and you would have to wait for it to be finished.

    • @hallythejester1234
      @hallythejester1234 2 หลายเดือนก่อน

      @@jfversluis that’s a fair statement. Thanks for telling me

  • @markoviitanen2671
    @markoviitanen2671 ปีที่แล้ว

    I made timer that plays sounds when round starts or breaks starts, but weirdly it does not play sounds every time. Can it be something wrong with sync? It seems to be totally random.

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      Hm interesting. If you could put it in a small reproduction sample and report it in the repository? Then we can have a look at what might be going on.

    • @markoviitanen4441
      @markoviitanen4441 ปีที่แล้ว

      @@jfversluis I added you as collaborator for ETS-RT which is the app. For me it is just practice app. But I would like to have it working, as it is good for our training evenings in Brazilian Jiujitsu. Feel free to check what ever you find.... it might not be the most beautiful code. I am rather new to this :P

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      @@markoviitanen4441 Ah sorry for not being clear. I meant "we" as the .NET MAUI team. Unfortunately I can't look into private repositories like this. Sorry!

  • @jamg.barett749
    @jamg.barett749 10 หลายเดือนก่อน

    How could I use with a streaming of radio url?

    • @jfversluis
      @jfversluis  10 หลายเดือนก่อน

      Not possible with this right now. You can use MediaElement
      Introducing MediaElement for .NET MAUI - Playing Video & Audio
      th-cam.com/video/_sp4RG0I0x4/w-d-xo.html

  • @yousefsayed5909
    @yousefsayed5909 ปีที่แล้ว

    Is there a possibility to play a sound using a title url

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      What is a title url?

  • @tuyetcumeo2001
    @tuyetcumeo2001 9 หลายเดือนก่อน

    It is possible to play mp3 sound from a url ? thanks

    • @jfversluis
      @jfversluis  9 หลายเดือนก่อน

      Unfortunately not yet!

  • @vedatg80
    @vedatg80 ปีที่แล้ว

    For those getting file not found error on Android , please include to your csproj file to any ItemGroup

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      Thanks for sharing! That should be in your csproj for a new project by default though 🤔

  • @luislhg
    @luislhg ปีที่แล้ว

    Any way to play a video?

  • @user-sr2em5ob7i
    @user-sr2em5ob7i 9 หลายเดือนก่อน

    Hey, for me sound is not playing in iPhone, it only plays in simulators . What am I missing

    • @jfversluis
      @jfversluis  9 หลายเดือนก่อน

      Did you try the silent switch on the side?

  • @krzysztofb.6396
    @krzysztofb.6396 11 หลายเดือนก่อน +1

    yes, .wav working but try with mp3 - this format doesn't work

    • @jfversluis
      @jfversluis  11 หลายเดือนก่อน +1

      That depends on the underlaying platform and the formats supported by that

  • @jazijim
    @jazijim 21 วันที่ผ่านมา

    can play mp3 files?

  • @moayadmyro2091
    @moayadmyro2091 3 หลายเดือนก่อน

    Not working in case you want to play from url I have to download the file first 😢

  • @Lee-ix5sm
    @Lee-ix5sm ปีที่แล้ว

    Eu estou precisando criar um player que direciona o Playlist para a saída padrão e tem a opção de clicar em um áudio e enviar para um segunda saída, um segundo dispositivo, para isso preciso selecionar os dispositivos de e definir no player em qual quero que ele rode.

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      Sounds like a great and challenging project 😉

    • @Lee-ix5sm
      @Lee-ix5sm ปีที่แล้ว

      @@jfversluis THANK YOU

  • @fallout9285
    @fallout9285 9 หลายเดือนก่อน

    Good afternoon, why is it that when I press the button, it freezes and nothing happens??there is no music

    • @jfversluis
      @jfversluis  9 หลายเดือนก่อน

      Not sure, report an issue to the repo with a reproduction sample if you want us to take a look

    • @fallout9285
      @fallout9285 9 หลายเดือนก่อน

      thanks for the quick response, I found the problem, everything works

    • @jfversluis
      @jfversluis  9 หลายเดือนก่อน +1

      @fallout9285 perfect!

    • @fallout9285
      @fallout9285 9 หลายเดือนก่อน

      @@jfversluis hello again, when I created another page and registered everything the same way, another error occurs,
      Severity Code Description Project File String Suppression Status
      Error CS7036 There is no argument corresponding to the required parameter "AudioManager" from "StartPage.StartPage(IAudioManager)". MauiApp1 (net7.0-android), MauiApp1 (net7.0-ios), MauiApp1 (net7.0-mac catalyst), MauiApp1 (net7.0-windows10.0.19041.0) C:\Users\Fallout\source
      epos\Path\MauiApp1\MauiApp1\App.xaml.cs 11 Active

  • @shahanahmed9444
    @shahanahmed9444 ปีที่แล้ว

    How do you record audio on a maui app?

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      We’re planning on adding that soon

    • @shahanahmed9444
      @shahanahmed9444 ปีที่แล้ว

      @@jfversluis you mean .net maui does not have that ability yet?

    • @shahanahmed9444
      @shahanahmed9444 ปีที่แล้ว

      And also can this still be implemented using platform specific code in my maui app?

    • @jfversluis
      @jfversluis  ปีที่แล้ว +1

      @@shahanahmed9444 .NET MAUI absolutely does, if I can create a plugin for it, you can implement the code to do it yourself. But there is currently no plugin for you to use.

    • @shahanahmed9444
      @shahanahmed9444 ปีที่แล้ว

      @@jfversluis thats awesome, dude. If I wanted to create my own plugins, for example, for accessing the microphone to record audio, how would i go about doing so? Can you point me in the right direction so that i can also learn how to implement plugins to access native features on android and ios which i cannot do so on maui out of the box?

  • @haroldscott3402
    @haroldscott3402 11 หลายเดือนก่อน

    Error when using builder.Services.AddSingleton(AudioManager.Current) the AudioManager does not exist

    • @jfversluis
      @jfversluis  11 หลายเดือนก่อน

      You’re going to have to add the right using statement or fully qualify the type with Plugin.Maui.Audio.AudioManager

    • @haroldscott3402
      @haroldscott3402 11 หลายเดือนก่อน

      @@jfversluis Thanks, did see that in your code

  • @11ManMulti
    @11ManMulti ปีที่แล้ว +1

    If someone going to use this on another page other than the main page, you should write the navigation method like this:
    await Navigation.PushAsync(new OtherPage(AudioManager.Current));

    • @datisa99
      @datisa99 ปีที่แล้ว

      i delete mainpage. i replace it with bmiview. where should i wrote this code? tnx

    • @11ManMulti
      @11ManMulti 9 หลายเดือนก่อน

      @@datisa99 wherever your going to navigate through pages.

    • @thisisyusuf
      @thisisyusuf หลายเดือนก่อน

      Thank you, you saved my life!