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

activity_main.xml:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/video_file_name" />

android:id="@+id/et_video_file_name"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

android:id="@+id/play"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="videoplay"

android:src="@drawable/play" />

android:id="@+id/pause"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="videoplay"

android:src="@drawable/pause" />

android:id="@+id/reset"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="videoplay"

android:src="@drawable/reset" />

android:id="@+id/stop"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="videoplay"

android:src="@drawable/stop" />

android:id="@+id/sufaceView"

android:layout_width="match_parent"

android:layout_height="240dip" />

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);

}

}

}

}

0818b9ca8b590ca3270a3433284dd417.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值