How to play audio in flutter? | Audio File From Asset

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello folks welcome back to another video in this video we are going to make our flutter app to play a sound like this as you can see whenever I tap onto this button or alarm sound is playing so we are going to build the same app in the flutter so I have created this this empty flutter project and now as per the hour Channel culture let's remove this my homepage class and create another state full widget named as play audio now from above replace this my homepage with the play audio and remove the title from here because in our class we are not going to accept any title or something else now come down to here now for playing the sound in flutter we need to use some external third- party Library so for doing that let's open your favorite browser and then here I'm using audio player package for playing the sound in our flutter application so let's copy the package name and come to your flutter app and now open your pubp do yaml file and here place the package name with the the version which you are going to use I'm using version 4.0.0 you can use any version compatible with your Dot and flut version now just click on Pub get so that it will get all the dependencies now come here to the main do dot file and if you want to follow the return tutorial then you can follow my article as you can see I have provided all the codes and all the details how the things going to be work and in this article I have mentioned how to play songs from assets and play sound from URL and also all the other operation like play pause resume seek all the other things so you can check that out I provide the link in the description and in pin comment now come back to the video here as I have told you we are going to use audio player library for playing the sound so we need the instance of audio player class so I'm creating the instance of audio player class and inside this build method we need an elevated button and I'm just creating an elevated button which is going to play the sound now inside this elevated button for onpress method we will call a function and for the child part create a text widget and inside this text widget I'm going to assign a value as please press me you can write it anything you want like play pause or anything now the final part is coming for playing the sound I'm going to create an another method because we don't want our code to be cluttered so I'm creating a new method for playing sound so from onpressed method called a method play sound and now let's create our this play sound method so inside our play sound method we need the path of the sound which we want to play from our assets so just now create here a variable string named as audio path okay now give the address of the path in my project I have created an assets folder in which I have placed an audio folder and inside audio folder I have my sound file named as alarm. MP3 and now come to your pubp speec do yam will file and down from here remove these lines as and make sure you're having correct indentation now below the assets line write the address of your audio so my sound address is assets / audio okay now inside in main. files play sound method just giving the path of my audio and one more thing just remember whenever we are using audio player it assumes that our local sound is going to be in assets folder so we don't need to write Assets in this path variable so here I'm going to give it its path audio sound. MP3 okay after this whenever we want to play sound we will give the path of our sound in the audio player whenever we want to play the sound the sound is the future event so we need to make our function as a future and our this future method is not returning anything so I'm making it void now whenever you tap on this play button it will look into the assets folder and then it will play the sound so here we need to await for these tasks so just type here await and then make use of our player instance so player dot play and then inside it make use of asset source and then assign the path of your sound that's it you just need to do this much of work okay now just save the program and hit the Run button and there is something wrong happened so just let me check okay I found the issue I have given the wrong name of our sound so let me fix this I changed the name and now our issue is fixed so save this and run the app and now again press onto this button now as you can see our sound is playing so that's it for this video if you like the video then please like share and subscribe it will motivate me to create such videos thank you and have a good day
Info
Channel: Widget Wisdom
Views: 1,844
Rating: undefined out of 5
Keywords: flutter course, why flutter, how to play sounds in flutter, How to play audio in flutter, Audio File From Asset, sounds in flutter, package for sounds in flutter, play sounds in ios and android, mobile app development, flutter programming, flutter, flutter tutorial, android, ios, how to play audio in flutter, how to play sound in flutter, audio file from asset, flutter audio, flutter audio player, flutter tutorial for beginners
Id: OHVKzKc5NgI
Channel Id: undefined
Length: 4min 46sec (286 seconds)
Published: Mon Jan 01 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.