Android-tool for playing music

Source: Internet
Author: User

The music of a general Android app can be divided into background music and sound effects when you click various controls. The former is generally a relatively large music file, and the latter is generally a relatively small music. In Android, mediaplayer is generally used to process relatively large audio files and soundpool is used to process relatively short audio files.

Therefore, a music playing tool is designed as follows: musicplayer
Import android. content. context; <br/> Import android. media. audiomanager; <br/> Import android. media. mediaplayer; <br/> Import android. media. soundpool; </P> <p> Import COM. ruanko. shengji4android. r; <br/> Import COM. ruanko. shengji4android. model. syssetting; </P> <p> public class musicplayer implements mediaplayer. oncompletionlistener, mediaplayer. onerrorlistener {<br/> private context; <br/> private Mediaplayer bgplayer; // player for playing background music <br/> private soundpool actionmusicplayer; // player for playing sound effects <br/> private int source_da, source_givecard, source_start, source_win, source_calllord; // source of various sound effects </P> <p> Public musicplayer (context) {// initialization <br/> This. context = context; <br/> This. actionmusicplayer = new soundpool (10, audiomanager. stream_system, 5); // The maximum number of audio streams in the sound pool is 10, and the sound quality is 5. You can test the effect by yourself. <br/> thi S. source_da = actionmusicplayer. load (context, R. raw. da, 0); <br/> This. source_givecard = actionmusicplayer. load (context, R. raw. givecard, 0); <br/> This. source_start = actionmusicplayer. load (context, R. raw. start, 0); <br/> This. source_win = actionmusicplayer. load (context, R. raw. win, 0); <br/> This. source_calllord = actionmusicplayer. load (context, R. raw. calllord, 0); <br/>}</P> <p> Public void onco Mpletion (mediaplayer MP) {// automatic call upon audio file playback completion </P> <p >}</P> <p> Public Boolean onerror (mediaplayer MP, int what, int extra) {// call automatically when an error occurs during audio file playback <br/> return false; <br/>}</P> <p> Public void playbgsound (INT paramint) {// play background music. paramint is the audio file ID <br/> If (! Syssetting. getinstance (context ). isbgsound () {// syssetting is a singleton class defined by myself and used to save settings related to this application. isbgsound () <br/> return; <br/>}</P> <p> stopsound (bgplayer ); </P> <p> try {<br/> mediaplayer = mediaplayer. create (context, paramint); <br/> bgplayer = mediaplayer; <br/> bgplayer. setoncompletionlistener (this); <br/> bgplayer. setlooping (true); // sets whether to loop. Generally, the background music is set to true <br/> bgplayer. start (); <br/>}catch (illegalstateexception e) {<br/> E. printstacktrace (); <br/>}</P> <p> Public mediaplayer getbackgroundplayer () {<br/> return this. bgplayer; <br/>}</P> <p> Public void stopbgsound () {// stop playing background music <br/> If (bgplayer = NULL) <br/> return; <br/> If (bgplayer. isplaying () <br/> bgplayer. stop (); <br/> bgplayer. release (); <br/> bgplayer = NULL; <br/>}</P> <p> private void playsound (INT source) {// if sound effects are enabled in the system settings, the relevant audio files will be played <br/> If (syssetting. getinstance (context ). isaudio tsound () {<br/> actionmusicplayer. play (source, 1, 1, 0, 0, 1); <br/>}</P> <p> Public void playhitcardsound () {<br/> playsound (source_da); <br/>}</P> <p> Public void playgivecardsound () {<br/> playsound (source_givecard ); <br/>}</P> <p> Public void playstartsound () {<br/> playsound (source_start ); <br/>}</P> <p> Public void playwinsound () {<br/> playsound (source_win ); <br/>}</P> <p> Public void playcalllordsound () {<br/> playsound (source_calllord ); <br/>}< br/>

 

How to use this tool class in activity? We generally think of service when playing background music, but it is difficult for the service to control background music, especially when background music needs to be changed from time to time in different scenarios, you can define an activity, inherit the activity, and define a static member variable in it.

  1. Public StaticMusicplayer;

And initialize the musicplayer in the oncreate method of the activity.

  1. If(Musicplayer =Null){
  2. Syssetting. getinstance (getapplicationcontext (). Load (); // loads system settings. syssetting is a class defined by myself. Because the code is long, it is not given.
  3. Musicplayer =NewMusicplayer (getapplicationcontext ());
  4. Musicplayer. playbgsound (R. Raw. Welcome); // play background music
  5. }

 

 

Then, inherit all the activities defined by the activity.

 

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.