303-MP3播放器(行動裝置應用)
1.main的xml檔
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget32"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/app_name"
android:textColor="@drawable/black" />
<ImageButton
android:id="@+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:src="@drawable/play" />
<ImageButton
android:id="@+id/pause_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/stop_button"
android:layout_toRightOf="@+id/start_button"
android:src="@drawable/pause" />
<ImageButton
android:id="@+id/stop_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_toRightOf="@+id/pause_button"
android:src="@drawable/stop" />
</RelativeLayout>
2.GDD03類別程式碼
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class GDD03 extends Activity {
private ImageButton mButtonPlay, mButtonPause, mButtonStop;
private TextView mTextView;
private MediaPlayer mMediaPlayer;
private boolean bIsPaused = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 透過findViewById建構子建立TextView與ImageView物件 */
mTextView = (TextView) this.findViewById(R.id.textView1);
mButtonPlay = (ImageButton) this.findViewById(R.id.start_button);
mButtonPause = (ImageButton) this.findViewById(R.id.pause_button);
mButtonStop = (ImageButton) this.findViewById(R.id.stop_button);
mMediaPlayer = new MediaPlayer();
/* 將音樂以Import的方式儲存在res/raw/always.mp3 */
mMediaPlayer = MediaPlayer.create(this, R.raw.always);
/* 執行播放音樂的按鈕 */
mButtonPlay.setOnClickListener(new ImageButton.OnClickListener() {
/* 覆寫OnClick事件 */
public void onClick(View v) {
// TODO Auto-generated method stub
try {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
}
mMediaPlayer.prepare();
mMediaPlayer.start();
mTextView.setText(R.string.str_start);
} catch (Exception e) {
// TODO Auto-generated catch block
mTextView.setText(e.toString());
e.printStackTrace();
}
}
});
/* 停止播放 */
mButtonStop.setOnClickListener(new ImageButton.OnClickListener() {
// @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mTextView.setText(R.string.str_close);
}
} catch (Exception e) {
// TODO Auto-generated catch block
mTextView.setText(e.toString());
e.printStackTrace();
}
}
});
/* 暫停播放 */
mButtonPause.setOnClickListener(new ImageButton.OnClickListener() {
// @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
if (mMediaPlayer != null) {
if (bIsPaused == false) {
mMediaPlayer.pause();
bIsPaused = true;
mTextView.setText(R.string.str_pause);
} else if (bIsPaused == true) {
mMediaPlayer.start();
bIsPaused = false;
mTextView.setText(R.string.str_start);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
mTextView.setText(e.toString());
e.printStackTrace();
}
}
});
/* 當MediaPlayer.OnCompletionLister會執行的Listener */
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
// @Override
/* 覆寫檔案播出完畢事件 */
public void onCompletion(MediaPlayer arg0) {
try {
mMediaPlayer.release();
} catch (Exception e) {
mTextView.setText(e.toString());
e.printStackTrace();
}
}
});
/* 當MediaPlayer.OnErrorListener會執行的Listener */
mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
// @Override //p530
/* 覆寫錯誤處理事件 */
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
try {
mMediaPlayer.release();
} catch (Exception e) {
mTextView.setText(e.toString());
e.printStackTrace();
}
return false;
}
});
}
@Override
/* 覆寫主程式暫停狀態事件 */
protected void onPause() {
// TODO Auto-generated method stub
try {
mMediaPlayer.release();
} catch (Exception e) {
mTextView.setText(e.toString());
e.printStackTrace();
}
super.onPause();
}
}
沒有留言:
張貼留言