Linux教程網
實現功能,播放,暫停,重置,進度條的使用
String文件
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
-
- <string name="app_name">MusicPlayer</string>
- <string name="music_name">歌曲:</string>
- <string name="play_text">播放</string>
- <string name="pause_text">暫停</string>
- <string name="continue_text">繼續</string>
- <string name="reset_text">重置</string>
- <string name="stop_text">關閉</string>
- <string name="notfoundfile_text">媒體文件不存在</string>
- <string name="notfoundSdcard_text">SDCard不存在</string>
-
- </resources>
布局:
- <?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="@string/music_name" />
-
- <EditText
- android:id="@+id/musicEt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="a.mp3"
- />
- <SeekBar
- android:id="@+id/seekBar"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:max="10000"
- />
- <TableLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:stretchColumns="*" >
-
- <TableRow >
-
- <Button
- android:id="@+id/playBtn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/play_text" />
-
- <Button
- android:id="@+id/pauseBtn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/pause_text" />
-
- <Button
- android:id="@+id/resetBtn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/reset_text" />
-
- <Button
- android:id="@+id/stopBtn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/stop_text" />
- </TableRow>
- </TableLayout>
- </LinearLayout>
Java源碼:
- package cn.class3g.musicplayer;
-
- import java.io.File;
- import java.io.IOException;
-
- import android.app.Activity;
-
- import android.media.MediaPlayer;
- import android.os.Bundle;
- import android.os.Environment;
- import android.os.Handler;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.SeekBar;
- import android.widget.SeekBar.OnSeekBarChangeListener;
- import android.widget.Toast;
-
- public class MusicPlayerActivity extends Activity implements OnClickListener,
- OnSeekBarChangeListener {
-
- private static final String TAG = "MusicPlayerActivity";
- EditText musicEt;
- Button playBtn, pauseBtn, resetBtn, stopBtn;
- Handler handler = null;
- Handler fHandler = null;
- MediaPlayer player = null;
-
- SeekBar seekbar = null;
-
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- player = new MediaPlayer();
-
- findViews();
-
- }
-
- private void findViews() {
- musicEt = (EditText) this.findViewById(R.id.musicEt);
- playBtn = (Button) this.findViewById(R.id.playBtn);
- pauseBtn = (Button) this.findViewById(R.id.pauseBtn);
- resetBtn = (Button) this.findViewById(R.id.resetBtn);
- stopBtn = (Button) this.findViewById(R.id.stopBtn);
- seekbar = (SeekBar) this.findViewById(R.id.seekBar);
- playBtn.setOnClickListener(this);
- pauseBtn.setOnClickListener(this);
- resetBtn.setOnClickListener(this);
- stopBtn.setOnClickListener(this);
- seekbar.setOnSeekBarChangeListener(this);
- }
-
- public void onClick(View v) {
- String fileName = musicEt.getText().toString();
-
- // 確定sdcard狀態是否為已加載
- if (Environment.getExternalStorageState().equals(
- Environment.MEDIA_MOUNTED)) {
- File file = new File(Environment.getExternalStorageDirectory(),
- fileName);
-
- // 判斷所播放的媒體文件是否存在
- if (file.exists()) {
-
- try {
- switch (v.getId()) {
- case R.id.playBtn:
- playMusic(file);
- break;
- case R.id.pauseBtn:
- if (player.isPlaying()) {
- player.pause();
- pauseBtn.setText(R.string.continue_text);
- } else {
- player.start();
- pauseBtn.setText(R.string.pause_text);
- }
- break;
- case R.id.resetBtn:
- if (player.isPlaying()) {
- player.seekTo(0);
- } else {
- playMusic(file);
- }
- break;
- case R.id.stopBtn:
- if (player.isPlaying()) {
- player.stop();
- }
- break;
- }
- } catch (Exception e) {
- Log.e(TAG, e.toString());
- }
- } else {
- Toast.makeText(this, R.string.notfoundfile_text,
- Toast.LENGTH_SHORT).show();
- }
- } else {
- Toast.makeText(this, R.string.notfoundSdcard_text,
- Toast.LENGTH_SHORT).show();
- }
- }
-
- protected void onDestroy() {
- if (player != null) {
- if (player.isPlaying()) {
- player.stop();
- }
- player.release();
- }
- super.onDestroy();
- }
-
- protected void onPause() {
- if (player != null) {
- if (player.isPlaying()) {
- player.pause();
- }
- }
- super.onPause();
- }
-
- protected void onResume() {
- if (player != null) {
- if (!player.isPlaying()) {
- player.start();
- }
- }
- super.onResume();
- }
-
- private void playMusic(File file) throws IllegalArgumentException,
- IllegalStateException, IOException {
- if (player != null) {
- player.reset();
- player.setDataSource(file.getAbsolutePath());
- player.prepare();
- player.start();
-
- seekbar.setMax(player.getDuration());
- Log.i(TAG, String.valueOf(player.getDuration()));
-
- new Thread(new seekBar()).start();
- }
- }
-
- class seekBar implements Runnable {
-
- int position = 0;
-
- public void run() {
- while (player != null) {
- position = player.getCurrentPosition();
- seekbar.setProgress(position);
-
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- Log.e(TAG, e.toString());
- }
- }
-
- }
-
- }
-
- public void onProgressChanged(SeekBar seekBar, int progress,
- boolean fromUser) {
- if (fromUser)
- player.seekTo(progress);
- }
-
- public void onStartTrackingTouch(SeekBar seekBar) {
-
- }
-
- public void onStopTrackingTouch(SeekBar seekBar) {
-
- }
- }
Copyright ©
Linux教程網 All Rights Reserved