Use audiomanager to control small-volume mobile audio exercises

Source: Internet
Author: User

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 ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.