Add music for Android

Source: Internet
Author: User
Package COM. llkan. setup; import Java. util. hashmap; import Java. util. map; import Java. util. random; import COM. llkan. r; import android. content. context; import android. media. audiomanager; import android. media. mediaplayer; import android. media. soundpool;/*** sound control class * @ author WYF **/public class soundplayer {Private Static mediaplayer music; Private Static soundpool; Private Static Boolean musicst = True; // music switch Private Static Boolean soundst = true; // sound switch Private Static Context context; Private Static final int [] musicid = {R. raw. BG, R. raw. bg1, R. raw. bg2, R. raw. bg3}; Private Static Map <integer, integer> soundmap; // ing relationship between the Audio Resource ID and the loaded audio source ID/*** Initialization Method * @ Param C */public static void Init (context c) {context = C; initmusic (); initsound () ;}// initialize the audio player Private Static void initsound () {soundpool = New soundpool (10, audiomanager. stream_music, 100); soundmap = new hashmap <integer, integer> (); soundmap. put (R. raw. itemboom, soundpool. load (context, R. raw. itemboom, 1); soundmap. put (R. raw. sel, soundpool. load (context, R. raw. sel, 1);} // initialize the music player Private Static void initmusic () {int r = new random (). nextint (musicid. length); music = mediaplayer. create (context, musicid [R]); music. setlooping (true );} /*** Play sound ** @ Param resid sound resource ID */public static void playsound (INT resid) {If (soundst = false) return; integer soundid = soundmap. get (resid); If (soundid! = NULL) soundpool. play (soundid, 1, 1, 1, 0, 1);}/*** pause music */public static void pausemusic () {If (music. isplaying () music. pause ();}/*** play music */public static void startmusic () {If (musicst) music. start ();}/*** switch a music and play it */public static void changeandplaymusic () {If (music! = NULL) music. release (); initmusic (); startmusic ();}/*** obtain the music switch status * @ return */public static Boolean ismusicst () {return musicst ;} /*** set the music switch ** @ Param musicst */public static void setmusicst (Boolean musicst) {soundplayer. musicst = musicst; If (musicst) music. start (); else music. stop ();}/*** obtain the sound switch status * @ return */public static Boolean issoundst () {return soundst ;} /*** set the sound switch ** @ Param soundst */public static void setsoundst (Boolean soundst) {soundplayer. soundst = soundst;}/*** sounds 'status' */public static void boom () {playsound (R. raw. itemboom );}}
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.