Android uses mediaplayer to play an instance of music in RES / raw folder
Android uses mediaplayer to play an instance of music in RES / raw folder
(1) Create a new folder in the res folder, rename it raw, and put the music to be played in the raw folder
(2) Modify the XML layout file in the layout directory, add three button spaces and a text control to prompt the current playback status, pause and stop. The specific codes are as follows
(3) Open mainactivity and define the required member variables in this class. The specific code is as follows
(4) In the oncreate() method, get the text box that prompts the current status of the play pause / continue stop button, and create a playback object for the mediaplayer object. The specific code is as follows.
(5) Write a play () method with no return value for playing music. In this method, first call the reset () method of the mediaplayer object to reset the mediaplayer object, and then reset the audio file to be played. Finally, call the start () method to start playing the audio.
(6) Add a completion time listener for the mediaplayer object to restart playing music after the music is played
(7) Add a click event listener for the play button
(8) Add a click event listener for the pause button
(9) Add a click event listener for the stop button
(10) Be sure to remember this. Override the ondestroy () method of the activity to stop the audio being played and release the resources occupied by the mediaplayer when the current activity is destroyed. Otherwise, it will be played every time you open it, and the last playback will not stop. You can try it. I can't explain clearly
Thank you for reading, hope to help you, thank you for your support to this site!