标签:
布局文件中设置了三个按钮分别来进行播放、暂停和继续播放
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <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="continuePlay"/> </LinearLayout>
在Activity中开启服务, 在服务中实现MP3的播放功能,然后通过一个Binder对象将该功能暴露给Activity。则可以在Activity中调用Service中的方法。
package xidian.dy.com.chujia; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.view.View; public class MainActivity extends AppCompatActivity { IMusic music; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent intent = new Intent(this, MusicService.class); startService(intent); bindService(intent, new MyConnection(), BIND_AUTO_CREATE); } public void play(View v){ music.play(); } public void pause(View v){ music.pause(); } public void continuePlay(View v){ music.continuePlay(); } class MyConnection implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) { music = (IMusic) service; } @Override public void onServiceDisconnected(ComponentName name) { } } }
在Service中创建一个MediaPlayer来控制音乐播放
package xidian.dy.com.chujia; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Binder; import android.os.IBinder; import android.support.annotation.Nullable; import java.io.IOException; /** * Created by dy on 2016/7/29. */ public class MusicService extends Service { MediaPlayer player; @Override public void onCreate() { player = new MediaPlayer(); super.onCreate(); } @Nullable @Override public IBinder onBind(Intent intent) { return new MyBinder(); } public void continuePlay(){ player.start(); } public void pause(){ player.pause(); } public void play(){ player.reset(); try { player.setDataSource("/sdcard/bc.mp3"); player.prepare(); player.start(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onDestroy() { super.onDestroy(); player.stop(); player.release(); } class MyBinder extends Binder implements IMusic{ @Override public void play() { MusicService.this.play(); } @Override public void pause() { MusicService.this.pause(); } @Override public void continuePlay() { MusicService.this.continuePlay(); } } }
IMusic是一个接口类,主要是为了能在Activity中调用service中的方法。
package xidian.dy.com.chujia; /** * Created by dy on 2016/7/29. */ public interface IMusic { void play(); void pause(); void continuePlay(); }
在清单文件中需要对服务进行注册
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="xidian.dy.com.chujia"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="主界面"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MusicService"/> </application> </manifest>
标签:
原文地址:http://www.cnblogs.com/xidongyu/p/5719893.html