Android Learning to play audio

Source: Internet
Author: User

Today I learned how to play audio with the MediaPlayer method in Android, the code below.

Start with a new Android project, place pre-recorded audio music.wma in the Res/raw directory, add a button to the layout file, and the audio will ring when the button is pressed.

1  Public classTestaudioextendsActionbaractivityImplementsOnclicklistener {2 3     PrivateMediaPlayer MP;4 @Override5      Public voidonCreate (Bundle savedinstancestate) {6         Super. OnCreate (savedinstancestate);7 Setcontentview (r.layout.activity_main);8Setvolumecontrolstream (Audiomanager.stream_music);//Indicates that the volume key is pressed to adjust the volume of music or other media, not the volume of the ringtone9 TenButton Btnaudio =(Button) Findviewbyid (r.id.audio); OneBtnaudio.setonclicklistener ( This); A     } -  -      Public voidOnClick (View v) { the         Switch(V.getid ()) { -              CaseR.id.audio: -MP = Mediaplayer.create ( This, r.raw.music); -Mp.start ();//Press the play button and the music rings +                  Break; -         } +}

Android Learning to play audio

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.