Asynchronous music playback

Source: Internet
Author: User
Tags android games

In android game development, we must consider playing background music. The mediaplayer class can be used to play sound on the Android platform. However, in addition to playing music, we also need to consider the smoothness of pictures, as well as multiple types of sound effects play simultaneously, Android multithreading and asynchronous sound effects must be used for playing at the same time. The android SDK has provided the asyncplayer class since 1.0. Here we can derive or modify more flexible playback classes based on our own needs.

Import android. content. context; import android.net. uri; import android. OS. powermanager; import android. OS. systemclock; import android. util. log; import Java. io. ioexception; import Java. lang. illegalstateexception; import Java. util. optional list; public class asyncplayer {Private Static final int play = 1; Private Static final int stop = 2; Private Static final Boolean mdebug = false; Private Static final clas S command {int code; Context context; Uri URI; Boolean looping; int stream; long requesttime; Public String tostring () {return "{code =" + code + "looping =" + looping + "stream =" + stream + "uri =" + URI + "}";}} private shortlist <command> mcmdqueue = new shortlist (); // use a linked list to save the playback parameter queue private void startsound (command cmd) {try {mediaplayer player = new mediaplayer (); player. setaudiostream Type (CMD. stream); player. setdatasource (CMD. context, cmd. uri); // sets the media source. Here, android123 prompts you to use the public void play (context, Uri, Boolean looping, int stream) of this class) the second parameter URI is the media location. Player. setlooping (CMD. looping); player. Prepare (); player. Start (); If (mplayer! = NULL) {mplayer. release () ;} mplayer = player;} catch (ioexception e) {log. W (mtag, "error loading sound for" + cmd. uri, e);} catch (illegalstateexception e) {log. W (mtag, "illegalstateexception (content provider died ?) "+ Cmd. uri, e) ;}} private final class thread extends Java. lang. thread {// thread () {super ("asyncplayer-" + mtag);} public void run () {While (true) {command cmd = NULL; synchronized (mcmdqueue) {// run cmd = mcmdqueue in synchronous mode. removefirst ();} switch (CMD. code) {case play: startsound (CMD); break; Case stop: If (mplayer! = NULL) {mplayer. stop (); mplayer. release (); mplayer = NULL;} else {log. W (mtag, "Stop command without a player");} break;} synchronized (mcmdqueue) {If (mcmdqueue. size () = 0) {mthread = NULL; releasewakelock (); Return ;}}} private string mtag; private thread mthread; private mediaplayer mplayer; private powermanager. wakelock mwakelock; private int mstate = stop; Public asyncplayer (stri Ng tag) {If (tag! = NULL) {mtag = tag;} else {mtag = "asyncplayer" ;}} public void play (context, Uri, Boolean looping, int Stream) {command cmd = new command (); cmd. requesttime = systemclock. uptimemillis (); // in order to test the performance, the system tickcount timer value CMD is passed before execution. code = play; cmd. context = context; cmd. uri = URI; cmd. looping = looping; cmd. stream = stream; synchronized (mcmdqueue) {enqueuelocked (CMD); mstate = play ;}} Public void stop () {synchronized (mcmdqueue) {If (mstate! = Stop) {command cmd = new command (); cmd. requesttime = systemclock. uptimemillis (); cmd. code = stop; enqueuelocked (CMD); mstate = stop ;}} private void enqueuelocked (command cmd) {mcmdqueue. add (CMD); If (mthread = NULL) {acquirewakelock (); mthread = new thread (); mthread. start () ;}} generally, the following code does not need to be considered for Android games. Generally, users are interacting with each other and there is no screen lock issue. Public void setuseswakelock (context) {// power management wakelock Processing If (mwakelock! = NULL | mthread! = NULL) {Throw new runtimeexception ("assertion failed mwakelock =" + mwakelock + "mthread =" + mthread);} powermanager PM = (powermanager) context. getsystemservice (context. power_service); mwakelock = PM. newwakelock (powermanager. partial_wake_lock, mtag);} private void acquirewakelock () {// lock if (mwakelock! = NULL) {mwakelock. Acquire () ;}} private void releasewakelock () {// unlock if (mwakelock! = NULL) {mwakelock. Release ();}}}

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.