<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.app.MainActivity"
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>
</manifest>
2.activity_main.xml
<?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="音樂播放" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="上一首" />
<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="播放" />
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="下一首" />
<Button
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="暫停" />
</LinearLayout>
</LinearLayout>
3.MusicService.java
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.app.PLAY_ACTION";
public static final String PAUSE_ACTION = "com.app.PAUSE_ACTION";
public static final String NEXT_ACTION = "com.app.NEXT_ACTION";
public static final String PREVIOUS_ACTION = "com.app.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() {
init();
}
// 暫停時,結束服務
public void pause() {
this.stopSelf();
}
// 上一首
public void previous() {
mPlayPosition--;
if (mPlayPosition < 0) {
mPlayPosition = mCursor.getCount() - 1;
}
init();
}
public void next() {
mPlayPosition++;
if (mPlayPosition == mCursor.getCount()) {
mPlayPosition = 0;
}
init();
}
public void init() {
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();
}
}
4.MainActivity.java
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 MainActivity extends Activity implements OnClickListener {
private Button mPrevious, mPlay, mNext, mPause;
private ComponentName component;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
}
}
}
沒有留言:
張貼留言