Android lyrics show design ideas (3) General music play service (I)

Source: Internet
Author: User

MediaPlayerService is a common music playing Service. It has the following functions:
Controls the playing, stopping, and pause of music, and switches between front and back songs.
Audio Focus processing (corresponding application switchover ).
Intent processing (corresponding to the multimedia key, pulling out the headset line, and making a call)
Notification Processing
In fact, this class was originally associated with LyricPlayerService, but the Code became increasingly messy with the increase of functions. So we had the idea of separation. However, the classification should also be separated, so the function defined for this category is to implement a general media playing service.
The separated class relationships can be seen from the graph below.

The functions are described below.
Controls the playing, stopping, and pause of music, and switches between front and back songs.
One problem to be solved before starting a real job is that, as a general music playing service, there will certainly be a requirement for playing a song before/after playing it in the background. However, this is because the application does not exist. It is impossible to submit this task to Activie for processing. To solve this problem, a MediaInfoProvider class is defined for obtaining the corresponding media file information.
Public interface MediaInfoProvider {
String getUrl ();
String getTitle ();
Boolean moveToNext ();
Boolean moveToPrev ();
}
You can call the moveToNext and moveToPrev methods to switch between media files. Then, you can call getUrl and getTitle to obtain the file information.
Note that only the interface is specified here. The real instance must be provided by the caller through the following method. One additional task is to stop playing a song if it is different from playing it.
Public void setMediaInfoProvider (MediaInfoProvider provider ){
If (mDataSource! = Provider. getUrl () & (isStop () = false )){
Stop ();
}
MMediaInfoProvider = provider;
MDataSource = provider. getUrl ();
MTitle = provider. getTitle ();
}
Of course, before using MediaPlayer, we need to prepare for the onCreate method. Content includes
Create a MediaPlayer object,
Set the wake-up mode (to prevent the CPU from entering sleep state during playback, remember to set the permission to android. permission. WAKE_LOCK in AndroidManifest. xml)
Specifies the listener after playing.
MMediaPlayer = new MediaPlayer ();
MMediaPlayer. setWakeMode (getApplicationContext (), PowerManager. PARTIAL_WAKE_LOCK );
MMediaPlayer. setOnCompletionListener (this );
 
The listener has been played for two purposes. One is to stop the icon display on the taskbar, and the other is to automatically disable the service when there is no foreground application. What if no one cares? Right.
@ Override
Public void onCompletion (MediaPlayer mp ){
MIsPausing = false;
MNotificationManager. cancel (NOTIFICATION );
If (mBinded = false ){
StopSelf ();
}
}
Then, complete the final work in onDestroy. The stopped part of the video seems to be okay, but it is better to do everything step by step.
MMediaPlayer. setOnCompletionListener (null );
If (! IsStop ()){
Stop ();
}
MMediaPlayer. release ();
MMediaPlayer = null;
Finally, the real control part is reached. The subsequent program is relatively long, but the content is indeed relatively simple.
The start method at the beginning is complex, mainly because although it is playing, the resume from pause is different from the process for playing a new song. Nothing else can be said.
Public void start (){
TryToGetAudioFocus ();
If (! IsPausing ()){
If (isPlaying ()){
Stop ();
}
Try {
MMediaPlayer. reset ();
MMediaPlayer. setDataSource (mDataSource );
MMediaPlayer. prepare ();
} Catch (IOException e ){
Return;
} Catch (IllegalArgumentException e ){
Return;
}
MMediaPlayer. start ();
} Else {
MIsPausing = false;
MMediaPlayer. start ();
}
ShowNotification ();
}

Public void stop (){
GiveUpAudioFocus ();
MMediaPlayer. stop ();
MIsPausing = false;
MNotificationManager. cancel (NOTIFICATION );
}

Public void pause (){
GiveUpAudioFocus ();
MIsPausing = true;
MMediaPlayer. pause ();
}

Public void playNext (){
If (mMediaInfoProvider. moveToNext ()){
If (isPlaying () | isPausing ()){
Stop ();
}
MDataSource = mMediaInfoProvider. getUrl ();
MTitle = mMediaInfoProvider. getTitle ();
Start ();
}
}

Public void playPrev (){
If (mMediaInfoProvider. moveToPrev ()){
If (isPlaying () | isPausing ()){
Stop ();
}
MDataSource = mMediaInfoProvider. getUrl ();
MTitle = mMediaInfoProvider. getTitle ();
Start ();
}
}

Public boolean isPausing (){
Return mIsPausing;
}

Public boolean isPlaying (){
Return mMediaPlayer. isPlaying ();
}

Public boolean isStop (){
Return (isPlaying () = false & isPausing () = false );
}

Public int getDuration (){
Return mMediaPlayer. getDuration ();
}

Public int getPosition (){
Return mMediaPlayer. getCurrentPosition ();
}

Public long seek (long whereto ){
MMediaPlayer. seekTo (int) whereto );
Return whereto;
}
The function of the following code is to process Intent related to playback control. Media keys (which are actually the buttons on the Line Control) are implemented in this way when the headset is pulled out and calls are paused. Of course, they all need to be defined in AndroidManifest. xml.
Public static final String ACTION_PAUSE = "MediaPlayer. xwg. action. PAUSE ";
Public static final String ACTION_PLAY_PAUSE = "MediaPlayer. xwg. action. PLAY_PAUSE ";
Public static final String ACTION_PREVIOUS = "MediaPlayer. xwg. action. PREVIOUS ";
Public static final String ACTION_NEXT = "MediaPlayer. xwg. action. NEXT ";
@ Override
Public int onStartCommand (Intent intent, int flags, int startId ){
String action = intent. getAction ();
If (action! = Null)
{
If (action. equals (ACTION_PLAY_PAUSE )){
If (isPlaying ()){
Pause ();
} Else {
Start ();
}
} Else if (action. equals (ACTION_PAUSE )){
If (isPlaying ())
{
Pause ();
}
} Else if (action. equals (ACTION_PREVIOUS )){
If (isPlaying () | isPausing ())
{
PlayPrev ();
}
} Else if (action. equals (ACTION_NEXT )){
If (isPlaying () | isPausing ())
{
PlayNext ();
}
}
}
Return START_NOT_STICKY; // Means we started the service, but don't want it
// Restart in case it's killed.
}
In fact, I did not mention a few points in the previous code, that is, the AudioFocus and MEDIA_BUTTOn, plug-out of the headset, incoming call processing, and Notification. For more information, see
Android lyrics show design ideas (4) General music play service (II)
Software Function Description: Original: Android Application Development-Andorid lyrics show, with source code
Project, source code download: Android lyrics show source code, project file 2011/9/11
 
 
Author: "From Dalian"

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.