安卓:bindService播放指定音乐
来源:互联网 发布:简单力学分析软件 编辑:程序博客网 时间:2024/06/03 21:32
raw为自个建的
清单文件中注册服务
逻辑代码文件:
<span style="font-size:18px;">package com.example.day22_music;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;import com.example.day22_music.MusicService.MyBind;public class MainActivity extends Activity {MusicService musicService;MyConn conn=new MyConn(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View v) { switch(v.getId()) { case R.id.bt1: if(musicService==null) { Intent intent=new Intent(MainActivity.this,MusicService.class); bindService(intent, conn, Context.BIND_AUTO_CREATE); } else { musicService.play(); } break; case R.id.bt2: if(musicService!=null) { musicService.pause(); } break; case R.id.bt3: if(musicService!=null) { musicService.stop(); musicService=null; unbindService(conn); } break; } } class MyConn implements ServiceConnection {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {MyBind mb= (MyBind) service;musicService=mb.getService();}@Overridepublic void onServiceDisconnected(ComponentName name) {conn=null;} }}</span>
服务类文件:
<span style="font-size:18px;">package com.example.day22_music;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 MusicService extends Service{MediaPlayer mediaPlayer;//声明操作媒体的对象int pos=0;//记录播放的位置@Overridepublic IBinder onBind(Intent intent) {play();//绑定时进行播放return new MyBind();}class MyBind extends Binder{public MusicService getService(){return MusicService.this;}}@Overridepublic void onCreate() {super.onCreate();if(mediaPlayer==null){mediaPlayer=mediaPlayer.create(MusicService.this, R.raw.heavencity);mediaPlayer.setLooping(false);//设置不需要单曲循环}//播放完毕 进行监听回调mediaPlayer.setOnCompletionListener(new OnCompletionListener() {@Overridepublic void onCompletion(MediaPlayer mp) {mediaPlayer.release();//释放资源}});}public void play(){if(mediaPlayer!=null&&!mediaPlayer.isPlaying()){try {if(pos!=0){//根据指定位置进行播放mediaPlayer.seekTo(pos);mediaPlayer.start();}else{//从头播放mediaPlayer.stop();mediaPlayer.prepare();//启动之前必须preparemediaPlayer.start();}} catch (Exception e){e.printStackTrace();}}else{}}public void pause(){if(mediaPlayer!=null&&mediaPlayer.isPlaying()){pos=mediaPlayer.getCurrentPosition();mediaPlayer.pause();}}public void stop(){if(mediaPlayer!=null){mediaPlayer.stop();mediaPlayer.reset();}}}</span>
布局文件:
<span style="font-size:18px;"><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:id="@+id/tv" android:layout_width="match_parent" android:layout_height="150dp" android:gravity="center" android:background="#00ff00"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="播放音乐" android:onClick="click"/> <Button android:id="@+id/bt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="暂停音乐" android:onClick="click"/> <Button android:id="@+id/bt3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止音乐" android:onClick="click"/> </LinearLayout> </LinearLayout></span>
0 0
- 安卓:bindService播放指定音乐
- 安卓播放播放音乐
- Service (bindService音乐播放器)
- 安卓音乐播放器
- 安卓 音乐播放器
- 安卓后台播放音乐
- 安卓拍照+音乐播放
- bindService 实现音乐播放的功能
- 笔记——BindService实现音乐播放
- BindService实现播放音乐的案例.txt
- 安卓开发之音乐播放
- 安卓音乐播放器--侧边栏
- cocos2dx 安卓环境播放mid音乐
- 安卓后台音乐播放(Service)
- 安卓音乐播放器1
- 安卓简易音乐播放器
- 安卓 简易音乐播放器
- 安卓 多媒体音乐播放器
- Linux——更新
- Java垃圾回收机制
- org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has
- python中格式化字符串
- struts 中继承ActionSupport类 HelloWorld!aliasAction.action
- 安卓:bindService播放指定音乐
- Android Proguard 如何混淆package name
- SOCKET:SO_LINGER 选项
- UVA 12716 GCD XOR
- jQuery.validate.js第二次调用validate()方法时,返回值错误bug解决
- spring笔记 多线程的支持
- Android studio 导入工程 出现错误
- ReactJs 技巧
- 关于csdn的赵老师