关于ContentProvider的阐述请访问:http://www.cnblogs.com/xuyinghui/p/4613754.html

该程序实现的功能是:当点击播放,暂停按钮时,能够实现音乐的控制。实现界面如下:

一般的Android应用程序的音乐可以分为背景音乐和点击各种控件时的音效,前者一般为比较大的音乐文件,后者一般为比较小的音乐。在Android中一般用MediaPlayer类处理比较大的音频文件,用SoundPool类处理比较短促的音频文件。 

针对这个程序:

1:布局中,相对简单些,一个文本框,四个按钮。由于这里没有复杂的东西,不再展示。

2:MainActivity中:这里要说的一点就是由于有四个按钮,设置按钮的点击事件时,应该实现当用户点击不同按钮时,传递不同的参数给Service.

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

一定要避免控件没有查找就进行使用的情况。

3:自定义一个Service类,用来获取音频,启动服务操作。并且通过onStart()方法判断得到的意图中的参数,并且调用相应的方法。

得到要播放视频的位置,然后调用init方法播放由位置得到的音乐文件。代码如下:

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

 

posted on 2015-07-01 17:45  星梦缘vs惜  阅读(51)  评论(0编辑  收藏  举报