Music Playback Demo Knowledge Points Summary

Source: Internet
Author: User

Package org.crazyit.broadcast;


Import android.app.Activity;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.ImageButton;
Import Android.widget.TextView;


/**
* Description:
* <br/>site: <a href= "http://www.crazyit.org" >crazyit.org</a>
* <br/>copyright (C), 2001-2012, Yeeku.h.lee
* <br/>this program was protected by copyright laws.
* <br/>program Name:
* <br/>date:
* @author Yeeku.h.lee [email protected]
* @version 1.0
*/
public class MusicBox extends Activity
Implements Onclicklistener
{
Gets the title of the song, the author text box displayed in the interface
TextView title, author;
Play/Pause, stop button
ImageButton play, stop;
Activityreceiver Activityreceiver;
public static final String ctl_action
= "Org.crazyit.action.CTL_ACTION";
public static final String update_action
= "Org.crazyit.action.UPDATE_ACTION";
Defines the playback status of the music, the 0X11 representative is not playing, the 0x12 representative is playing; the 0x13 represents a pause
int status = 0x11;
string[] Titlestrs = new string[]{
"Wish",
"Convention",
"Beautiful New World"
};
string[] Authorstrs = new string[]{
"Unknown Artist",
"Zhou Yi",
"Wu Bai"
};
@Override
public void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Get two buttons in the Program Interface interface
Play = (ImageButton) This.findviewbyid (R.id.play);
Stop = (ImageButton) This.findviewbyid (r.id.stop);
title = (TextView) Findviewbyid (r.id.title);
Author = (TextView) Findviewbyid (R.id.author);
Add listeners for click events of two buttons
Play.setonclicklistener (this);
Stop.setonclicklistener (this);
Activityreceiver = new Activityreceiver ();
Create Intentfilter
Intentfilter filter = new Intentfilter ();
Specifies the action that Broadcastreceiver listens on
Filter.addaction (update_action);
Register Broadcastreceiver
Registerreceiver (activityreceiver, filter);
Intent Intent = new Intent (this, musicservice.class);
Start a background service
StartService (Intent);
}
Custom Broadcastreceiver, responsible for listening to broadcasts coming back from the service
public class Activityreceiver extends Broadcastreceiver
{
@Override
public void OnReceive (context context, Intent Intent)
{
Get update message in intent, update represents playback status
int update = Intent.getintextra ("Update",-1);
Gets the current message in intent, which represents the song that is currently playing
int current = Intent.getintextra ("Current",-1);
if (current >= 0)
{
Title.settext (Titlestrs[current]);
Author.settext (Authorstrs[current]);
}
Switch (update)
{
Case 0X11:
Play.setimageresource (R.drawable.play);
status = 0x11;
Break
Control system enters playback state
Case 0x12:
Set use pause icon in playback state
Play.setimageresource (R.drawable.pause);
Set the current state
status = 0x12;
Break
Control system enters suspend state
Case 0X13:
Pause state Settings Use the play icon
Play.setimageresource (R.drawable.play);
Set the current state
status = 0x13;
Break
}
}
}
@Override
public void OnClick (View source)
{
Create Intent
Intent Intent = new Intent ("Org.crazyit.action.CTL_ACTION");
Switch (Source.getid ())
{
Press the Play/pause button
Case R.id.play:
Intent.putextra ("Control", 1);
Break
Press the Stop button
Case R.id.stop:
Intent.putextra ("Control", 2);
Break
}
Send a broadcast that will be received by Broadcastreceiver in the service component
Sendbroadcast (Intent);
}

}





/**
*
*/
Package org.crazyit.broadcast;


Import java.io.IOException;


Import Android.app.Service;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import Android.content.res.AssetFileDescriptor;
Import Android.content.res.AssetManager;
Import Android.media.MediaPlayer;
Import Android.media.MediaPlayer.OnCompletionListener;
Import Android.os.IBinder;


/**
* Description:
* <br/> website: <a href= "http://www.crazyit.org" > Crazy Java Federation </a>
* <br/>copyright (C), 2001-2012, Yeeku.h.lee
* <br/>this program was protected by copyright laws.
* <br/>program Name:
* <br/>date:
* @author Yeeku.h.lee [email protected]
* @version 1.0
*/
public class Musicservice extends Service
{
Myreceiver Servicereceiver;
Assetmanager am;
string[] musics = new string[]{
"Wish.mp3",
"Promise.mp3",
"Beautiful.mp3"
};
MediaPlayer MPlayer;
Current status, 0x11 represents not playing; 0x12 representative is playing; 0x13 Delegate paused
int status = 0x11;
Record music that is currently playing
int current = 0;
@Override
Public IBinder Onbind (Intent Intent)
{
return null;
}
@Override
public void OnCreate ()
{
am = Getassets ();
Create Broadcastreceiver
Servicereceiver = new Myreceiver ();
Create Intentfilter
Intentfilter filter = new Intentfilter ();
Filter.addaction (musicbox.ctl_action);
Registerreceiver (servicereceiver, filter);
Create MediaPlayer
MPlayer = new MediaPlayer ();
Complete event binding listener for MediaPlayer playback
Mplayer.setoncompletionlistener (New Oncompletionlistener ()
{
@Override
public void Oncompletion (MediaPlayer MP)
{
current++;
if (current >= 3)
{
current = 0;
}
/* Send broadcast notification activity change text box */
Intent sendintent = new Intent (musicbox.update_action);
Sendintent.putextra ("current", current);
Send a broadcast that will be received by the broadcastreceiver in the activity component
Sendbroadcast (sendintent);
Prepare, and play music
Prepareandplay (Musics[current]);
}
});
Super.oncreate ();
}
public class Myreceiver extends Broadcastreceiver
{
@Override
public void OnReceive (final context context, Intent Intent)
{
int control = Intent.getintextra ("Control",-1);
Switch (Control)
{
Play or pause
Case 1:
Originally in no Play state
if (status = = 0x11)
{
Prepare, and play music
Prepareandplay (Musics[current]);
status = 0x12;
}
Originally in playback state
else if (status = = 0x12)
{
Time out
Mplayer.pause ();
Change to paused state
status = 0x13;
}
The original is in a paused state
else if (status = = 0x13)
{
Play
Mplayer.start ();
Change state
status = 0x12;
}
Break
Stop Sound
Case 2:
If the original is playing or pausing
if (status = = 0x12 | | status = = 0x13)
{
Stop playing
Mplayer.stop ();
status = 0x11;
}
}
/* Send broadcast notification activity change icon, text box */
Intent sendintent = new Intent (musicbox.update_action);
Sendintent.putextra ("Update", status);
Sendintent.putextra ("current", current);
Send a broadcast that will be received by the broadcastreceiver in the activity component
Sendbroadcast (sendintent);
}
}
private void Prepareandplay (String music)
{
Try
{
Open the specified music file
Assetfiledescriptor afd = AM.OPENFD (music);
Mplayer.reset ();
Loads the specified sound file using MediaPlayer.
Mplayer.setdatasource (Afd.getfiledescriptor ()
, Afd.getstartoffset ()
, Afd.getlength ());
Preparing the Sound
Mplayer.prepare ();
Play
Mplayer.start ();
}
catch (IOException E)
{
E.printstacktrace ();
}
}
}



1: Dynamic registration of broadcasts

Activityreceiver = new Activityreceiver ();
Create Intentfilter
Intentfilter filter = new Intentfilter ();
Specifies the action that Broadcastreceiver listens on
Filter.addaction (update_action);
Register Broadcastreceiver
Registerreceiver (activityreceiver, filter);

2: Service startup:

Intent Intent = new Intent (this, musicservice.class);
Start a background service
StartService (Intent);


3: The OnCreate method of the service initializes the data needed for music playback

public void OnCreate ()
{
am = Getassets ();
Create Broadcastreceiver
Servicereceiver = new Myreceiver ();
Create Intentfilter
Intentfilter filter = new Intentfilter ();
Filter.addaction (musicbox.ctl_action);
Registerreceiver (servicereceiver, filter);
Create MediaPlayer
MPlayer = new MediaPlayer ();
Complete event binding listener for MediaPlayer playback
Mplayer.setoncompletionlistener (New Oncompletionlistener ()//media Listening after the music is played
{//register A callback to being invoked when the end of a media source has been reached during playback
@Override
public void Oncompletion (MediaPlayer MP)
{
current++;
if (current >= 3)
{
current = 0;
}
/* Send broadcast notification activity change text box */
Intent sendintent = new Intent (musicbox.update_action);
Sendintent.putextra ("current", current);
Send a broadcast that will be received by the broadcastreceiver in the activity component
Sendbroadcast (sendintent);
Prepare, and play music
Prepareandplay (Musics[current]);
}
});
Super.oncreate ();
}




Service general processing Such logic is something that our users can't see. We can call him everywhere, like it's global.

Broadcasting is dealing with something like this. There's a OnReceive method in the broadcast, so I can only call him a method, but this method has a feature that can be called anywhere, as long as we send the broadcast, He is not only available in all areas of our application and other applications can also invoke this broadcast when our application does not start, broadcast can help us save memory space, and help us to deal with the problem of calling between applications broadcast is global not only in the application

Music Playback Demo Knowledge Points Summary

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.