建议:学习本实例之前,请掌握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生命周期的相关知识。至于页面布局部分,采用的是非常傻瓜式的显示和控制风格。
如果想要开发一款完善的(音乐播放器相关的)产品,还有太多太多的地方需要完善。