main.xml
<?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"
>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/edt"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
>
<Button
android:id="@+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放"/>
<Button
android:id="@+id/btnPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"/>
<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止"/>
</LinearLayout>
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="fill_parent"
android:layout_height="240dp"/>
</LinearLayout>
MainActivity
package com.sun.activity;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener{
MediaPlayer mediaPlayer;
SurfaceHolder surfaceHolder;
EditText edt;
String path;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonPause = (Button) findViewById(R.id.btnPause);
Button buttonPlay = (Button) findViewById(R.id.btnPlay);
Button buttonStop = (Button) findViewById(R.id.btnStop);
buttonPause.setOnClickListener(this);
buttonPlay.setOnClickListener(this);
buttonStop.setOnClickListener(this);
edt=(EditText) findViewById(R.id.edt);
mediaPlayer= new MediaPlayer();
SurfaceView surfaceView= (SurfaceView) findViewById(R.id.surfaceView);
surfaceHolder=surfaceView.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder.setFixedSize(176, 144);
surfaceHolder.setKeepScreenOn(true);
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnPlay:
try {
File file=new File(Environment.getExternalStorageDirectory(),edt.getText().toString());
Log.v("aaaaa", file.getPath());
if(file.exists())
{
path=file.getAbsolutePath();
play();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
class Prepared implements OnPreparedListener{
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
}
private void play() {
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(path);
mediaPlayer.setDisplay(surfaceHolder);
mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(new Prepared());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}