布局:
<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"
tools:context=".MainActivity"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="play"
android:text="播放" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="pause"
android:text="暂停" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="stop"
android:text="停止" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="continuePlay"
android:text="重播" />
</LinearLayout>
代码实现:
步骤:
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void play(View view){
}
public void pause(View view){
}
public void continuePlay(View view){
}
public void stop(View view){
}
}
这时候需要一个服务类:
创建服务类MusicService继承service
public class SSSMusicService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
并配置服务
<service android:name=".MusicService"/>
自然就需要几个方法:
public class SSSMusicService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void play(){
}
private void pause(){
}
private void continuePlay(){
}
private void stop(){
}
}
在MainActivity中调用的方法其实是service里面的4个方法,
这个时候需要一个充当中间人的对象;
public class SSSMusicService extends Service {
@Override
public IBinder onBind(Intent intent) {
return new MusicBinder();
}
class MusicBinder extends Binder{
}
private void play(){
}
private void pause(){
}
private void continuePlay(){
}
private void stop(){
}
}
这里需要一个接口:
新建一个接口MusicInterface.java
public interface MusicInterface {
void play();
void pause();
void stop();
void continuePlay();
}
实现接口:
class MusicBinder extends Binder implements MusicInterface{
@Override
public void play() {
}
@Override
public void pause() {
}
@Override
public void stop() {
}
@Override
public void continuePlay() {
}
}
因为它会调用服务的方法所以填充代码如下:
class MusicBinder extends Binder implements MusicInterface {
@Override
public void play() {
MusicService.this.play();
}
@Override
public void pause() {
MusicService.this.pause();
}
@Override
public void stop() {
MusicService.this.stop();
}
@Override
public void continuePlay() {
MusicService.this.continuePlay();
}
}
然后需要绑定服务得到中间者对象:
先start 再绑定 –>还需要一个服务连接对象
MainActivity.java
public class MainActivity extends Activity {
private MusicInterface mi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, MusicService.class);
startService(intent);
bindService(intent, new MyServiceConn(), BIND_AUTO_CREATE);
}
// 服务的连接对象
class MyServiceConn implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mi = (MusicInterface) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
public void play(View view) {
mi.play();
}
public void pause(View view) {
mi.pause();
}
public void stop(View view) {
mi.stop();
}
public void continuePlay(View view) {
mi.continuePlay();
}
}
下面开始设置播放:
需要一个MediaPlayer播放器
定义MediaPlayer;
MediaPlayer player;
在服务创建的时候创建,在服务销毁的时候销毁;
@Override
public void onCreate() {
super.onCreate();
player=new MediaPlayer();
}
@Override
public void onDestroy() {
super.onDestroy();
player.release();
player=null;
}
private void play() {
player.reset();
try {
player.setDataSource("sdcard/My Love.mp3");
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
player.start();
}
private void pause() {
player.pause();
}
private void stop() {
player.stop();
}
private void continuePlay() {
player.start();
}
另外,注意添加权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
效果如下:(仅界面)