|
[基础,代码流]利用VideoView和SurfaceView进行视频播放
发表于 2016-10-11 16:39:49
浏览:7422
|
回复:0
打印
只看该作者
[复制链接]
楼主
本帖最后由 jingjin221 于 2016-10-11 16:52 编辑
MainActivity.java- /*
- /* 利用原生VIDEOVIEW和SURFACEVIEW进行本地文件播放
- /* VIDEOVIEW可用于进行流媒体播放
- /*
- /*
- */
- package com.timestech.xplayer;
- import java.io.File;
- import java.io.IOException;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.content.pm.ActivityInfo;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.WindowManager.LayoutParams;
- import android.widget.MediaController;
- import android.widget.VideoView;
- public class MainActivity extends Activity implements SurfaceHolder.Callback {
- private MediaPlayer xPlayer = null;
- private SurfaceView xSurface = null;
-
- private VideoView xVideoView = null;
-
- private static final String videoUrl = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
- private static final String videoLocalUrl = "http://192.168.1.179:60000";
- @SuppressLint("SdCardPath")
- private static final String videoPath = "/mnt/sdcard/Movies/L00.mp4";
-
- @SuppressLint("SdCardPath")
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //隐藏标题栏
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- //沉浸模式
- getWindow().getDecorView().setSystemUiVisibility(
- View.SYSTEM_UI_FLAG_FULLSCREEN |
- View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
- View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
- // 设置横屏 禁止翻转
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
-
- // 禁止休眠
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
- WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
-
- setContentView(R.layout.activity_main);
- if(false)
- {
- //----------------------------------------videoView播放视频----------------------------------------//
- //xVideoView = (VideoView) findViewById(R.id.videoView);
- MediaController mediaco = new MediaController(this);
- File file = new File(videoPath);
- if(file.exists()){
- Log.e("###XPLAYER###", "ok!");
-
- xVideoView.setVideoURI(Uri.parse(videoLocalUrl));
- //xVideoView.setVideoPath(file.getAbsolutePath());
-
- //VideoView与MediaController进行关联
- xVideoView.setMediaController(mediaco);
-
- mediaco.setMediaPlayer(xVideoView);
-
- //让VideoView获取焦点
- xVideoView.requestFocus();
-
- xVideoView.start();
- }
- else
- Log.e("###XPLAYER###", "Not found this file!");
- }
- else
- {
- //----------------------------------------surfaceView播放视频----------------------------------------//
- xSurface = (SurfaceView)findViewById(R.id.surfaceView);
-
- /*
- LayoutParams lp = (LayoutParams) xSurface.getLayoutParams(); //导致闪退
- lp.width = 1280;
- lp.height = 647;
- xSurface.setLayoutParams(lp);
- */
-
- SurfaceHolder surfaceHolder = xSurface.getHolder(); //SurfaceHolder是SurfaceView的控制接口
- surfaceHolder.addCallback(this); //因为这个类实现了SurfaceHolder.Callback接口,所以回调参数直接this
- //surfaceHolder.setFixedSize(1280, 720); //显示的分辨率,不是surface的大小,不设置为视频默认
- }
- }
- @SuppressLint("SdCardPath")
- public void surfaceCreated(SurfaceHolder holder) {
- //必须在surface创建后才能初始化MediaPlayer,否则不会显示图像
- xPlayer = new MediaPlayer();
- //xPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
- xPlayer.setDisplay(holder);
- //设置显示视频显示在SurfaceView上
- try {
- File file = new File(videoPath);
- if(file.exists()){
- Log.e("###XPLAYER###", "ok!");
- xPlayer.setDataSource(file.getAbsolutePath());
- xPlayer.prepare();
- xPlayer.start();
- }
- else
- Log.e("###XPLAYER###", "Not found this file!");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Override
- public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
- // TODO Auto-generated method stub
-
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder arg0) {
- // TODO Auto-generated method stub
-
- }
-
- protected void onDestroy() {
- if(xPlayer!=null)
- {
- //Activity销毁时停止播放,释放资源。不做这个操作,即使退出还是能听到视频播放的声音
- if(xPlayer.isPlaying()) {
- xPlayer.stop();
- }
- xPlayer.release();
- }
- super.onDestroy();
- }
- }
复制代码
activity_main.xml- <FrameLayout 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" >
- <SurfaceView
- android:id="@+id/surfaceView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center" />
- <!--
- <VideoView
- android:id="@+id/videoView"
- android:layout_width="800dp"
- android:layout_height="600dp"
- android:layout_gravity="center" />
- -->
- </FrameLayout>
复制代码
|
|