<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ethan.service"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ServiceDemo2Activity"
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" android:enabled="true"/>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="service............."
/>
<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>
package com.ethan.service;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
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[] cursorCols = new String[] {
"audio._id AS _id",
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 player;
private Cursor cursor;
private int playPosition = 0;
boolean pause = false;
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 intent) {
return null;
}
@Override
public void onCreate() {
player = new MediaPlayer();
//通过一个URI可以获取所有音频文件
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
//这里我过滤了一下,有些音频文件是游戏音频,很短
//播放不到一秒钟,我这里作了处理,默认大于10秒的可以看作是歌
cursor = getContentResolver().query(uri, cursorCols, "duration > 10000", null, null);
super.onCreate();
}
@Override
public void onDestroy() {
player.release();
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
String action = intent.getAction();
if(action.equals(PLAY_ACTION)){
play();
}else if(action.equals(PAUSE_ACTION)){
pause();
pause = true;
}else if(action.equals(NEXT_ACTION)){
next();
}else if(action.equals(PREVIOUS_ACTION)){
previous();
}
super.onStart(intent, startId);
}
private void previous() {
if(playPosition==0) {
playPosition = cursor.getCount()-1;
} else {
playPosition--;
}
init();
}
private void next() {
if (playPosition == cursor.getCount() - 1) {
playPosition = 0;
} else {
playPosition++;
}
init();
}
private void pause() {
// stopSelf(); //结束service
player.pause();
pause = true;
}
private void play() {
//暂停之后回来继续播放
if(pause) {
player.start();//不会接着播放
pause = false;
}
init();
}
private void init() {
player.reset();
String dataSource = getDatalByPosition(cursor,playPosition);
String info = getInfoByPosition(cursor,playPosition);
try {
Toast.makeText(getApplicationContext(), new String(info.getBytes(),"UTF-8"), Toast.LENGTH_SHORT).show();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
try {
player.setDataSource(dataSource);
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private String getInfoByPosition(Cursor c, int playPosition2) {
c.moveToPosition(playPosition2);
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;
}
//获取路径
private String getDatalByPosition(Cursor c, int playPosition2) {
c.moveToPosition(playPosition2);
//歌曲路径
int dataColumn = c.getColumnIndex(MediaStore.Audio.Media.DATA);
String data = c.getString(dataColumn);
return data;
}
}
package com.ethan.service;
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 ServiceDemo2Activity extends Activity implements OnClickListener {
private Button mPrevious, mPlay, mNext, mPause;
private ComponentName component;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViews();
}
private 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);
}
@Override
public void onClick(View v) {
Intent intent = null;
switch(v.getId()) {
case R.id.previous:
intent = new Intent(MusicService.PREVIOUS_ACTION);
intent.setComponent(component);
startService(intent);
break;
case R.id.play:
intent = new Intent(MusicService.PLAY_ACTION);
intent.setComponent(component);
startService(intent);
break;
case R.id.pause:
intent = new Intent(MusicService.PAUSE_ACTION);
intent.setComponent(component);
startService(intent);
break;
case R.id.next:
intent = new Intent(MusicService.NEXT_ACTION);
intent.setComponent(component);
startService(intent);
break;
}
}
}