Using Media in Android Studio

Asked

Viewed 45 times

0

Hello, good morning. I have an audio in the raw folder, a few seconds long. I want to use this audio when the application opens. When the app starts, it will already start with the audio playing. Can anyone help me do that? Thanks!

1 answer

1


You need only the class Mediaplayer and use the method start() 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();
}

To stop the audio, just use the method stop(). Below is an example to stop audio when the application is destroyed. See:

public void onDestroy() {

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

}
  • Thank you very much!

  • @Adlemelvis for nothing! =)

  • How do I learn about these classes and methods? Or will I just learn every time I ask someone? for example: if I need to display a pdf, how will I learn to do this? has something somewhere to study, or I’ll have to ask someone?

  • @Adlemelvis in the documentation has a lot of things. But if you wanted something specific, you can ask here.

  • Thank you very much again!

  • I did not find this option. "Validate"

  • 1

    Ready. Thank you very much for your patience.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.