建議:學習本執行個體之前,請掌握Activity的生命週期相關的事件和方法,這樣學習效果會更好。
本執行個體僅供參考學習,並非一款非常完善的產品。由於時間和本人技術有限,不足或者錯誤之處敬請諒解。希望熱心的網友能夠繼續完善。
下面是Activity部分代碼(我一般都會有詳細注釋):
package cn.chaoyang.activity;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.text.BoringLayout.Metrics;import android.view.View;import android.widget.Button;import android.widget.EditText;//學習本執行個體之前,請掌握Activity的生命週期和相關的方法,這樣學習效果會更好。public class MainActivity extends Activity {private MediaPlayer mediaplayer;private EditText txtName;private int postion;private String fileName; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ButtonClickListener listener =new ButtonClickListener(); txtName =(EditText)this.findViewById(R.id.inputName); Button btnPlay =(Button)this.findViewById(R.id.btnPlay); Button btnPause =(Button) this.findViewById(R.id.btnPause); Button btnStop =(Button) this.findViewById(R.id.btnStop); Button btnResart=(Button) this.findViewById(R.id.btnRestart); btnPlay.setOnClickListener(listener); btnPause.setOnClickListener(listener); btnStop.setOnClickListener(listener); btnResart.setOnClickListener(listener); } //當系統復原後,可以重新讀取出之前儲存的狀態值 @Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState) { this.fileName=savedInstanceState.getString("fileName"); this.postion=savedInstanceState.getInt("postion"); super.onRestoreInstanceState(savedInstanceState);} //當發生意外時,在系統將Activity的進程殺死之前,儲存一些狀態值@Overrideprotected void onSaveInstanceState(Bundle outState) {outState.putString("fileName", fileName);outState.putInt("postion",postion);super.onSaveInstanceState(outState);} //onDestroy方法可以殺掉程式的進程,徹底釋放資源@Overrideprotected void onDestroy() {mediaplayer.release();super.onDestroy();} //如果打電話結束了,繼續播放音樂@Overrideprotected void onResume() {if(postion>0&&fileName!=null){try {play();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}mediaplayer.seekTo(postion);postion=0;}super.onResume();} //如果突然來電話或者來簡訊,Acticity會暫停,停止播放音樂@Overrideprotected void onPause() {if(mediaplayer.isPlaying()){postion =mediaplayer.getCurrentPosition();//儲存當前播放點mediaplayer.stop();}super.onPause();}private final class ButtonClickListener implements View.OnClickListener { @Overridepublic void onClick(View v) {// TODO Auto-generated method stubmediaplayer =new MediaPlayer();Button button =(Button) v ;try {switch (v.getId()){//播放case R.id.btnPlay:if(!mediaplayer.isPlaying()){play();}break;//暫停case R.id.btnPause://如果現正播放,則按下按鈕後暫停.且按鈕上的文本顯示為"繼續“if(mediaplayer.isPlaying()){mediaplayer.pause();button.setText(R.string.txtContinue);//設定按鈕文本}else{//如果是暫停狀態,按下按鈕後繼續播放 //play();}break;//停止case R.id.btnStop:if(mediaplayer.isPlaying()){mediaplayer.stop();}break;//重複case R.id.btnRestart:if(mediaplayer.isPlaying()){mediaplayer.seekTo(0);}else{play();}break;}}catch (Exception e) {// TODO: handle exception}} }private void play() throws IOException{//獲得音樂檔案的絕對路徑 fileName=txtName.getText().toString();File file =new File(Environment.getExternalStorageDirectory(),fileName); mediaplayer.reset();//歸位mediaplayer.setDataSource(file.getAbsolutePath());//設定需要播放的資料來源 mediaplayer.prepare();mediaplayer.start();}}
下面是軟體布局檔案代碼,很簡單的線性布局
<?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="@string/hello" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/labName" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/inputName" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/play" android:id="@+id/btnPlay" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pause" android:id="@+id/btnPause" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop" android:id="@+id/btnStop" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/restart" android:id="@+id/btnRestart" /> </LinearLayout></LinearLayout>
下面是資源檔string.xml代碼
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">mp3播放器</string> <string name="labName">輸入歌名</string> <string name="play">播放</string> <string name="pause">暫停</string> <string name="stop">停止</string> <string name="restart">重複</string> <string name="txtContinue">繼續</string></resources>
本執行個體的目的,是為了熟悉android中音頻介面的使用及相關操作,鞏固Activity生命週期的相關知識。至於頁面配置部分,採用的是非常傻瓜式的顯示和控制風格。
如果想要開發一款完善的(音樂播放器相關的)產品,還有太多太多的地方需要完善。