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
- android示例之音乐播放器第五天
- android示例之音乐播放器第一天
- android示例之音乐播放器第一天补充
- android示例之音乐播放器第三天
- android示例之音乐播放器第二天
- android示例之音乐播放器第四天
- android示例之音乐播放器第六天
- android 之 service+contentProvider 音乐播放示例
- android播放音乐示例
- Android:音乐播放示例
- android播放音乐示例
- android之音乐播放器
- Android之音乐播放器
- 第三十五天 模拟音乐播放器MediaPlayer
- android示例之音乐播放器第七天(实现第六天吹的牛之得到SD卡的所有音乐文件)
- Android 项目之--Mini音乐播放器
- android音乐播放器之----天天动听
- android 应用开发之音乐播放器
- 每天一点小记录
- 事件注册与删除
- cocos 2d android-1 精灵基本操作
- 机器学习算法与Python实践之(三)支持向量机(SVM)进阶
- HDU1269
- android示例之音乐播放器第五天
- Dungeon Master uva BFS
- HDU3594、、、、、、、**************
- 机器学习算法与Python实践之(四)支持向量机(SVM)实现(源码)
- JAVASCRIPT作用域链,词法分析
- hdu1028
- 倒计时 css实现
- mysql数据库备份及恢复命令mysqldump,source的用法
- 多线程下变量-原子操作 __sync_fetch_and_add等等