android 使用MideaPlayer API來播放簡單的應用

採用的Activity來播放音樂


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, HomeActivity!</string>
    <string name="app_name">音樂播放器</string>
    <string name="filename">音樂文件</string>
    <string name="play">播放</string>
    <string name="pause">暫停</string>
    <string name="stop">停止</string>
    <string name="replay">重播</string>
</resources>

<?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/filename"
	    />
	<EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/filenmae"
    />
    
    <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="fill_parent" 
    		android:layout_height="wrap_content" 
    		android:id="@+id/play"
    		android:text="@string/play"
   		 />
   		 <Button
    		android:layout_width="fill_parent" 
    		android:layout_height="wrap_content" 
    		android:id="@+id/pause"
    		android:text="@string/pause"
   		 />
   		 <Button
    		android:layout_width="fill_parent" 
    		android:layout_height="wrap_content" 
    		android:id="@+id/stop"
    		android:text="@string/stop"
   		 />
   		 <Button
    		android:layout_width="fill_parent" 
    		android:layout_height="wrap_content" 
    		android:id="@+id/replay"
    		android:text="@string/replay"
   		 />
   		 
    
    </LinearLayout>
</LinearLayout>

package com.zyq.mp3;

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.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class HomeActivity extends Activity {
	
	private EditText filename;
	private File file;
	private MediaPlayer mediaPlayer;
	private int position;
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mediaPlayer = new MediaPlayer();
        
        filename = (EditText)this.findViewById(R.id.filenmae);
        Button play = (Button)this.findViewById(R.id.play);
        Button pause = (Button)this.findViewById(R.id.pause);
        Button stop = (Button)this.findViewById(R.id.stop);
        Button replay = (Button)this.findViewById(R.id.replay);
        
        ButtonListener l = new ButtonListener();
        play.setOnClickListener(l);
        pause.setOnClickListener(l);
        stop.setOnClickListener(l);
        replay.setOnClickListener(l);
    }
    //模擬電話接聽,觸發Activity onPause生命週期函數
    @Override
    protected void onPause()
    {
    	if(mediaPlayer.isPlaying())
    	{
    		position = mediaPlayer.getCurrentPosition();//保存音樂播放的進度
    		mediaPlayer.stop();
    	}
    	super.onPause();
    }
    
    //模擬電話掛斷,重新回到音樂播放界面,觸發Activity onResume生命週期函數
    @Override
    protected void onResume()
    {
    	try
		{
			if(file!=null && position > 0)
			{
				play();
				mediaPlayer.seekTo(position);
			}
		} catch (IllegalArgumentException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	super.onResume();
    }
    //模擬系統內存不足,KILL掉此Activity,觸發的onSaveInstanceState 方法來保存參數
    @Override
    protected void onSaveInstanceState(Bundle outState)
    {
    	if(file != null)
    	{
    		outState.putString("path", file.getAbsolutePath());
    	}
    	outState.putInt("position", position);
    	super.onSaveInstanceState(outState);
    }
    //模擬系統內存不足,回覆此Activity,觸發的onRestoreInstanceState 重新設置參數
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
    	String path = savedInstanceState.getString("path");
    	if(path != null && "".equals(path))
    	{
    		this.file = new File(path);
    	}
    	this.position = savedInstanceState.getInt("position");
    	super.onRestoreInstanceState(savedInstanceState);
    }
    @Override
    protected void onDestroy()
    {
    	super.onDestroy();
    	
    	mediaPlayer.release();
    }
    private class ButtonListener implements View.OnClickListener
    {
    	private boolean flag;
    	
		@Override
		public void onClick(View v)
		{
			try
			{
				switch (v.getId())
				{
				case R.id.play:
					file = new File(Environment.getExternalStorageDirectory(),filename.getText().toString());
					if(file.exists())
					{
						Toast.makeText(HomeActivity.this, "音樂文件不存在", 1).show();
						return;
					}
					
					play();
					
					break;
				case R.id.pause:
					if(mediaPlayer.isPlaying())
					{
						mediaPlayer.pause();
						flag = true;
						
						((Button)v).setText("繼續");
					}
					else
					{
						if(flag)
						{
							mediaPlayer.start();
							((Button)v).setText("暫停");
						}
					}
					break;
				case R.id.stop:
					if(mediaPlayer.isPlaying())
					{
						mediaPlayer.seekTo(0);
					}
					else
					{
						if(file.exists())
						{
							play();
						}
					}
					break;
				case R.id.replay:
					if(mediaPlayer.isPlaying())
					{
						mediaPlayer.stop();
					}
					break;
				default:
					break;
				}
			} catch (IllegalArgumentException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalStateException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
    	
    }
    //播放音樂
    private void play() throws IllegalArgumentException, IllegalStateException, IOException
    {
    	mediaPlayer.reset();
    	mediaPlayer.setDataSource(file.getAbsolutePath());
		mediaPlayer.prepare();
		mediaPlayer.start();
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章