(Android小应用)音乐播放器
这个小应用确实是小哈,仅做为学习之用,了解MediaPlayer类,以后再做个稍微大点的,打算从网络上直接下载歌曲和歌词文件,并动态显示歌词,这个小应用先不实现那些吧,以后补上……
界面:
<?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="音乐文件路径"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/filename"
android:text="test.mp3"
/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/startButton"
android:text="播放"
android:textSize="15px"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/startButton"
android:layout_alignTop="@id/startButton"
android:layout_marginLeft="10px"
android:id="@+id/pauseButton"
android:text="暂停"
android:textSize="15px"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/pauseButton"
android:layout_alignTop="@id/pauseButton"
android:layout_marginLeft="10px"
android:id="@+id/stopButton"
android:text="停止"
android:textSize="15px"
/>
</RelativeLayout>
</LinearLayout>
很简单的布局
AudioActivity:
package com.studio.audio;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AudioActivity extends Activity {
/** Called when the activity is first created. */
private MediaPlayer mediaPlayer = null;
private Button startButton = null;
private Button pauseButton = null;
private Button stopButton = null;
private EditText editText = null;
private boolean isPause = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.filename);
startButton = (Button) findViewById(R.id.startButton);
pauseButton = (Button) findViewById(R.id.pauseButton);
stopButton = (Button) findViewById(R.id.stopButton);
startButton.setOnClickListener(listener);
pauseButton.setOnClickListener(listener);
stopButton.setOnClickListener(listener);
mediaPlayer = new MediaPlayer();
mediaPlayer
.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
Toast.makeText(AudioActivity.this, "播放完成",
Toast.LENGTH_LONG).show();
}
});
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button button = (Button) v;
switch (button.getId()) {
case R.id.startButton:
try {
String fileName = editText.getText().toString();
mediaPlayer.reset();
mediaPlayer.setDataSource("/sdcard/" + fileName);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.pauseButton:
try {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
} catch (Exception e) {
// TODO: handle exception
}
break;
case R.id.stopButton:
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
break;
default:
break;
}
}
};
@Override
protected void onPause() {
// TODO Auto-generated method stub
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
isPause = true;
}
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
if (isPause) {
mediaPlayer.start();
isPause = false;
}
super.onResume();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
mediaPlayer.release();
mediaPlayer = null;
super.onDestroy();
}
}
因为我们这里用的sdcard里面的音乐文件,所以我们得先往sdcard里面放首音乐
选择“sdcard",然后点击右上角第二个手机图标(指向手机里面的那个)就可以将本地的文件传到sdcard里面,注意名字的关联,因为程序里面定义的名字是test.mp3,呵呵,本来功能就不复杂的,仅作为学习之用