使用服务实现简单的音乐播放
2015-10-14 20:16
666 查看
今天下午我们还使用服务实现了一个简单的音乐播放器。
步骤:
1、在res文件夹下创建raw文件夹,把我们要播放的音频文件放在raw文件夹下。
2、下面就是创建服务对象的子类了。
MainActivity:
步骤:
1、在res文件夹下创建raw文件夹,把我们要播放的音频文件放在raw文件夹下。
2、下面就是创建服务对象的子类了。
MainActivity:
package com.example.text03; import com.example.text03.MyService.MyBinder; import android.os.Bundle; import android.os.IBinder; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { private MyServiceConnection conn; private MyService myService; private Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intent = new Intent(this, MyService.class); conn = new MyServiceConnection(); } public void play(View view){ myService.play(); } public void continuePlay(View view){ myService.continuePlay(); } public void pause(View view){ myService.pause(); } public void bind(View view){ bindService(intent, conn, BIND_AUTO_CREATE); } public void unbind(View view){ unbindService(conn); } class MyServiceConnection implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) { MyBinder binder = (MyBinder) service; myService = binder.getMyService(); } @Override public void onServiceDisconnected(ComponentName name) { } } }继承自Service的字了MyService
package com.example.text03; import java.io.IOException; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Binder; import android.os.IBinder; public class MyService extends Service { //声明一个媒体播放器 private MediaPlayer player; //记录播放的位置 private int position; @Override public void onCreate() { super.onCreate(); //初始化媒体播放器 if (player == null) { //Uri uri = Uri.fromFile(file); player = MediaPlayer.create(MyService.this, R.raw.eason); //设置播放模式:单曲循环 player.setLooping(true); }else{ //设置音乐播放完毕后的方法 player.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { //释放资源 player.release(); } }); } } @Override public IBinder onBind(Intent intent) { return new MyBinder(); } class MyBinder extends Binder{ public MyService getMyService(){ return MyService.this; } } public void play(){ if (player != null && !player.isPlaying()) { if (position == 0) { try { //从头播放,想调用start必须先调用stop player.stop(); //准备方法 player.prepare(); //开始播放 player.start(); } catch (Exception e) { e.printStackTrace(); } }else{ //设置进度 player.seekTo(position); player.start(); } } } public void continuePlay(){ if (player != null && player.isPlaying()) { //获取当前播放的位置 position = player.getCurrentPosition(); //暂停音乐 player.pause(); } } public void pause(){ if (player != null) { //停止方法 player.stop(); //重置媒体播放器 player.reset(); } } }
相关文章推荐
- Linux简单的文本处理
- Shell在大数据的魅力时代:从一点点思路百度大数据面试题
- Java并发编程:Thread类的使用
- 使用VelocityTracker获取在屏幕滑动的速度
- sscanf() 的作用
- 小胖
- C语言第五课练习
- VSFTPD服务器
- PostgreSQL Why checkpointer impact performance so much ? - 3
- OCdemo-06 Block
- shell编程的MD5校验
- php性能优化
- Piotr's Ants(Uva 10881)
- Android自定义控件开发系列(零)——基础原理篇
- 主机上访问Virtual Box虚拟机上的Tomcat服务器
- angular $http配置属性
- PostgreSQL Why checkpointer impact performance so much ? - 2
- 我的Java学习之路1——Eclipse调试Bug的方法
- 《剑指Offer》面试题:扑克牌的顺子
- 深度探索C语言函数可变长参数