android 四大组件service 音乐播放器的实现
来源:互联网 发布:sqlserver修改主键值 编辑:程序博客网 时间:2024/06/04 22:46
- 新创建一个Android工程《音乐播放器》,包名:com.itheima.musicPlayer。
在res目录下新建一个文件夹raw(名字必须为raw,约定大于配置的原则),然后在raw目录中拷贝进一个音乐文件,注意文件名必须遵循Android资源文件的命名规则。 - 在src目录下,新建一个MediaService继承Service类,在该类中实现核心服务的方法。
private static MediaPlayer player; public static int ispb; @Override public IBinder onBind(Intent intent) { System.out.println("服务返回了MediaContoller对象。。。。"); return new MediaContoller(); } @Override public void onCreate() { super.onCreate(); System.out.println("创建一个服务,并初始化MediaPlayer"); player = MediaPlayer.create(getApplicationContext(), R.raw.a); } public static class MediaContoller extends Binder{ //开始播放 public int play(){ player.start(); return 0; } //暂停播放 public int puse(){ player.pause(); return 1; } //停止播放 public void stop(){ player.stop(); } //音乐的总时长 public int getDuration(){ return player.getDuration(); } //当前播放的位置 public int getCurrentPosition(){ return player.getCurrentPosition(); } //判断是否播放 public boolean isplaying(){ return player.isPlaying(); } }
- 这是使用系统默认的布局文件,activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:padding="10dp" android:text="音乐播放器" android:textSize="20sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="play" android:text="播放" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="puse" android:text="暂停" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="stop" android:text="停止" /> </LinearLayout> <ProgressBar android:id="@+id/pb" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="10dp" /></LinearLayout>
- 使用默认的MainActivity类,在该类中完成业务的控制
package com.example.servicemusic;import com.example.service.MusicService;import com.example.service.MusicService.MediaContoller;import android.support.v7.app.ActionBarActivity;import android.annotation.SuppressLint;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.SystemClock;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.MediaController;import android.widget.ProgressBar;import android.widget.Toast;public class MainActivity extends ActionBarActivity { private ProgressBar pb; // 声明自定义的MediaController对象 private MediaContoller controller; private boolean isrunning; private static int ispb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pb = (ProgressBar) findViewById(R.id.pb); // 如何绑定服务? // 创建一个用于启动服务的显示意图,指向我们自定义的MediaService类 Intent service = new Intent(this, MusicService.class); // 绑定服务,同时服务开启,如果成功则返回true否则返回false isrunning = bindService(service, new MediaConnection(), BIND_AUTO_CREATE); if (isrunning) { System.out.println("服务绑定成功!"); } else { System.out.println("服务绑定失败!"); } } /** * 播放 * * @param v */ @SuppressLint("ShowToast") public void play(View v) { if (controller != null) { if (controller.isplaying()) { Toast.makeText(this, "音乐正在播放中......", 1).show(); return; }else{ controller.play(); System.out.println("开始播放音乐"); } } } /** * 暂停 * * @param v */ public void puse(View v) { if (controller != null) { controller.puse(); System.out.println("音乐暂停"); } } /** * 停止 * * @param v */ public void stop(View v) { if (controller != null) { pb.setProgress(0); controller.stop(); //unbindService(new MediaConnection());//终止绑定 System.out.println("音乐停止"); } } /** * 更新进度条 */ private void updatePro() { new Thread() { public void run() { while (true) { SystemClock.sleep(400); pb.setProgress(controller.getCurrentPosition()); if (controller.getCurrentPosition() == controller .getDuration()) { break; } } }; }.start(); } /** * 实现ServiceConnection 类 * * @author Blake * */ class MediaConnection implements ServiceConnection { /** * 当service被绑定的时候回调该函数 */ @Override public void onServiceConnected(ComponentName name, IBinder service) { // 返回的ibinder对象其实就是我们自定义的MediaContoller controller = (MediaContoller) service; // 设置进度条的最大值 pb.setMax(controller.getDuration()); // 更新进度条 updatePro(); System.out.println("服务器已经连接......"); } /** * 服务器断开或关闭的时候调用该方法 */ @Override public void onServiceDisconnected(ComponentName name) { System.out.println("服务器断开或关闭"); } }}
- 在AndroidManifest.xml中注册Service
- 将工程部署到模拟器上,点击播放,发现成功播放了音乐。点击暂停,发现音乐暂停了,然后点击播放,音乐再次响起。点击停止,问题来了,我们发现点击停止后再次点击播放音乐没能再次播放,因为这里面直接调用MediaPlayer的stop方法是有bug的。因此为了解决这样的问题,我们应该将停止调用层pause方法,同时只需调用MediaPlayer的seekTo(int)方法将音乐设置到开始位置。
//停止播放
public void stop(){
player.pause();
player.seekTo(0);
}
0 0
- android 四大组件service 音乐播放器的实现
- 深入剖析Android四大组件(六)——相对完美的后台Service实现播放音乐功能
- Android四大组件之Service详解(二)——Service在音乐播放器中的使用
- 【Android】基于Service组件的简易音乐播放器
- Android音乐播放器 -- MediaPlayer Service实现
- Android四大组件--Service,后台播放背景音乐
- android组件Service控制后台音乐播放
- Android的音乐播放器使用service
- 从一个音乐播放器来了解android的Service组件和BroadcastReceiver
- android-service音乐播放器
- Android Service 音乐播放器
- Android 音乐播放器的实现(三)Service的实现
- Android学习笔记(七) 音乐播放器 Service实现
- Android应用开发--MP3音乐播放器Service实现
- Android应用开发--MP3音乐播放器Service实现
- Android应用开发--MP3音乐播放器Service实现
- android音乐播放器实现(Service+BroadcastReceiver+Notification)
- Android之Service服务实现音乐播放器
- HashMap vs. TreeMap vs. Hashtable vs. LinkedHashMap
- 输入子系统
- 希尔排序c语言版
- 运算符
- 单例模式(多线程不安全,序列化不安全,反射不安全实例)
- android 四大组件service 音乐播放器的实现
- IdentityHashMap 源代码
- mesos上安装hadoop过程简介
- linux中find批量删除空文件及空文件夹脚本
- oracle
- 黑马程序员——Java基础语法(一.关键字、标识符、注释、常量和变量、运算符)
- url加密调研报告
- FFMpeg+SDL多线程播放器
- ArrayAdapter实现仿QQ左右发送消息显示