音乐播放器

package com.example.app06_musicplayerframework;


import com.example.app06_musicplayerframework.MusicPlayerService.MyBinder;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends Activity {


private MyConnection conn;
private MyBinder musicContro1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Intent service = new Intent(this,MusicPlayerService.class);
conn = new MyConnection();

//第一步,调用通过bindService bind方式开启音乐播放的服务
bindService(service, conn, BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {

super.onDestroy();
unbindService(conn);
}
//上一首
public void pre(View v){
musicContro1.callpre();
}
//播放
public void play(View v){
musicContro1.callplay();
}
//暂停
public void pause(View v){
musicContro1.callpause();
}
//下一首
public void next(View v){
musicContro1.callNext();
}
private class MyConnection implements ServiceConnection{


@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//第四步,Service执行完onBind之后 如果返回值不为空
//就会走这个方法,参数IBinder对象就是onBind的返回值
//获取到这个对象,就可以调用它的public方法
musicContro1 = (MyBinder) service;

}


@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub

}


}

}


package com.example.app06_musicplayerframework;


import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;


public class MusicPlayerService extends  Service{


@Override
public IBinder onBind(Intent intent) {
//第三步 ,执行onBind
return new MyBinder();
}
@Override
public void onCreate() {


super.onCreate();
//第二步,执行onCreate
//MediaPlayer
Log.e("TAG", "音乐播放器");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("TAG", "onDestroy");
}
private void next(){
Log.e("TAG", "播放下一首----甜蜜蜜");
}
private void pre(){
Log.e("TAG", "播放上一首----一剪梅");
}
private void play(){
Log.e("TAG", "播放----倩女幽魂");


}
private void pause() {
Log.e("TAG", "暂停");


}
public class   MyBinder extends Binder{
public void callNext(){
next();
}
public void callpre(){
pre();
}
public void callplay(){
play();
}
public void callpause(){
pause();
}
}

}


activity--xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


    <Button
       
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上一首"
        android:onClick="pre" />


    <Button
    
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="播放" 
        android:onClick="play"/>


    <Button
        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="暂停"
        android:onClick="pause" />


    <Button
      
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下一首"
        android:onClick="next" />


</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值