How to play Audio on Button Click in app | Android App Development | Java

 In this post, you will learn how to play audio on button click feature in your Android App.

We will be using the MediaPlayer to implement this functionality.

MediaPlayer class can be used to control playback of audio/video files and streams.

MediaPlayer is not thread-safe. Creation of and all access to player instances should be on the same thread. If registering callbacks, the thread must have a Looper.


Implementation:

First, in the activity_main.xml file, add the following code:

Then, create a new Android Resources Directory of type ' raw ' and add the audio sounds of your choice in it.

Then in the MainActivity.java file, add the following code:



Output:



Comments

Popular posts from this blog

Custom SeekBar in Android Studio .

How to implement simple Pie Chart in app | Android Studio | Java | Android App Development