Using Media in Android Studio [duplicate]

0

Hello, good morning. I have an audio in the raw folder, with a few seconds of duration. I want to use this audio when the application opens. When the application is initialized, it will start with the audio playing. Does anyone help me do this? Thanks!

    
asked by anonymous 01.05.2017 / 14:24

1 answer

1

You only need the MediaPlayer class and use the start() method to initialize. See:

MediaPlayer mPlayer;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);         
    setContentView(R.layout.main);

    mPlayer = MediaPlayer.create(this, R.raw.mysoundfile);
    mPlayer.start();
}

In order for the audio to stop, use the stop() method. Below is an example of stopping the audio when the application is destroyed. See:

public void onDestroy() {

    mPlayer.stop();
    super.onDestroy();

}
    
01.05.2017 / 14:58