Android开发入门之在线视频播放器

activity_main.xml:

<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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/video_file_name" />

    <EditText
        android:id="@+id/et_video_file_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="videoplay"
            android:src="@drawable/play" />

        <ImageButton
            android:id="@+id/pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="videoplay"
            android:src="@drawable/pause" />

        <ImageButton
            android:id="@+id/reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="videoplay"
            android:src="@drawable/reset" />

        <ImageButton
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="videoplay"
            android:src="@drawable/stop" />
    </LinearLayout>

    <SurfaceView
        android:id="@+id/sufaceView"
        android:layout_width="match_parent"
        android:layout_height="240dip" />

</LinearLayout>

MainActivity.java:

package cn.leigo.vedioplayer;

import java.io.File;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText mVideoFileName;
	private SurfaceView mSurfaceView;
	private String path;
	private File file;
	private MediaPlayer mediaPlayer;
	private SurfaceHolder surfaceHolder;
	private boolean pause;
	private int position;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mediaPlayer = new MediaPlayer();
		mVideoFileName = (EditText) findViewById(R.id.et_video_file_name);
		mSurfaceView = (SurfaceView) findViewById(R.id.sufaceView);
		// 把输送给surfaceView的视频画面,直接显示到屏幕上,不要维持它自身的缓冲区
		surfaceHolder = mSurfaceView.getHolder();
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		surfaceHolder.setFixedSize(176, 144);
		surfaceHolder.setKeepScreenOn(true);
		surfaceHolder.addCallback(new SurfaceCallback());
	}

	private final class SurfaceCallback implements Callback {

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			if (position > 0 && path != null) {
				play(position);
				position = 0;
			}
		}

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {

		}

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			if (mediaPlayer.isPlaying()) {
				position = mediaPlayer.getCurrentPosition();
				mediaPlayer.stop();
			}
		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mediaPlayer != null) {
			mediaPlayer.release();
			mediaPlayer = null;
		}
	}

	public void videoplay(View v) {
		switch (v.getId()) {
		case R.id.play:
			String fileName = mVideoFileName.getText().toString();
			if (fileName.startsWith("http")) {
				path = fileName;
				play(0);
			} else {
				file = new File(Environment.getExternalStorageDirectory(),
						fileName);
				if (file.exists()) {
					path = file.getAbsolutePath();
					play(0);
				} else {
					path = null;
					Toast.makeText(this, R.string.video_file_not_exists,
							Toast.LENGTH_SHORT).show();
				}
			}
			break;
		case R.id.pause:
			if (mediaPlayer.isPlaying()) {
				mediaPlayer.pause();
				pause = true;
			} else {
				if (pause) {
					mediaPlayer.start();
					pause = false;
				}
			}
			break;
		case R.id.reset:

			if (mediaPlayer.isPlaying()) {
				mediaPlayer.seekTo(0);

			} else {
				if (path != null) {
					play(0);
				}
			}
			break;
		case R.id.stop:
			if (mediaPlayer.isPlaying()) {
				mediaPlayer.stop();
			}
			break;

		default:
			break;
		}
	}

	private void play(int position) {
		try {
			mediaPlayer.reset();
			mediaPlayer.setDataSource(path);
			mediaPlayer.setDisplay(surfaceHolder);
			mediaPlayer.prepare(); // 缓冲
			mediaPlayer.setOnPreparedListener(new MyPreparedListener(position));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private final class MyPreparedListener implements OnPreparedListener {
		private int position;

		public MyPreparedListener(int position) {
			this.position = position;
		}

		@Override
		public void onPrepared(MediaPlayer mp) {
			mp.start(); // 播放视频
			if (position > 0) {
				mp.seekTo(position);
			}
		}

	}

}

Android开发入门之在线视频播放器_第1张图片

你可能感兴趣的:(Android开发,视频播放器,MediaPlay)