多媒体_视频播放器

//mainActivity

package com.demo.vedio;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class MainActivity extends Activity {
	private MediaPlayer player;
	private SurfaceView sv;
	SurfaceHolder holder;
	int progress = 0;     //定义播放进度
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		sv = (SurfaceView) findViewById(R.id.sv);
		//获取surfaceview的控制器
		holder = (SurfaceHolder) sv.getHolder();
		
		holder.addCallback(new Callback() {

			@Override
			//在surfaceview销毁时调用
			public void surfaceDestroyed(SurfaceHolder holder) {
				if(player != null){
					//停止之前先保存播放进度
					progress = player.getCurrentPosition();
					player.stop();    //停止
					player.release();   //释放资源
					player = null;
				}
				
			}
			
			//在surfaceview创建时调用
			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				
				if(player == null){  //判断play是否为空,空就创建
					player = new MediaPlayer();
					//player.reset();
					try {
						player.setDataSource("mnt/sdcard/2.3gp");
						//指定视频播放在哪个surfaceview
						player.setDisplay(holder);  //返回的是一个Surfaceholder
						player.prepare();
						//跳转到上一次停止的地方继续播放
						player.seekTo(progress);
						player.start();
					} catch (Exception e) {
						e.printStackTrace();
					} 
					
				}
				
				
			}
			
			@Override
			//在surfaceview改变时调用
			public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
			}
	
		});

		
	}
}

//布局文件

<RelativeLayout 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="${relativePackage}.${activityClass}" >


    <SurfaceView
        android:id="@+id/sv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />


</RelativeLayout>



方法2:Videoview 控件


//MainActivit文件

package com.demo.videoview;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.VideoView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		VideoView vv = (VideoView) findViewById(R.id.vv);
		vv.setVideoPath("sdcard/2.3gp");
		vv.start();
				
	}
}

//布局文件

<RelativeLayout 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="${relativePackage}.${activityClass}" >


    <VideoView
        android:id="@+id/vv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       />


</RelativeLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值