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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Do you need to play a simple sound in your .NET MAUI application? Well then I've got the solution for you. Check out this video and you'll learn everything. For Xamarin.Forms you have the Simple Audio Player. I think that's what it was called and it did exactly what the name says. It's a very simple audio audio player. It kind of like does the basics for playing a small sound bite, maybe a song. But it doesn't integrate into like iOS and Android audio features or media features. It isn't a fully featured media player inside of your application. There is other plug ins for that. There is a time and place for everything. But this is just for playing with those sound bites. Maybe you're creating a game which has to do pew pew or something like that. Or you want to have a little audio feedback whenever you tap a button or something inside of your application. But it wasn't made available for .NET MAUI yet and the plugin didn't really seem that maintained. And my good friend Shaun and I had a couple of ideas on how to make this plugin, maybe even better. So we took some inspiration from their code and we created the Plugin.Maui.Audio, which is now the new plugin which allows you to very easily play a little sound bite in .NET MAUI. Let's go check out how to get started with it. Inside of Visual Studio 2022. I've created a new .NET MAUI project. And the first thing I did, of course, is install the NuGet package. So here in the Solution Explorer, you can right click on the project and you can say Manage NuGet Packages. Now I've already installed it so you can browse for Plugin. Maui.Audio. Make sure to check the include prerelease checkbox at the time of recording at least because we added a little preview label on it because we just wanted you to test it out first before we go anywhere. So whenever you click that, install it on your project and you're ready to go. You don't need to do any initialization, you don't need any permissions for this to play a sound. So that makes it very easy to use. Now you can use it in kind of like two ways. We've adopted kind of like the Essentials APIs, how they're doing things. So net me show you the most complicated thing and then the easy one is much easier. So let's go to our MauiProgram first. Open the project, MauiProgram.cs and you can of course use this with dependency injection, right? So you can just register this in the dependency injection containers so we can say Builder.Services.Add and probably what you want to do is AddSingleton. Because the way we have this set up is we use an IAudioManager. And with that AudioManager, you can have multiple players, right? So each sound can have its own player. And this. IAudioManager. Well, actually, not the IAudioManager. It has an interface, of course, that you can use, but you want to have the AudioManager concrete type, and you want to say Current. Again, this is kind of like following the same pattern that the essentials APIs are doing. So we have an AudioManager current, and this Audio Manager can create multiple players. This is what will be responsible for all the players inside of your application. So we've got that to be able to inject that inside of our page or our page model, page view model. We also need to register that in our dependency service, dependency injection containers. So Services.AddTransient. And we want to say MainPage. Okay, so now we got that set up, we should be able to inject our AudioManager inside of our MainPage. So let's do that. Go to our MainPage.xaml.cs. And here in the MainPage constructor, I can now say IAudioManager audioManager. Let's give it a name. And then I'm going to create a field here. It doesn't exist yet, but I'm just going to type it. this.audioManager = audioManager. So I can use it from outside of the constructor, right? So that's why I'm doing this. Audio Manager. Okay, let IntelliSense help me here to create a little read only field, audioManager. And boom. I got that audio manager set up. Now with this, I can create those players, right? So what I also already did is add a little sound bite to my project. So here in the Solution Explorer under Resources and Raw, you can just add resources that will be added inside of your application. So, for instance, little movie clips, audio clips, images that you want to use, although there's a better place for that. So everything that's not an image of font, basically whatever you see here. So I put it in that mysound.wav and that's here. So I'm going to use that to actually play this here in the application. Now on the OnCounterCliek, which is in the default template, I'm going to reuse all the code here and I'm going to say var player = audioManager.CreatePlayer, right? So you can create multiple players that you can hang on to. Imagine that you're building a game with spaceships and you're shooting asteroid, something like that. So you have the shooting sound, pew, pew. And you have the asteroid exploding. Don't ask me to create those sounds for you. You can create a player for each of those, right? And you can then just say you will have that player and you can say player play whenever it has to play. So you don't have to create a player each time that you need to play that sound. So that's basically how you can do that. If we inspect what we want to do here, this either wants a stream or a file name. Now, for the file name, it really has to be in an exact position. So probably what you want to do mostly is use the stream here, the audio stream. But that can be very easily done because we have the Essentials APIs to help you get a stream out of that raw file for the resources. So we can just say file system, whoops that has to be a capital... FileSystem, which is the file system helpers. And we can do OpenAppPackageFileAsync with which we can just open a file from the app package. So our application package. And here you can just specify the file name. So I can just say mysound.wav. And this is mostly done. It needs to be async. So I'm going to do a wait here and then I need to make this async, right? So then it all works. Now we have this player which is going to load my sound. And now I can just say player. And we have a bunch of APIs here. We can get the duration from this audio file. We can get the current position. So if it's playing, we can see it, what position it is, can seek. So can we actually take that little slider? There's no slider, but take that virtual slider, put it somewhere else, and then it starts playing from that position. Right? So can we do that? And if we can, then you can use this Seek API to actually set it to a position where you wanted to continue playing from. We can ask like, is it playing? We can do the balance so that you only hear it left or right whenever it's stereo. Of course, the current position, duration, I think I have them all. And we got a couple of methods, of course, for play, for pause, for seek, for stop. And we can set the volume right. So that's basically all the things that we can do here. Well, we want to do the play. I'm only going to show you the basics right now. The rest should speak for itself, how to use it. So I'm just going to do the play. And whenever we do this, I can just run this on Windows. It also supports Android, iOS. You can just run this and your application should play the little sound. So I'm just doing this on Windows right now. That will probably build the fastest. And it's easiest to do with all the audio input right here. So whenever the app is going to come up, we are going to see the little .NET bot who is waving. But now whenever we click the button, it's going to actually play this sound. So whenever I do click here and I'm going to keep my mouth still here for a moment because this is a prerecorded sound. Look at my mouth. I'm not speaking. This is a prerecorded sound. I swear. Just subscribe to my channel whenever you hear the sound. Okay, thank you. It is a prerecorded sound. You can do that. Now this is how easy it is to actually implement it. Now there is a couple of things that are good to know. Mostly that the AudioManager is a IDisposable. So whenever you're done with the AudioManager or whatever you're done with this player, you can say hey, I want to dispose of this, right? I'm done with this, I don't need it anymore. It needs to be disposed because else it will keep hanging on to that stream from the mysound and it can become a memory leak. So it's probably more common that you will dispose of the players whenever you don't need them anymore. And the AudioManager, it's a singleton so that's probably going to live throughout your whole application. And it doesn't matter that much because if you clean up all the players then the AudioManager should be fairly small, right? So whenever you're done with the player, dispose of that and you should be good. Now the other thing that I wanted to show you is how you could easily play a sound without having to do all the dependency injection, right? Typically whenever you have a good architecture building that game or whatever, you want to have it through that dependency injection. If you don't want to do that and just want to play a simple sound we can go back to this AudioManager current, right? So we can go here and you can hear directly say AudioManager.Current.CreatePlayer and all the rest is exactly the same, right? So you're just going to get that player again, don't forget to clean it up. But you can access that instance directly and you can just play a sound that way. So that's basically how easy it is and that is how you can easily play a sound in .NET MAUI. Now, if you check out the plugin repository because it is open source so we accept pull requests, we accept issues, we accept whatever, go check it out. And we have a little sample app in there which does a lot of things. It shows you actually how to show the duration, the progress. With a little timer there you can have sliders for like seeking throughout the audio clip, throughout the balance. You can set it to left and right. We have a little demo for all the features that are in here. We're thinking about ways to making it actually easier to play those sounds. So whenever you want to have that little audio feedback on tapping a button, we're thinking of creating a little XAML extension that will easily allow you to do that. If you have any ideas, please open issues or better yet, open those pull requests because it's a very small project so it should be fairly easy to get started with contributing and if you don't know how, please ask me because I'm always happy to help. Let me know down in the comments what you're going to create with this little audio player. And if you actually like this video, basically put everything that you want to tell me in the comments like this video so that it will spread to more people throughout the wonderful YouTube community and I can grow my little channel right here. Thank you so much for watching and please check out this playlist for more .NET MAUI content. Check out this video for some more recommended content and click this button to see if you're already subscribed. See you for the next.
Info
Channel: Gerald Versluis
Views: 8,773
Rating: undefined out of 5
Keywords: .net maui, dotnet maui tutorial, microsoft maui, dotnet maui, learn dotnet maui, dotnet maui getting started, .net 6, .net 6 maui, learn .net maui, c# maui, .net maui tutorial, .net maui play sound, dotnet maui play sound, dotnet maui audio, .net maui audio, plugin.maui.audio, SimpleAudioPlayer, c# maui example, c# maui getting started, dotnet 6 maui
Id: oIYnEuZ9oew
Channel Id: undefined
Length: 10min 44sec (644 seconds)
Published: Mon Sep 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.