实现音乐播放器

音乐播放器

   首先声明一下,本例是直接采用课本中范例122的方法。

效果图如下:

1、activity_main.xml布局

 

 1 //四个按钮
 2     <LinearLayout
 3         android:layout_width="fill_parent"
 4         android:layout_height="wrap_content"
 5         android:orientation="horizontal">
 6 // 上一首 
 7      <Button
 8          android:id="+id/previous"
 9          android:layout_width="wrap_content"
10          android:layout_height="fill_parent"
11          android:layout_weight="1"
12          android:text="上一首"
13       />
14 // 播放
15      <Button
16          android:id="+id/play"
17          android:layout_width="wrap_content"
18          android:layout_height="fill_parent"
19          android:layout_weight="1"
20          android:text="播放"
21       />
22 // 下一首
23      <Button
24          android:id="+id/next"
25          android:layout_width="wrap_content"
26          android:layout_height="fill_parent"
27          android:layout_weight="1"
28          android:text="下一首"
29       />
30 // 暂停 
31      <Button
32          android:id="+id/pause"
33          android:layout_width="wrap_content"
34          android:layout_height="fill_parent"
35          android:layout_weight="1"
36          android:text="暂停"
37       />
38      </LinearLayout>
View Code

 

2、MainAxtivity.java

  (1)定义四个按钮

  (2)绑定相应的监听对象

  (3)传递不同点击参数

代码如下:

 1 public class MainActivity extends Activity implements OnClickListener {
 2         //初始化控件
 3     private Button mBtnPrevious; // 上一首
 4     private Button mBtnPlay; // 播放
 5     private Button mBtnNext; // 下一首
 6     private Button mBtnPause; // 暂停
 7     private ComponentName component; // 用于启动服务
 8 
 9     public void onCreate(Bundle savedInstanceState) {
10         super.onCreate(savedInstanceState);
11         setContentView(R.layout.activity_main);
12         // 得到布局中的控件
13         findView();
14         // 绑定控件事件
15         setListener();
16     }
17 
18     // 得到布局中的控件
19     private void findView() {
20         component = new ComponentName(this, MusicService.class);
21         mBtnPrevious = (Button) findViewById(R.id.previous);
22         mBtnPlay = (Button) findViewById(R.id.play);
23         mBtnNext = (Button) findViewById(R.id.next);
24         mBtnPause = (Button) findViewById(R.id.pause);
25     }
26     // 绑定控件事件
27     private void setListener() {
28         mBtnPrevious.setOnClickListener(this);
29         mBtnPlay.setOnClickListener(this);
30         mBtnNext.setOnClickListener(this);
31         mBtnPause.setOnClickListener(this);
32     }
33     // 按钮点击事件响应
34     public void onClick(View v) {
35         // 如果点击前一首歌,就在intent中传递前一首歌参数
36         if (v == mBtnPrevious) {
37             Intent mIntent = new Intent(MusicService.PREVIOUS_ACTION);
38             mIntent.setComponent(component);
39             startService(mIntent);
40         // 如果点击前播放歌曲,就在intent中传递播放当前歌参数
41         } else if (v == mBtnPlay) {
42             Intent mIntent = new Intent(MusicService.PLAY_ACTION);
43             mIntent.setComponent(component);
44             startService(mIntent);
45         // 如果点击前一首歌,就在intent中传递下一首歌参数
46         } else if (v == mBtnNext) {
47             Intent mIntent = new Intent(MusicService.NEXT_ACTION);
48             mIntent.setComponent(component);
49             startService(mIntent);
50         // 如果点击前一首歌,就在intent中传递暂停首歌参数
51         } else {
52             Intent mIntent = new Intent(MusicService.PAUSE_ACTION);
53             mIntent.setComponent(component);
54             startService(mIntent);
55         }
56     }
57 }
主要代码

3、自定义Service

    (1)获取音频数据的字段名称

     (2) 初始化MediaPlayer对象

    (3)通过getContentResolver得到系统中所有音乐,只获取播放时间在10秒以上的音乐

    (4)onStart方法中判断得到的intent中的参数,调用相应方法

    (5)分别定义音乐的播放、暂停、前一首、下一首的实现内容

代码如下:

  1 //定义音乐服务类
  2 public class MusicService extends Service {
  3     // 定义需要显示的音乐的字段
  4     String[] mCursorCols = new String[] {
  5             "audio._id AS _id", // index must match IDCOLIDX below
  6             MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM,
  7             MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA,
  8             MediaStore.Audio.Media.MIME_TYPE, MediaStore.Audio.Media.ALBUM_ID,
  9             MediaStore.Audio.Media.ARTIST_ID, MediaStore.Audio.Media.DURATION };
 10     private MediaPlayer mMediaPlayer; // 声明播放器
 11     private Cursor mCursor; // 声明游标
 12     private int mPlayPosition = 0; // 当前播放的歌曲
 13 
 14     // 注册意图
 15     public static final String PLAY_ACTION = "com.wyl.music.PLAY_ACTION";
 16     public static final String PAUSE_ACTION = "com.wyl.music.PAUSE_ACTION";
 17     public static final String NEXT_ACTION = "com.wyl.music.NEXT_ACTION";
 18     public static final String PREVIOUS_ACTION = "com.wyl.music.PREVIOUS_ACTION";
 19 
 20     @Override
 21     public IBinder onBind(Intent arg0) {
 22         return null;
 23     }
 24 
 25     @Override
 26     public void onCreate() {
 27         super.onCreate();
 28         mMediaPlayer = new MediaPlayer();
 29         Uri MUSIC_URL = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;// 通过一个URI可以获取所有音频文件
 30         //默认大于10秒的可以看作是系统音乐
 31         mCursor = getContentResolver().query(MUSIC_URL, mCursorCols,
 32                 "duration > 10000", null, null);
 33     }
 34 
 35     @Override
 36     public void onStart(Intent intent, int startId) {
 37         super.onStart(intent, startId);
 38         // 根据不同的action,做不同的相应
 39         String action = intent.getAction();
 40         //播放
 41         if (action.equals(PLAY_ACTION)) {
 42             play();
 43         //暂停
 44         } else if (action.equals(PAUSE_ACTION)) {
 45             pause();
 46         //下一首
 47         } else if (action.equals(NEXT_ACTION)) {
 48             next();
 49         //前一首
 50         } else if (action.equals(PREVIOUS_ACTION)) {
 51             previous();
 52         }
 53     }
 54 
 55     // 播放音乐
 56     public void play() {
 57         //初始化音乐播放器
 58         inite();
 59     }
 60 
 61     // 暂停时,结束服务
 62     public void pause() {
 63         //暂停音乐播放
 64         stopSelf();
 65     }
 66 
 67     // 上一首
 68     public void previous() {
 69         //得到前一首的歌曲
 70         if (mPlayPosition == 0) {
 71             mPlayPosition = mCursor.getCount() - 1;
 72         } else {
 73             mPlayPosition--;
 74         }
 75         //开始播放
 76         inite();
 77     }
 78 
 79     // 下一首
 80     public void next() {
 81         //得到后一首歌曲
 82         if (mPlayPosition == mCursor.getCount() - 1) {
 83             mPlayPosition = 0;
 84         } else {
 85             mPlayPosition++;
 86         }
 87         //开始播放
 88         inite();
 89     }
 90 
 91     // 初始化播放器
 92     public void inite() {
 93         //充值MediaPlayer
 94         mMediaPlayer.reset();
 95         // 获取歌曲位置
 96         String dataSource = getDateByPosition(mCursor, mPlayPosition);
 97         // 歌曲信息
 98         String info = getInfoByPosition(mCursor, mPlayPosition);
 99         // 用Toast显示歌曲信息
100         Toast.makeText(getApplicationContext(), info, Toast.LENGTH_SHORT)
101                 .show();
102         try {
103             // 播放器绑定资源
104             mMediaPlayer.setDataSource(dataSource);
105             // 播放器准备
106             mMediaPlayer.prepare();
107             // 播放
108             mMediaPlayer.start();
109         } catch (IllegalArgumentException e1) {
110             e1.printStackTrace();
111         } catch (IllegalStateException e1) {
112             e1.printStackTrace();
113         } catch (IOException e1) {
114             e1.printStackTrace();
115         }
116     }
117 
118     // 根据位置来获取歌曲位置
119     public String getDateByPosition(Cursor c, int position) {
120         c.moveToPosition(position);
121         int dataColumn = c.getColumnIndex(MediaStore.Audio.Media.DATA);
122         String data = c.getString(dataColumn);
123         return data;
124     }
125 
126     // 获取当前播放歌曲演唱者及歌名
127     public String getInfoByPosition(Cursor c, int position) {
128         c.moveToPosition(position);
129         int titleColumn = c.getColumnIndex(MediaStore.Audio.Media.TITLE);
130         int artistColumn = c.getColumnIndex(MediaStore.Audio.Media.ARTIST);
131         String info = c.getString(artistColumn) + " "
132                 + c.getString(titleColumn);
133         return info;
134 
135     }
136 
137     // 服务结束时要释放MediaPlayer
138     public void onDestroy() {
139         super.onDestroy();
140         mMediaPlayer.release();
141     }
142 }
音乐服务类

播放器完成!

 

转载于:https://www.cnblogs.com/j0820/p/4620104.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值