09 | <action android:name="android.intent.action.MAIN"> |
10 | <category android:name="android.intent.category.LAUNCHER"> |
11 | </category></action></intent> |
13 | <activity android:name=".PlayerActivity"> |
15 | <service android:name=".MusicService" android:enabled="true"> |
我们注意到有2个Activity,1个Service,还有读写外部存储的权限声明
3、CoverActivity.java的代码如下:这是个全屏的启动画面,2秒后会跳转到PlayerActivity
01 | package app.android.elfplayer; |
03 | import android.app.Activity; |
04 | import android.content.Intent; |
05 | import android.os.Bundle; |
06 | import android.os.Handler; |
07 | import android.view.Window; |
08 | import android.view.WindowManager; |
10 | public class CoverActivity extends Activity { |
11 | /** Called when the activity is first created. */ |
13 | public void onCreate(Bundle savedInstanceState) { |
14 | super.onCreate(savedInstanceState); |
15 | getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); |
16 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
17 | setContentView(R.layout.cover); |
19 | new Handler().postDelayed(new Runnable(){ |
23 | Intent mainIntent = new Intent(CoverActivity.this,PlayerActivity.class); |
24 | CoverActivity.this.startActivity(mainIntent); |
25 | CoverActivity.this.finish(); |
4、PlayerActivity.java的代码如下:
001 | package app.android.elfplayer; |
003 | import android.app.Activity; |
004 | import android.content.ComponentName; |
005 | import android.content.Context; |
006 | import android.content.Intent; |
007 | import android.content.ServiceConnection; |
008 | import android.os.Bundle; |
009 | import android.os.Handler; |
010 | import android.os.IBinder; |
011 | import android.os.Message; |
012 | import android.os.RemoteException; |
013 | import android.util.Log; |
014 | import android.view.View; |
015 | import android.widget.ImageButton; |
016 | import android.widget.SeekBar; |
017 | import android.widget.SeekBar.OnSeekBarChangeListener; |
019 | public class PlayerActivity extends Activity { |
021 | public static final int PLAY = 1; |
022 | public static final int PAUSE = 2; |
024 | ImageButton imageButtonFavorite; |
025 | ImageButton imageButtonNext; |
026 | ImageButton imageButtonPlay; |
027 | ImageButton imageButtonPre; |
028 | ImageButton imageButtonRepeat; |
029 | SeekBar musicSeekBar; |
031 | IServicePlayer iPlayer; |
032 | boolean isPlaying = false; |
033 | boolean isLoop = false; |
036 | public void onCreate(Bundle savedInstanceState) { |
037 | super.onCreate(savedInstanceState); |
038 | setContentView(R.layout.player); |
040 | imageButtonFavorite = (ImageButton) findViewById(R.id.imageButtonFavorite); |
041 | imageButtonNext = (ImageButton) findViewById(R.id.imageButtonNext); |
042 | imageButtonPlay = (ImageButton) findViewById(R.id.imageButtonPlay); |
043 | imageButtonPre = (ImageButton) findViewById(R.id.imageButtonPre); |
044 | imageButtonRepeat = (ImageButton) findViewById(R.id.imageButtonRepeat); |
045 | musicSeekBar = (SeekBar) findViewById(R.id.musicSeekBar); |
047 | bindService(new Intent(PlayerActivity.this, MusicService.class), conn, Context.BIND_AUTO_CREATE); |
048 | startService(new Intent(PlayerActivity.this, MusicService.class)); |
050 | imageButtonPlay.setOnClickListener(new View.OnClickListener() { |
053 | public void onClick(View v) { |
054 | Log.i("yao", "imageButtonPlay -> onClick"); |
059 | } catch (RemoteException e) { |
062 | imageButtonPlay.setBackgroundResource(R.drawable.pause_button); |
068 | } catch (RemoteException e) { |
071 | imageButtonPlay.setBackgroundResource(R.drawable.play_button); |
077 | musicSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { |
080 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
084 | public void onStartTrackingTouch(SeekBar seekBar) { |
088 | public void onStopTrackingTouch(SeekBar seekBar) { |
089 | if (iPlayer != null) { |
091 | iPlayer.seekTo(seekBar.getProgress()); |
092 | } catch (RemoteException e) { |
099 | handler.post(updateThread); |
102 | private ServiceConnection conn = new ServiceConnection() { |
103 | public void onServiceConnected(ComponentName className, IBinder service) { |
104 | Log.i("yao", "ServiceConnection -> onServiceConnected"); |
105 | iPlayer = IServicePlayer.Stub.asInterface(service); |
108 | public void onServiceDisconnected(ComponentName className) { |
112 | Handler handler = new Handler() { |
114 | public void handleMessage(Message msg) { |
118 | private Runnable updateThread = new Runnable() { |
121 | if (iPlayer != null) { |
123 | musicSeekBar.setMax(iPlayer.getDuration()); |
124 | musicSeekBar.setProgress(iPlayer.getCurrentPosition()); |
125 | } catch (RemoteException e) { |
129 | handler.post(updateThread); |
5、其中用到的IServicePlayer.aidl,放在和Java文件相同的包中,内容如下:
01 | package app.android.elfplayer; |
02 | interface IServicePlayer{ |
07 | int getCurrentPosition(); |
08 | void seekTo(int current); |
09 | boolean setLoop(boolean loop); |
一旦你写好了这个IServicePlayer.aidl文件,ADT会自动帮你在gen目录下生成IServicePlayer.java文件
6、MusicService.java的内容如下:
01 | package app.android.elfplayer; |
03 | import android.app.Service; |
04 | import android.content.Intent; |
05 | import android.media.MediaPlayer; |
06 | import android.os.IBinder; |
07 | import android.os.RemoteException; |
08 | import android.util.Log; |
10 | public class MusicService extends Service { |
14 | public static MediaPlayer mPlayer; |
16 | public boolean isPause = false; |
18 | IServicePlayer.Stub stub = new IServicePlayer.Stub() { |
21 | public void play() throws RemoteException { |
26 | public void pause() throws RemoteException { |
31 | public void stop() throws RemoteException { |
36 | public int getDuration() throws RemoteException { |
37 | return mPlayer.getDuration(); |