1,界面设计的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/music_text" />
<EditText
android:id="@+id/musicTv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My Love.mp3"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*" >
<TableRow >
<Button
android:id="@+id/playBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/play_text" />
<Button
android:id="@+id/pauseBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/pause_text" />
<Button
android:id="@+id/resetBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/reset_text" />
<Button
android:id="@+id/stopBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/stop_text" />
</TableRow>
</TableLayout>
</LinearLayout>
2.activity的代码
package com.example.musicplayer;
import java.io.File;
import java.io.IOException;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Button playBtn, pauseBtn, resetBtn, stopBtn;
EditText musicTv;
String musicName = "";
MediaPlayer player = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
player = new MediaPlayer();
musicTv = (EditText) findViewById(R.id.musicTv);
playBtn = (Button) findViewById(R.id.playBtn);
pauseBtn = (Button) findViewById(R.id.pauseBtn);
resetBtn = (Button) findViewById(R.id.resetBtn);
stopBtn = (Button) findViewById(R.id.stopBtn);
ButtonOnClickListener btnOnClick = new ButtonOnClickListener();
playBtn.setOnClickListener(btnOnClick);
pauseBtn.setOnClickListener(btnOnClick);
resetBtn.setOnClickListener(btnOnClick);
stopBtn.setOnClickListener(btnOnClick);
}
class ButtonOnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
musicName = musicTv.getText().toString();
// 判断有没有sd卡
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory(),
musicName);
// 判断有没有要播放的文件
if (file.exists()) {
try {
switch (v.getId()) {
case R.id.playBtn:
playMusic(file);
break;
case R.id.pauseBtn:
if (player == null)
break;
if (player.isPlaying()) {
player.pause();
pauseBtn.setText(R.string.continue_text);
} else {
player.start();
pauseBtn.setText(R.string.pause_text);
}
break;
case R.id.resetBtn:
if (player.isPlaying()) {
player.seekTo(0);
} else {
playMusic(file);
}
break;
case R.id.stopBtn:
if (player.isPlaying()) {
player.stop();
}
break;
}
} catch (Exception e) {
Log.e("TAG", e.toString());
}
} else {
Toast.makeText(MainActivity.this, R.string.nofile, 1)
.show();
}
} else {
Toast.makeText(MainActivity.this, R.string.SDCarderror, 1)
.show();
}
}
private void playMusic(File file) {
// TODO Auto-generated method stub
if (player != null) {
player.reset();
try {
player.setDataSource(file.getAbsolutePath());
player.prepare();
} catch (Exception e) {
e.printStackTrace();
}
player.start();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}