Android audio playback

Source: Internet
Author: User
Package COM. example. test2; import android. media. audiomanager; import android. media. mediaplayer; import android. OS. bundle; import android. app. activity; import android. app. service; import android. view. menu; import android. view. view; import android. widget. button; import android. widget. compoundbutton; import android. widget. compoundbutton. oncheckedchangelistener; import android. widget. togglebutton; public class Mai Nactivity extends activity {private button btnplay = NULL, btnupper = NULL, btnlower = NULL; private togglebutton tbmute = NULL; private mediaplayer = NULL; // audio frequency private audiomanager = NULL; // audio @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); audiomanager = (audiomanager) getsystemservice (service. au Dio_service); btnplay = (button) findviewbyid (R. id. btnplay); btnupper = (button) findviewbyid (R. id. btnupper); btnlower = (button) findviewbyid (R. id. btnlower); btnplay. setonclicklistener (listener); btnupper. setonclicklistener (listener); btnlower. setonclicklistener (listener); tbmute = (togglebutton) findviewbyid (R. id. tbmute); tbmute. setoncheckedchangelistener (New oncheckedchangelistener () {public void onchecked Changed (compoundbutton buttonview, Boolean ischecked) {audiomanager. setstreammute (audiomanager. stream_music ,! Ischecked); // set whether to mute});} view. onclicklistener listener = new view. onclicklistener () {public void onclick (view v) {@ suppresswarnings ("UNUSED") button BTN = (button) V; Switch (v. GETID () {case R. id. btnplay: // play music mediaplayer = mediaplayer. create (mainactivity. this, R. raw. music); mediaplayer. setlooping (true); // sets mediaplayer for loop playback. start (); // playback sound break; case R. id. btnupper: // increase the volume // adjuststreamvolume: adjust the volume of the specified audio type audiomanager. adjuststreamvolume (audiomanager. stream_music, audiomanager. adjust_raise, audiomanager. flag_show_ui); // raise the voice break; case R. id. btnlower: // reduce the volume // first parameter: Sound type // second parameter: adjust the volume direction // third parameter: Optional flag audiomanager. adjuststreamvolume (audiomanager. stream_music, audiomanager. adjust_lower, audiomanager. flag_show_ui); // lowering the voice break ;}}};}
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <button Android: id = "@ + ID/btnplay" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "Play Music"/> <linearlayout Android: orientation = "horizontal" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_gravity = "center_horizontal"> <togglebutton Android: id = "@ + ID/tbmute" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: texton = "mute" Android: textoff = "normal" Android: checked = "true" Android: layout_gravity = "center_vertical"/> <button Android: Id = "@ + ID/btnupper" Android: text = "increase volume" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <button Android: Id = "@ + ID/btnlower" Android: text = "" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> </linearlayout>

 

Related Article

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.