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"
android:orientation="vertical"
tools:context=".MainActivity" > <EditText
android:id="@+id/et_path"
android:hint="请输入视频文件"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="/sdcard/mm.mp4"
/> <SeekBar
android:id="@+id/sb"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/bt_play"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="播放"
/>
<Button
android:id="@+id/bt_pause"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="暂停"
/>
<Button
android:id="@+id/bt_replay"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="重播"
/>
<Button
android:id="@+id/bt_stop"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="停止"
/> </LinearLayout> <SurfaceView
android:id="@+id/sv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/> </LinearLayout>
2、android代码
package com.example.videoplay; import java.io.File; import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{
private EditText et_path;
private Button bt_play,bt_pause,bt_replay,bt_stop;
private MediaPlayer mediaPlayer;
private SurfaceView sv;
private int currentPosition;
private SeekBar sb;
private boolean isplaying;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); et_path = (EditText) findViewById(R.id.et_path);
bt_play = (Button) findViewById(R.id.bt_play);
bt_pause = (Button) findViewById(R.id.bt_pause);
bt_replay = (Button) findViewById(R.id.bt_replay);
bt_stop = (Button) findViewById(R.id.bt_stop); sb = (SeekBar) findViewById(R.id.sb); //监听进度条拖动改变状态
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
//当拖动停止时的调用的方法
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int process = sb.getProgress();
if(mediaPlayer != null && mediaPlayer.isPlaying()){
mediaPlayer.seekTo(process);
} } @Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub } @Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub }
}); sv = (SurfaceView) findViewById(R.id.sv); //为什么要增加这个回调?主要是监控holder的变化的;因为如果你按一下小房子,声音仍在,而holder容易被销毁,再次打开时,看不到画面
sv.getHolder().addCallback(new Callback(){ @Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// TODO Auto-generated method stub
System.out.println("holder被改变了!"); } @Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
System.out.println("holder被创建了!");
if(currentPosition > 0){
play(currentPosition);
} } @Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
System.out.println("holder被销毁了!"); if(mediaPlayer != null && mediaPlayer.isPlaying()){
currentPosition = mediaPlayer.getCurrentPosition();//获取当前所播放的位置
stop();//holder被销毁时,就stop媒体
} } }); bt_play.setOnClickListener(this);
bt_pause.setOnClickListener(this);
bt_replay.setOnClickListener(this);
bt_stop.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bt_play:
play(0);
break;
case R.id.bt_pause:
pause();
break;
case R.id.bt_replay:
replay();
break;
case R.id.bt_stop:
stop();
break;
} } private void pause() {
if("继续".equals(bt_pause.getText().toString().trim())){
mediaPlayer.start();
bt_pause.setText("暂停");
return;
}
if(mediaPlayer != null && mediaPlayer.isPlaying()){
mediaPlayer.pause();
bt_pause.setText("继续");
return;
} } private void replay() {
if(mediaPlayer != null && mediaPlayer.isPlaying()){
mediaPlayer.seekTo(0);
return;
}
play(0); } private void stop() {
// TODO Auto-generated method stub
if(mediaPlayer != null && mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
bt_play.setEnabled(true);
isplaying = false;
}
} private void play(int currentPosition2){
String path = et_path.getText().toString().trim();
//File file = new File(path);
//if(file.exists() && file.length() > 0){
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(sv.getHolder());//设置video以SurfaceHolder播放
mediaPlayer.setDataSource(path);
//mediaPlayer.seekTo(currentPosition2);//不能把这定位放在此,否则播放不了
mediaPlayer.prepare();
mediaPlayer.seekTo(currentPosition2);
mediaPlayer.start();
//获取视频总长度
int max = mediaPlayer.getDuration();
sb.setMax(max);
new Thread(){
public void run(){
isplaying = true;
while(isplaying){
int position = mediaPlayer.getCurrentPosition();
sb.setProgress(position);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
bt_play.setEnabled(true);
isplaying = false; }
});
bt_play.setEnabled(false);
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(this, "播放失败!", 0).show();
e.printStackTrace();
}
//}else{
//Toast.makeText(this, "音频文件不存在!", 0).show();
//}
} }
注意:
1)如果要播放互联网文件,则要加入网络权限: <uses-permission android:name="android.permission.INTERNET"/>
2)要注意和音频文件的播放代码进行比较,音频代码为:http://www.cnblogs.com/zhongyinghe/p/5354040.html