Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement sounds in an android application

Tags:

android

audio

I am developing a game application in android. I have designed all the views and implemented all the functionality. Now in the last screen I have to play sounds in android. Can anybody tell me how to pursue with it?

like image 815
User Avatar asked Feb 01 '11 11:02

User


People also ask

How do I add sounds to my Android?

Open your Android Studio and create a new project. Create a new folder named "raw" in your Android project's "res" folder and place your audio file inside the "raw" folder. You can see the audio is now successfully added into your Android Studio project by viewing the "raw" subfolder under "res" folder.

What class in Android can play audio?

One of the most important components of the media framework is the MediaPlayer class. An object of this class can fetch, decode, and play both audio and video with minimal setup.


2 Answers

MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.AudioFile1);
mp.start();

And then you get all the start/stop/reset/pause/release methods from mp.

like image 53
yosh Avatar answered Oct 20 '22 20:10

yosh


Just place the sound file in /res/raw (after creating the folder) and then use MediaPlayer to init, start and then stop playing the sound. MediaPlayer documentation can be found here.

HTH,
Sriram.

like image 26
Sriram Avatar answered Oct 20 '22 19:10

Sriram