android四大组件之Service 播放音乐
2015-11-19 14:36
417 查看
首先定义接口
package com.example.musicplay; public interface PlayInterface { void play(); void playContiue(); void pause(); }
package com.example.musicplay; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Binder; import android.os.IBinder; public class PlayService extends Service { private MediaPlayer mediaPlayer; @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. return new MusicBinder(); } class MusicBinder extends Binder implements PlayInterface{ @Override public void play() { // TODO Auto-generated method stub PlayService.this.play(); } @Override public void playContiue() { // TODO Auto-generated method stub PlayService.this.playContiue(); } @Override public void pause() { // TODO Auto-generated method stub PlayService.this.pause(); } } @Override public void onCreate() { mediaPlayer = new MediaPlayer(); } private void play(){ //重置 mediaPlayer.reset(); try { mediaPlayer.setDataSource("sdcard/Charlotte Perrelli - Hero.mp3"); //准备 mediaPlayer.prepare(); } catch (Exception e) { // TODO Auto-generated catcssh block e.printStackTrace(); } mediaPlayer.start(); } private void playContiue(){ mediaPlayer.start(); } private void pause(){ mediaPlayer.pause(); } }
package com.example.musicplay; 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; import android.view.View.OnClickListener; public class MainActivity extends Activity { private PlayInterface p; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(this,PlayService.class); startService(intent); bindService(intent, new Conn(), BIND_AUTO_CREATE); findViewById(R.id.paly).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { p.play(); } }); findViewById(R.id.contiue).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { p.playContiue(); } }); findViewById(R.id.pause).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub p.pause(); } }); } class Conn implements ServiceConnection{ @Override public void onServiceConnected(ComponentName arg0, IBinder arg1) { // TODO Auto-generated method stub p = (PlayInterface) arg1; } @Override public void onServiceDisconnected(ComponentName arg0) { // TODO Auto-generated method stub } } }
相关文章推荐
- 【Android】长按连续触发事件的实现方法
- Android编程实现仿心跳动画效果的方法
- android录制声音写入文件
- 关于android应用的toolbar(actionbar)不能填满布局解决
- Android在SDCard创建目录_卸载应用同时删除工程创建的文件
- Android 4.4前后版本读取图库图片和拍照完美解决方案
- android自定义状态栏的颜色
- Android 工程师如何快速学会web前段
- button设置点击更改背景图片
- 如何创建上下文菜单
- Android 设置Activity窗体 不显示标题和全屏显示
- Android Animation实现元素在屏幕上按照指定轨迹运动,以及出现NullPointerException的解决方案
- Android编程之界面跳动提示动画效果实现方法
- Android 工程师如何快速学会web前段
- Android学习碎片(二)——如何从本地图片获得Bitmap实例
- 【异常】Configure Android Studio such that source detected in git but Unregistered Vcs root detected
- android:scaleType、android:layout_alignBottom、layout_below学习
- Android RecyclerView使用介绍
- android 布局
- Android基础入门教程——10.6 PowerManager(电源服务)