android示例之音乐播放器第五天

来源:互联网 发布:问答 是哪个软件 编辑:程序博客网 时间:2024/05/17 08:55

老子到底要不要辞职呢?干得实在是没意思。如果辞职了,就凭我现在这本事也找不到好的编程工作啊,难道要租间小房子继续自学吗?其实这样也蛮不错的啊。。。

今天要完成的功能很简单,就是把之前播放音乐的功能从Activity转移到一个Service中,原因很简单,Service的优先级比较高,让Activity传递给Service消息,Service根据消息执行响应的功能。

public class PlayerActivity extends Activity{private ImageButton start;private ImageButton pause;private ImageButton stop;//播放的对象private Mp3Info mp3Info=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.player);//得到传递过来的IntentIntent intent=getIntent();//得到点击的对象mp3Info=(Mp3Info) intent.getSerializableExtra("mp3Info");start=(ImageButton)findViewById(R.id.start);pause=(ImageButton)findViewById(R.id.pause);stop=(ImageButton)findViewById(R.id.stop);start.setOnClickListener(new StartListener());pause.setOnClickListener(new PauseListener());stop.setOnClickListener(new StopListener());}/** * 点击开始按钮的监听类 * @author lyc */class StartListener implements OnClickListener{public void onClick(View v) {//把播放的文件对象和消息传递给ServiceIntent intent=new Intent();intent.putExtra("mp3Info", mp3Info);intent.putExtra("MSG", Content.MSG_START);intent.setClass(PlayerActivity.this, PlayService.class);startService(intent);}}/** *点击暂停按钮的监听类  * @author lyc */class PauseListener implements OnClickListener{public void onClick(View v) {//把暂停播放的消息传递给ServiceIntent intent=new Intent();intent.putExtra("MSG", Content.MSG_PAUSE);intent.setClass(PlayerActivity.this, PlayService.class);startService(intent);}}/** * 点击停止按钮的监听类 * @author lyc */class StopListener implements OnClickListener{public void onClick(View v) {//把停止播放的消息传递给ServiceIntent intent=new Intent();intent.putExtra("MSG", Content.MSG_STOP);intent.setClass(PlayerActivity.this, PlayService.class);startService(intent);}}}

在Service中首先接收传递过来的消息,根据消息执行响应的功能,后面的歌词同步更新的实现就是在Service中把歌词信息广播出去,在Activity中接收广播的歌词信息,然后在Activity中更新歌词。

这里依然涉及到了播放状态的判断,要把功能实现的完整,就要把各种情况都考虑到了。

/** * 播放音乐的Service * @author lyc */public class PlayService extends Service{//播放媒体的对象private MediaPlayer mediaPlayer;//设置文件播放的状态private boolean isStart=false;private boolean isStop=false;//正在播放的文件对象private Mp3Info mp3Info=null;@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {//得到传递过过来的消息int MSG=intent.getIntExtra("MSG", 0);if(MSG==Content.MSG_START){mp3Info=(Mp3Info) intent.getSerializableExtra("mp3Info");//播放音乐play(mp3Info);}else if(MSG==Content.MSG_PAUSE){//暂停播放音乐pause();}else if(MSG==Content.MSG_STOP){//停止播放音乐stop();}return super.onStartCommand(intent, flags, startId);}/** * 播放音乐 * @param mp3Info */public void play(Mp3Info mp3Info){String path=getFilePath(mp3Info);mediaPlayer=MediaPlayer.create(PlayService.this, Uri.parse("file://"+path));mediaPlayer.setLooping(false);mediaPlayer.start();isStart=true;isStop=false;}/** * 暂停播放音乐 */public void pause(){if(mp3Info!=null){if(isStart==true){mediaPlayer.pause();isStart=false;isStop=false;}else{mediaPlayer.start();isStart=true;isStop=false;}}}/** * 停止播放音乐 */public void stop(){if(mp3Info!=null){if(isStop==false){mediaPlayer.stop();mediaPlayer.release();isStart=false;isStop=true;}}}/** * 通过文件对象得到文件所在SD卡的路径 * @param mp3Info * @return 文件所在SD卡的路径 */public String getFilePath(Mp3Info mp3Info){String SDPath=Environment.getExternalStorageDirectory().getAbsolutePath();String path=SDPath+File.separator+"mp3"+File.separator+mp3Info.getMp3Name();return path;}}

最后不要忘记了注册Service。

0 0
原创粉丝点击