丁浪建議:學習本實例之前,請掌握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);
}
//當系統恢復後,可以重新讀取出之前保存的狀態值
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
this.fileName=savedInstanceState.getString("fileName");
this.postion=savedInstanceState.getInt("postion");
super.onRestoreInstanceState(savedInstanceState);
}
//當發生意外時,在系統將Activity的進程殺死之前,保存一些狀態值
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("fileName", fileName);
outState.putInt("postion",postion);
super.onSaveInstanceState(outState);
}
//onDestroy方法可以殺掉程序的進程,徹底釋放資源
@Override
protected void onDestroy() {
mediaplayer.release();
super.onDestroy();
}
//如果打電話結束瞭,繼續播放音樂
@Override
protected void onResume() {
if(postion>0&&fileName!=null)
{
try {
play();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaplayer.seekTo(postion);
postion=0;
}
super.onResume();
}
//如果突然來電話或者來短信,Acticity會暫停,停止播放音樂
@Override
protected void onPause() {
if(mediaplayer.isPlaying())
{
postion =mediaplayer.getCurrentPosition();//保存當前播放點
mediaplayer.stop();
}
super.onPause();
}
private final class ButtonClickListener implements View.OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mediaplayer =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生命周期的相關知識。至於頁面佈局部分,采用的是非常傻瓜式的顯示和控制風格。
如果想要開發一款完善的(音樂播放器相關的)產品,還有太多太多的地方需要完善
摘自:編程世界一凡人