bindService 实现音乐播放的功能
来源:互联网 发布:会员积分软件dnnyun 编辑:程序博客网 时间:2024/06/05 18:25
1:需要Activity实现ServiceConnection的接口
2:需要Service 提供一个继承了Binder的类来提供内部的方法。
3:进度条的设置
4:注册清单文件
public class MyActivity extends Activity implements ServiceConnection,Runnable { private Thread thread; private TextView textView; private Handler handler; private boolean flag; private ProgressBar progressBar; /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent service = new Intent(this, PlayService.class); bindService(service,this,BIND_AUTO_CREATE); textView = (TextView)findViewById(R.id.main_progress_textView); progressBar = (ProgressBar)findViewById(R.id.main_progressbar); thread = new Thread(this); handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); int position =msg.arg1; int total = msg.arg2; textView.setText(""+position+total); } }; thread.start(); } public void run(){ while(true){ try { Thread.sleep(1000); if(flag!=false) { int postion = controller.getCurrentPosition(); int total = controller.getDuration();// textView.setText(postion); Message msg = new Message(); msg.arg1=postion; msg.arg2=total; handler.sendMessage(msg); progressBar.setProgress(postion); } } catch (InterruptedException e) { e.printStackTrace(); } } } private PlayService.Controller controller; @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { if(PlayService.class.getName().equals(componentName.getClassName())){ controller = (PlayService.Controller)iBinder; progressBar.setMax(controller.getDuration()); Log.d("151217MY", "Connected"); } } public void btnPlay(View view) { if(progressBar.getVisibility()==ProgressBar.INVISIBLE){ progressBar.setVisibility(ProgressBar.VISIBLE); } controller.play(); flag=true; } public void btnPause(View view) { controller.pause(); flag=false; } @Override public void onServiceDisconnected(ComponentName componentName) { }}
public class PlayService extends Service{ private MediaPlayer player; @Override public IBinder onBind(Intent intent) { return new Controller(); } public class Controller extends Binder{ public void play(){ player.start(); } public void pause(){ player.pause(); } public int getCurrentPosition(){ return player.getCurrentPosition(); } public int getDuration(){ return player.getDuration(); } } @Override public void onCreate() { super.onCreate(); player = MediaPlayer.create(this, R.raw.nobody); } @Override public void onDestroy() { super.onDestroy(); player.stop(); player.release(); }
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/main_progress_textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="progress_textView" /> <ProgressBar android:id="@+id/main_progressbar" android:layout_width="match_parent" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Horizontal" android:visibility="invisible"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="btnPlay" android:text="Play" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="btnPause" android:text="Pause" /></LinearLayout>}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zqxue.HomeWork151216" android:versionCode="1" android:versionName="1.0" ><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-sdk android:minSdkVersion="15"/> <application android:label="@string/app_name" android:icon="@drawable/ic_launcher" > <activity android:name="MyActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <service android:name=".service.PlayService"/> <receiver android:name=".receiver.PlayerBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> <service android:name=".service.StartService"/> </application></manifest>
0 0
- bindService 实现音乐播放的功能
- BindService实现播放音乐的案例.txt
- 笔记——BindService实现音乐播放
- python实现简单的音乐播放功能
- Swing实现播放音乐的功能
- 使用service的startservice 和bindservice方法实现音乐的播放、暂停、继续
- 自己动手开发音乐播放器《五》播放功能的实现
- Service (bindService音乐播放器)
- 控制音乐播放器的退出再次播放相同音乐的功能实现
- 简单播放音乐的功能
- android音乐播放器之在线播放功能的实现
- android 应用开发-实现简单的音乐播放功能
- 安卓:bindService播放指定音乐
- android 音乐播放器功能实现
- 音乐播放器实现功能之—
- 使用AudioSession实现音乐后台播放功能
- 课程设计(打字游戏)设计功能实现之---简单的音乐mp3播放功能实现
- 音乐播放器功能的实现,歌词lrc显示,播放过程中来电
- 假如你想成为全栈工程师…
- 加班到底有什么用
- 论加班
- Swift之便利构造函数
- 通过BroadcastReceiver和 service 实现开机自动启动
- bindService 实现音乐播放的功能
- Android Scroll分析--实现滑动的七种方法
- OpenGL超级宝典7th简体中文-第二章前奏-开发环境的搭建
- Linux display subsytem(1)_概述
- 实现ios手机QQ空间导航栏控制器时隐时现效果,kvo的应用
- Corneerstone (svn)解决项目上传后,.a文件缺失问题解决记录
- Android命名规范详细
- 问题,值得研究 申请内存不释放没有问题
- Android入门第八篇之GridView(九宫图)