Exchange

Play Audio Clip in Simple Media player in Android Studio

First, add the audio file to the "raw" folder in the "res" folder of your project.

In your Java code, initialize a MediaPlayer object and pass the audio file's resource ID to it

To start playing the audio clip, call the 'start()' method on the MediaPlayer object

To stop playing the audio clip, call the 'stop()' method on the MediaPlayer object

Release the resources used by the MediaPlayer object using the 'release()' method when you are finished playing the audio clip

Note that you also need to handle any exceptions that may be thrown while using the MediaPlayer object.

Here is an example code for playing an audio clip in Simple Media player in Android Studio:


private void playBgMusic() {

mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.bg_music);
mediaPlayer.setLooping(true);
mediaPlayer.start();

}

@Override
protected void onResume() {
super.onResume();
mediaPlayer.start();
}

@Override
protected void onPause() {
super.onPause();
mediaPlayer.pause();
}

@Override
protected void onDestroy() {
super.onDestroy();
mediaPlayer.stop();
mediaPlayer.release();
}

 

android studio tutorials
android studio tutorials



Post a Comment

0 Comments