This should be regarded as a small module of the music player. I will not talk about it. It is very simple. It is purely for practice and the code is clearly written.
The layout file is as follows:
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent"> <button Android: id = "@ + ID/button1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "play songs"/> <button Android: id = "@ + ID/button2" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "increase volume"/> <button Android: id = "@ + ID/button3" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = ""/> <togglebutton Android: id = "@ + ID/togglebutton" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: textoff = "mute" Android: texton = "Do not mute"/> </linearlayout>
The Java code is as follows:
Package COM. mars. demoserviceaudio; import android. app. activity; import android. app. service; import android. media. audiomanager; import android. media. mediaplayer; import android. OS. bundle; import android. view. menu; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. compoundbutton; import android. widget. compoundbutton. oncheckedchangelistener; import android. widget. togglebutton; public class mainactivity extends activity {private button up; private button down; private button play; private togglebutton isquit; private audiomanager; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); audiomanager = (audiomanager) getsystemservice (service. audio_service); play = (button) findviewbyid (R. id. button1); up = (button) findviewbyid (R. id. button2); down = (button) findviewbyid (R. id. button3); isquit = (togglebutton) findviewbyid (R. id. togglebutton); play. setonclicklistener (New onclicklistener () {public void onclick (view v) {mediaplayer media = mediaplayer. create (mainactivity. this, R. raw. AAA); media. setlooping (true); // loop media. start () ;}}); up. setonclicklistener (New onclicklistener () {public void onclick (view v) {// adjust the audio volume to increase audiomanager. adjuststreamvolume (audiomanager. stream_music, audiomanager. adjust_raise, audiomanager. flag_show_ui) ;}}); down. setonclicklistener (New onclicklistener () {public void onclick (view v) {// adjust the audio volume to audiomanager. adjuststreamvolume (audiomanager. stream_music, audiomanager. adjust_lower, audiomanager. flag_show_ui) ;}}); isquit. setoncheckedchangelistener (New oncheckedchangelistener () {public void oncheckedchanged (compoundbutton buttonview, Boolean ischecked) {// determine whether to mute audiomanager Based on ischecked. setstreammute (audiomanager. stream_music, ischecked) ;}}) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. activity_main, menu); Return true ;}}