Relatively perfect background service for playing music functions

Source: Internet
Author: User

For service lifecycles started with Context.startservice () OnCreate ()-onstartcommand ()-ondestroy (); If you start the service multiple times with Context.startservice, only the Onstartcommand () method is executed more than once. Depending on the characteristics of the music player, using the Context.startservice () call is appropriate.

public class Mymusicplayer extends Service implements

Mediaplayer.oncompletionlistener {
public static final String TAG = "Mymusicplayer";
public static final String action_add_to_queue = "Com.example.musicmedia.ADD_TO_QUEUE";//Specify ACTION
Private concurrentlinkedqueue<uri> mtrackqueue;//It is a thread-free secure queue based on a link node. The elements of the queue follow the principle of FIFO. The head is the first to join, the tail is recently joined. The insertion element is appended to the tail. Extracting an element is extracted from scratch.
Private MediaPlayer Mmediaplayer;


Public IBinder Onbind (Intent Intent) {
return null;
}


@Override
public void OnCreate () {
Super.oncreate ();
LOG.I (TAG, "onCreate");
Mtrackqueue = new concurrentlinkedqueue<uri> ();//Initialize concurrent queue
}


@Override
public int Onstartcommand (Intent Intent, int flags, int startid) {
String action = Intent.getaction ();
if (Action_add_to_queue.equals (ACTION)) {
Uri Trackuri = Intent.getdata ();//Resources to get music
LOG.I ("Mymusicplayer", trackuri.tostring ());
This.addtracktoqueue (Trackuri);//Add resources
}
Return start_not_sticky;//Returns a value that determines that the service does not restart after the system shuts it down and performs a one-time operation often with this.
}


@Override
public void OnDestroy () {
Super.ondestroy ();
LOG.I (TAG, "OnDestroy");
if (Mmediaplayer! = null) {//Release Mmediaplayer resource when service is destroyed
Mmediaplayer.release ();
Mmediaplayer = null;
}
}

If you have already started playing, add a new music resource to the end of the queue, or create a MediaPlayer and start playing
private void Addtracktoqueue (Uri trackuri) {
if (Mmediaplayer = = null) {
try {
LOG.I ("Mymusicplayer", "Addtrackqueue:" + Trackuri);
Mmediaplayer = Mediaplayer.create (this, trackuri);//Initialize Mmediaplayer because it is initialized with mediaplayer.create so it is not necessary to call the prepare () method for preparation. This initialization is done automatically
Mmediaplayer.setoncompletionlistener (this);//registers a callback function that is called after the audio playback is complete
Mmediaplayer.start ();//Start playback
} catch (Exception e) {
Stopself ();//Stop service
}
} else {
Mtrackqueue.offer (Trackuri);//Add elements to the queue
}
}

When the track is finished, start playing the next song or stop the service
@Override
public void Oncompletion (MediaPlayer MediaPlayer) {
Mediaplayer.reset ();//Reusing MediaPlayer Objects
Uri Nexttrackuri = Mtrackqueue.poll ();//remove element from queue
if (Nexttrackuri! = null) {
try {
Mmediaplayer.setdatasource (this, nexttrackuri);//Initialize Mmediaplayer
Mmediaplayer.prepare ();//Preparation
Mmediaplayer.start ();//Play
} catch (IOException e) {
Stopself ();
}
} else {
Stopself ();//Stop service
}
}
}

Relatively perfect background service for playing music functions

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.