|
大家好今天我将为大家分享基于Service与ContentProvider的音乐播放实例,对于接触Android有一些时日的人来说,Android的核心也就是Activity,Service,ContentProvider,BroadCastReceiver,以及串联它们的Intent五大模块,Activity我就不用多说了,而我将就这个例子来说一下Service,以及ContentProvider.
Service:
Android中的服务,它与Activity不同,它是不能与用户交互的,运行在后台的程序,如果我们退出应用时,没有结束进程,它仍然在后台运行,那我们什么时候会用到Service呢?比如我们播放音乐的时候,有可能想边听音乐边干些其他事情,当我们退出播放音乐的应用,如果不用Service,我们就听不到歌了,所以这时候就得用到Service了,又比如当我们一个应用的数据是通过网络获取的,不同时间(一段时间)的数据是不同的这时候我们可以用Service在后台定时更新,而不用每打开应用的时候在去获取。
CotentProvider:
Android中的内容提供者,它让我们可以通过一个URL跨应用获取数据(通常是SQLite数据库),我觉得Android这个还是机制还是非常不错的,特别是我们想获取Sdcard里一些数据时,比如我们想获取所有Sdcard里的音频,视频,图片等,我们只要通过一个URL就可以轻松搞定,其实我们在开机或者插入Sdcard时,Android会做一些事情,就是它自动建库,将我们卡里所有音频,视频,图片等信息存在相应的表中,我们可以用DDMS打开看一下如下图(data/data目录下),红线是我手机当前卡建立的数据库(不同卡会建立不同的数据库)
然后我们可以将这个数据库导出,用可以打开.db的工具打开浏览数据库的相关信息如下图所示(我这里打开了音频的数据表,可以看到我手机里所有音频文件,当然还有数据表字段):
本来这个应用是我用来写播放音乐Widget的代码,但是布局有点多,我就简单化了,做了一个比较 简单的Demo,老规矩Step by Step.
第一步:新建一个Android工程命名为MusicDemo.
第二步:候改main.xml布局文件(我这里增加了四个按钮,上一首,播放,下一首,暂停)代码如下:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Mr Wei's blog."
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/previous"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="上一首"
/>
<Button
android:id="@+id/play"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="播放"
/>
<Button
android:id="@+id/next"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="下一首"
/>
<Button
android:id="@+id/pause"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="暂停"
/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Welcome to Mr Wei's blog." /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <Button android:id="@+id/previous" android:layout_height="fill_parent" android:layout_width="wrap_content" android:layout_weight="1" android:text="上一首" /> <Button android:id="@+id/play" android:layout_height="fill_parent" android:layout_width="wrap_content" android:layout_weight="1" android:text="播放" /> <Button android:id="@+id/next" android:layout_height="fill_parent" android:layout_width="wrap_content" android:layout_weight="1" android:text="下一首" /> <Button android:id="@+id/pause" android:layout_height="fill_parent" android:layout_width="wrap_content" android:layout_weight="1" android:text="暂停" /> </LinearLayout></LinearLayout>
第三步:新建一个MusicService.java类,播放音乐都是在这个类里进行的哦,代码如下:
view plaincopy to clipboardprint?
package com.tutor.music;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.IBinder;
import android.provider.MediaStore;
import android.widget.Toast;
public
class MusicService extends Service {
String[] mCursorCols = new String[] {
"audio._id AS _id", // index must match IDCOLIDX below
MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.MIME_TYPE, MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.ARTIST_ID, MediaStore.Audio.Media.DURATION
};
private MediaPlayer mMediaPlayer;
private Cursor mCursor;
private
int mPlayPosition = 0;
public
static
final String PLAY_ACTION = "com.tutor.music.PLAY_ACTION";
public
static
final String PAUSE_ACTION = "com.tutor.music.PAUSE_ACTION";
public
static
final String NEXT_ACTION = "com.tutor.music.NEXT_ACTION";
public
static
final String PREVIOUS_ACTION = "com.tutor.music.PREVIOUS_ACTION";
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return
null;
}
@Override
public
void onCreate() {
super.onCreate();
mMediaPlayer = new MediaPlayer();
//通过一个URI可以获取所有音频文件
Uri MUSIC_URL = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
//这里我过滤了一下,因为我机里有些音频文件是游戏音频,很短
//播放不到一秒钟,我这里作了处理,默认大于10秒的可以看作是歌
mCursor = getContentResolver().query(MUSIC_URL, mCursorCols, "duration > 10000", null, null);
}
@Override
public
void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
String action = intent.getAction();
if(action.equals(PLAY_ACTION)){
play();
}else
if(action.equals(PAUSE_ACTION)){
pause();
}else
if(action.equals(NEXT_ACTION)){
next();
}else
if(action.equals(PREVIOUS_ACTION)){
previous();
}
}
//play the music
public
void play() {
inite();
}
//暂停时,结束服务
public
void pause() {
stopSelf();
}
//上一首
public
void previous() {
if (mPlayPosition == 0) {
mPlayPosition = mCursor.getCount() - 1;
} else {
mPlayPosition--;
}
inite();
}
public
void next() {
if (mPlayPosition == mCursor.getCount() - 1) {
mPlayPosition = 0;
} else {
mPlayPosition++;
}
inite();
}
public
void inite() {
mMediaPlayer.reset();
String dataSource = getDateByPosition(mCursor, mPlayPosition);
String info = getInfoByPosition(mCursor, mPlayPosition);
//用Toast显示歌曲信息
Toast.makeText(getApplicationContext(), info, Toast.LENGTH_SHORT).show();
try {
mMediaPlayer.setDataSource(dataSource);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (IllegalStateException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
//根据位置来获取歌曲位置
public String getDateByPosition(Cursor c,int position){
c.moveToPosition(position);
int dataColumn = c.getColumnIndex(MediaStore.Audio.Media.DATA);
String data = c.getString(dataColumn);
return data;
}
//获取当前播放歌曲演唱者及歌名
public String getInfoByPosition(Cursor c,int position){
c.moveToPosition(position);
int titleColumn = c.getColumnIndex(MediaStore.Audio.Media.TITLE);
int artistColumn = c.getColumnIndex(MediaStore.Audio.Media.ARTIST);
String info = c.getString(artistColumn)+" " + c.getString(titleColumn);
return info;
}
//服务结束时要释放MediaPlayer
public
void onDestroy() {
super.onDestroy();
mMediaPlayer.release();
}
}
package com.tutor.music;import java.io.IOException;import android.app.Service;import android.content.Intent;import android.database.Cursor;import android.media.MediaPlayer;import android.net.Uri;import android.os.IBinder;import android.provider.MediaStore;import android.widget.Toast;public class MusicService extends Service { String[] mCursorCols = new String[] { "audio._id AS _id", // index must match IDCOLIDX below MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.MIME_TYPE, MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.ARTIST_ID, MediaStore.Audio.Media.DURATION }; private MediaPlayer mMediaPlayer; private Cursor mCursor; private int mPlayPosition = 0; public static final String PLAY_ACTION = "com.tutor.music.PLAY_ACTION"; public static final String PAUSE_ACTION = "com.tutor.music.PAUSE_ACTION"; public static final String NEXT_ACTION = "com.tutor.music.NEXT_ACTION"; public static final String PREVIOUS_ACTION = "com.tutor.music.PREVIOUS_ACTION"; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { super.onCreate(); mMediaPlayer = new MediaPlayer(); //通过一个URI可以获取所有音频文件 Uri MUSIC_URL = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; //这里我过滤了一下,因为我机里有些音频文件是游戏音频,很短 //播放不到一秒钟,我这里作了处理,默认大于10秒的可以看作是歌 mCursor = getContentResolver().query(MUSIC_URL, mCursorCols, "duration > 10000", null, null); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); String action = intent.getAction(); if(action.equals(PLAY_ACTION)){ play(); }else if(action.equals(PAUSE_ACTION)){ pause(); }else if(action.equals(NEXT_ACTION)){ next(); }else if(action.equals(PREVIOUS_ACTION)){ previous(); } } //play the music public void play() { inite(); } //暂停时,结束服务 public void pause() { stopSelf(); } //上一首 public void previous() { if (mPlayPosition == 0) { mPlayPosition = mCursor.getCount() - 1; } else { mPlayPosition--; } inite(); } public void next() { if (mPlayPosition == mCursor.getCount() - 1) { mPlayPosition = 0; } else { mPlayPosition++; } inite(); } public void inite() { mMediaPlayer.reset(); String dataSource = getDateByPosition(mCursor, mPlayPosition); String info = getInfoByPosition(mCursor, mPlayPosition); //用Toast显示歌曲信息 Toast.makeText(getApplicationContext(), info, Toast.LENGTH_SHORT).show(); try { mMediaPlayer.setDataSource(dataSource); mMediaPlayer.prepare(); mMediaPlayer.start(); } catch (IllegalArgumentException e1) { e1.printStackTrace(); } catch (IllegalStateException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } //根据位置来获取歌曲位置 public String getDateByPosition(Cursor c,int position){ c.moveToPosition(position); int dataColumn = c.getColumnIndex(MediaStore.Audio.Media.DATA); String data = c.getString(dataColumn); return data; } //获取当前播放歌曲演唱者及歌名 public String getInfoByPosition(Cursor c,int position){ c.moveToPosition(position); int titleColumn = c.getColumnIndex(MediaStore.Audio.Media.TITLE); int artistColumn = c.getColumnIndex(MediaStore.Audio.Media.ARTIST); String info = c.getString(artistColumn)+" " + c.getString(titleColumn); return info; } //服务结束时要释放MediaPlayer public void onDestroy() { super.onDestroy(); mMediaPlayer.release(); }}
第四步:修改Musicdemo.java代码如下(代码比较简洁易懂):
view plaincopy to clipboardprint?
package com.tutor.music;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public
class MusicDemo extends Activity implements OnClickListener {
private Button mPrevious,mPlay,mNext,mPause;
private ComponentName component;
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//oncreate里代码一如既往的少
setupViews();
}
//初始化一些工作
public
void setupViews(){
component = new ComponentName(this,
MusicService.class);
mPrevious = (Button)findViewById(R.id.previous);
mPlay = (Button)findViewById(R.id.play);
mNext = (Button)findViewById(R.id.next);
mPause = (Button)findViewById(R.id.pause);
mPrevious.setOnClickListener(this);
mPlay.setOnClickListener(this);
mNext.setOnClickListener(this);
mPause.setOnClickListener(this);
}
//按钮点击事件响应
public
void onClick(View v) {
if(v == mPrevious){
Intent mIntent = new Intent(MusicService.PREVIOUS_ACTION);
mIntent.setComponent(component);
startService(mIntent);
}else
if(v == mPlay){
Intent mIntent = new Intent(MusicService.PLAY_ACTION);
mIntent.setComponent(component);
startService(mIntent);
}else
if(v == mNext){
Intent mIntent = new Intent(MusicService.NEXT_ACTION);
mIntent.setComponent(component);
startService(mIntent);
}else{
Intent mIntent = new Intent(MusicService.PAUSE_ACTION);
mIntent.setComponent(component);
startService(mIntent);
}
}
}
package com.tutor.music;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MusicDemo extends Activity implements OnClickListener { private Button mPrevious,mPlay,mNext,mPause; private ComponentName component; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //oncreate里代码一如既往的少 setupViews(); } //初始化一些工作 public void setupViews(){ component = new ComponentName(this, MusicService.class); mPrevious = (Button)findViewById(R.id.previous); mPlay = (Button)findViewById(R.id.play); mNext = (Button)findViewById(R.id.next); mPause = (Button)findViewById(R.id.pause); mPrevious.setOnClickListener(this); mPlay.setOnClickListener(this); mNext.setOnClickListener(this); mPause.setOnClickListener(this); } //按钮点击事件响应 public void onClick(View v) { if(v == mPrevious){ Intent mIntent = new Intent(MusicService.PREVIOUS_ACTION); mIntent.setComponent(component); startService(mIntent); }else if(v == mPlay){ Intent mIntent = new Intent(MusicService.PLAY_ACTION); mIntent.setComponent(component); startService(mIntent); }else if(v == mNext){ Intent mIntent = new Intent(MusicService.NEXT_ACTION); mIntent.setComponent(component); startService(mIntent); }else{ Intent mIntent = new Intent(MusicService.PAUSE_ACTION); mIntent.setComponent(component); startService(mIntent); } }}
第五步:修改AndroidManifest.xml,这里只是把我们的MusicService申明进去,不然会报错(第14行代码),代码如下:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutor.music"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MusicDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MusicService" android:exported="true" />
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tutor.music" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MusicDemo" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MusicService" android:exported="true" /> </application> <uses-sdk android:minSdkVersion="7" /></manifest>
第六步:运行上述Android工程,效果如下图所示:
效果1:首界面:
效果2:点击播发按钮开始播放音乐:
效果3:我们可以在设置(Settings)->应用(Applications)->正在运行的服务(Running Services)查看我们启动了一个新的Service:
Ok~今天就先到这里,大家有什么不明白的请留言,Thx~ |
|