使用Stared类型服务实现

  • 播放、暂停、重播、结束、退出功能
  • 通过Intent传递参数值给Service。参数值:
    1----播放
    2----暂停
    3----重播
    0----结束
    -1----退出

一、准备工作

1.在res文件夹下新建raw资源文件夹
2.将音乐文件复制到raw文件夹

二、创建MusicService

public class MusicService extends Service {public MusicService() { }MediaPlayer mp; //定义音乐播放器@Overridepublic void onCreate() {super.onCreate();if(mp==null){mp=MediaPlayer.create(getApplicationContext(), R.raw.background);
//mp.setLooping(true);}}@Overridepublic void onDestroy() {super.onDestroy();if (mp != null) {mp.stop();mp.release(); //回收}}@Overridepublic int onStartCommand(Intent intent,int flags, int startId) {if (intent != null){Bundle bundle = intent.getExtras();int op=bundle.getInt("op");switch (op) {case 1:if (!mp.isPlaying()) {mp.start();}break;case 2:if (mp.isPlaying()) {mp.pause();}break;case 3:if (mp != null) {mp.stop(); //先停止try {mp.prepare(); //预备} catch (IOException ex) {Log.d("checkpoint","出现异常");}mp.seekTo(0);mp.start();}break;case 0:stopSelf();break;} //end switch} //end ifreturn super.onStartCommand(intent, flags, startId);} //end onStartCommand@Overridepublic IBinder onBind(Intent intent) {// TODO: Return the communication channel to the service.throw new UnsupportedOperationException("Not yet implemented");}
}

三、主程序MainActivity主要代码:

public class MainActivity extends AppCompatActivity {int op;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button bt1= (Button)findViewById(R.id.btn_start);Button bt2= (Button)findViewById(R.id.btn_pause);Button bt3= (Button)findViewById(R.id.btn_restart);Button bt4= (Button)findViewById(R.id.btn_stop);Button bt5= (Button)findViewById(R.id.btn_exit);View.OnClickListener onclicklistener= new View.OnClickListener() {@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_start:op = 1;break;case R.id.btn_pause:op = 2;break;case R.id.btn_restart:op = 3;break;case R.id.btn_stop:op = 0;break;case R.id.btn_exit:op = -1;break;}final Intent intent = new Intent( MainActivity.this, MusicService.class );if(op!=-1){Bundle bundle = new Bundle();bundle.putInt("op", op);intent.putExtras(bundle);startService(intent);}else{new AlertDialog.Builder(MainActivity.this).setTitle("提示").setMessage("确认要退出吗?").setPositiveButton("确定", new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface arg0, int arg1) {stopService(intent);finish();}}).setNegativeButton("取消", null).show();}//end onClick}; //listener};bt1.setOnClickListener(onclicklistener);bt2.setOnClickListener(onclicklistener);bt3.setOnClickListener(onclicklistener);bt4.setOnClickListener(onclicklistener);bt5.setOnClickListener(onclicklistener);}
}

四、布局文件

<Buttonandroid:id="@+id/btn_start"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="28dp"android:layout_marginTop="548dp"android:text="开始"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/btn_pause"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="160dp"android:layout_marginTop="548dp"android:text="暂停"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/btn_restart"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="48dp"android:layout_marginTop="548dp"android:text="重播"app:layout_constraintStart_toEndOf="@+id/btn_pause"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/btn_stop"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="92dp"android:layout_marginTop="624dp"android:text="结束"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/btn_exit"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="236dp"android:layout_marginTop="624dp"android:text="退出"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" />

五、运行结果


AndroidStudio音乐播放服务service实现相关推荐

  1. 【android】音乐播放器之service服务设计

    学习Android有一个多月,看完了<第一行代码>以及mars老师的第一期视频通过音乐播放器小项目加深对知识点的理解.从本文开始,将详细的介绍简单仿多米音乐播放器的实现,以及网络解析数据获 ...

  2. Androidstudio音乐播放器

    Androidstudio音乐播放器 实现目的:利用广播在myapplication中原本button点我暂停按钮是灰色无法点击的,此时发送一条短信给自己的安卓手机,按钮变成黑色,音乐开始播放,此时点 ...

  3. Android应用开发--MP3音乐播放器Service实现

    Android应用开发--MP3音乐播放器Service实现 2013年5月29日简.美音乐播放器开发记录 让网友们久等啦,关于简.美音乐播放器的开发,最重要的Service类总算是要发博了.关于An ...

  4. Android歌词秀设计思路(3)通用的音乐播放服务(上)

    MediaPlayerService作为通用的音乐播放Service类,它的功能有: 控制音乐播放,停止,暂停,前/后歌曲切换. Audio Focus相关处理(对应应用程序切换). Intent处理 ...

  5. Android仿虾米音乐播放器之service

    service就是后台服务,不同于activity在前台,虽然用户看不见,但是作用是很大的. 我们在service中先需要实例化mediaplayer对象,这个在上节中已经讲了,除此之外我们需要注册一 ...

  6. AndroidStudio音乐播放器进度条和歌曲时间的操作

    1.首先,我们需要使用在activity_main布局中定义好一个SeekBar和两个ListView,SeekBar表示进度条,两个ListView分别用来表示当前的时间和歌曲的总时间. 2.在Mu ...

  7. android mp3进度条和时间,AndroidStudio音乐播放器进度条和歌曲时间的操作

    1.首先,我们需要使用在activity_main布局中定义好一个SeekBar和两个ListView,SeekBar表示进度条,两个ListView分别用来表示当前的时间和歌曲的总时间. 2.在Mu ...

  8. Android Service使用方法--简单音乐播放实例

    Service翻译成中文是服务,熟悉Windows 系统的同学一定很熟悉了.Android里的Service跟Windows里的Service功能差不多,就是一个不可见的进程在后台执行. Androi ...

  9. Android---Service(生命周期、启动方式、服务通信、实战演练、思维导图、高级音乐播放器-源码)

    目   录 一.服务的创建 二.服务的生命周期 三.服务的启动方式 (1)startService 方式 启动 服务 实战演练---startService (2)bindService 方式 启动 ...

最新文章

  1. javascript-数据类型,json与数组,获取非行间样式
  2. android虚拟机的垃圾收集
  3. Spark基础脚本入门实践2:基础开发
  4. 200723学习日报
  5. 统计UTF-8编码方式字符串中的符号个数
  6. java对象类型有哪些_Java中常用的对象数据类型有哪些?它们分别又占多少个字节呢?...
  7. springboot使用mongodb
  8. Sharepoint学习笔记—ECM系列--从.CSV文件导入术语集(Term Sets)
  9. eXosip事件总结
  10. WiFi 转DMX512模块 支持Art-Net sACN RDM DMX
  11. 四 H264解码输出yuv文件
  12. NFT+体育,卡塔尔世界杯有哪些NFT看点!
  13. 《Adaptive Unimodal Cost Volume Filtering for Deep Stereo Matching》
  14. RedHat认证介绍
  15. android 实时同步短信,备份Android短信的4种方法你最好知道
  16. 学习汇编语言的重要性
  17. 软件测试面试英文自我介绍,软件测试英文自我介绍
  18. 10大PHP开源网店系统
  19. 【原创】IP摄像头技术纵览(六)---通过internet访问摄像头
  20. php开发微信群机器人,[极客开发]WechatRobot - PHP微信机器人开发包

热门文章

  1. [C库函数]memset的内部实现
  2. Kubernetes的ETCD集群备份、恢复
  3. 晚上鸟沒事,白天没鸟事_鸟箱
  4. websocket测试工具,支持ws wss客户端
  5. Reloaded modules:在Spyder运行时错误
  6. 用Python来可视化微信好友
  7. 上海电信:提速降费助力智慧城市建设
  8. 修改本地连接IP地址的脚本包括修改备用dns的方法(批处理命令)
  9. linux 进程监控
  10. JarvisOJ-握手包