Android笔记:视屏播放、VideoView、surfaceView,简易视频播放

发布时间:2020-06-26 17:12:09 作者:glblong
来源:网络 阅读:4375

一、VideoView方法

1.activity_video.xml

<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=".VideoActivity" >

    <VideoView
        android:id="@+id/video_videoView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>


2.代码

package com.example.vediotest;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoActivity extends Activity
{
    
    private VideoView videoView;
    private Uri mUri;
    private int mPositionWhenPaused;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置成全屏模式
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// 强制为横屏
        setContentView(R.layout.activity_video);
        
        String url = "http://videofile.xxxx.cn/Upload/Video/File/20140411/201404110228168972.mp4";
        // String url =
        // "http://player.youku.com/player.php/sid/XNDYwOTEzNzQ4/v.swf";
        
        mUri = Uri.parse(url);
        
        videoView = (VideoView) findViewById(R.id.video_videoView);
        MediaController mediaController = new MediaController(this);
        videoView.setMediaController(mediaController);
        
        // videoView.setVideoPath("/sdcard/xyx.3gp");
        // videoView.setVideoURI(mUri);
        // videoView.requestFocus();
        // videoView.start();
        
    }
    
    public void onStart()
    {
        // Play Video
        videoView.setVideoURI(mUri);
        videoView.start();
        
        super.onStart();
    }
    
    public void onPause()
    {
        // Stop video when the activity is pause.
        mPositionWhenPaused = videoView.getCurrentPosition();
        videoView.stopPlayback();
        
        super.onPause();
    }
    
    public void onResume()
    {
        // Resume video player
        if (mPositionWhenPaused >= 0)
        {
            videoView.seekTo(mPositionWhenPaused);
            mPositionWhenPaused = -1;
        }
        
        super.onResume();
    }
    
    public boolean onError(MediaPlayer player, int arg1, int arg2)
    {
        return false;
    }
    
    public void onCompletion(MediaPlayer mp)
    {
        this.finish();
    }
}



二、surfaceView方法

(一)

1.activity_video_surface.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="标题" />

    <SurfaceView
        android:id="@+id/surfaceVideo_surfaceView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >
    </SurfaceView>

</LinearLayout>


2.代码

package com.example.vediotest;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class SurfaceVideoActivity extends Activity implements Callback, OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener
{
    
    private int width = 0;
    private int height = 0;
    private MediaPlayer mMediaPlayer = null;
    private SurfaceView mSurfaceView = null;
    private SurfaceHolder holder = null;
    private String path = "";
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_surface);
        
        mSurfaceView = (SurfaceView) this.findViewById(R.id.surfaceVideo_surfaceView);
        holder = mSurfaceView.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);// 设置风格
        
    }
    
    public void playVedio()
    {
        try
        {
            path = android.os.Environment.getExternalStorageDirectory() + "/moto_0012.3gp";
            mMediaPlayer = new MediaPlayer();
            // mMediaPlayer.setDataSource(path);
            
          String url = "http://videofile.housebox.cn/Upload/Video/File/20140411/201404110228168972.mp4";
//            String url = "http://player.youku.com/player.php/sid/XNDYwOTEzNzQ4/v.swf";
            
            mMediaPlayer.setDataSource(this, Uri.parse(url));
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setDisplay(holder);
            mMediaPlayer.prepare();// 准备
            Log.e("TAG-Duration", mMediaPlayer.getDuration() + "");
            mMediaPlayer.setOnBufferingUpdateListener(this);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnPreparedListener(this);
        }
        catch (Exception ex)
        {
            
        }
    }
    
    public void onBufferingUpdate(MediaPlayer mp, int percent)
    {
        // TODO Auto-generated method stub
        Log.i("TAG-onBufferingUpdate", percent + "|" + mMediaPlayer.getCurrentPosition());
    }
    
    public void onCompletion(MediaPlayer mp)
    {
        // TODO Auto-generated method stub
        Log.i("TAG-onCompletion", "Completion");
    }
    
    public void onPrepared(MediaPlayer mp)
    {
        // TODO Auto-generated method stub
        width = mMediaPlayer.getVideoWidth();
        height = mMediaPlayer.getVideoHeight();
        if (width != 0 && height != 0)
        {
            holder.setFixedSize(width, height);// 设置视频高宽
            mMediaPlayer.start();
            Log.i("TAG-Duration2", mMediaPlayer.getDuration() + "");
        }
    }
    
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
    {
        // TODO Auto-generated method stub
        
    }
    
    public void surfaceCreated(SurfaceHolder holder)
    {
        // TODO Auto-generated method stub
        playVedio();
    }
    
    public void surfaceDestroyed(SurfaceHolder holder)
    {
        // TODO Auto-generated method stub
        Log.i("TAG-surfaceDestroyed", "surfaceDestroyed");
    }
    
    @Override
    protected void onPause()
    {
        super.onPause();
        if (mMediaPlayer != null)
        {
            if (mMediaPlayer.isPlaying())
            {
                mMediaPlayer.stop();
            }
            mMediaPlayer.reset();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }
    
}


(二)

  1. activity_video_surface2.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <SurfaceView
        android:id="@+id/surface2_surfaceView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >
    </SurfaceView>

</RelativeLayout>


2.代码

package com.example.vediotest;

import java.io.IOException;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;

public class SurfaceVideo2Activity extends Activity implements OnBufferingUpdateListener, OnCompletionListener, MediaPlayer.OnPreparedListener,
        SurfaceHolder.Callback
{
    private MediaPlayer mediaPlayer;
    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;
    private int videoWidth;
    private int videoHeight;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_video_surface2);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// 强制为横屏
        this.surfaceView = (SurfaceView) this.findViewById(R.id.surface2_surfaceView);
        this.surfaceHolder = this.surfaceView.getHolder();
        this.surfaceHolder.addCallback(this);
        this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        Log.v("cat", ">>>create ok.");
        
    }
    
    private void playVideo() throws IllegalArgumentException, IllegalStateException, IOException
    {
      String url = "http://videofile.housebox.cn/Upload/Video/File/20140411/201404110228168972.mp4";
//        String url = "http://player.youku.com/player.php/sid/XNDYwOTEzNzQ4/v.swf";
        
        
        this.mediaPlayer = new MediaPlayer();
//        this.mediaPlayer.setDataSource("/sdcard/daoxiang.3gp");
        this.mediaPlayer.setDataSource(this, Uri.parse(url));
        
        this.mediaPlayer.setDisplay(this.surfaceHolder);
        this.mediaPlayer.prepare();
        this.mediaPlayer.setOnBufferingUpdateListener(this);
        this.mediaPlayer.setOnPreparedListener(this);
        this.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        Log.i("mplayer", ">>>play video");
    }
    
    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)
    {
        Log.i("cat", ">>>surface changed");
    }
    
    @Override
    public void surfaceCreated(SurfaceHolder holder)
    {
        try
        {
            this.playVideo();
        }
        catch (Exception e)
        {
            Log.i("cat", ">>>error", e);
        }
        Log.i("cat", ">>>surface created");
    }
    
    @Override
    public void surfaceDestroyed(SurfaceHolder holder)
    {
        Log.v("mplayer", ">>>surface destroyed");
    }
    
    @Override
    public void onCompletion(MediaPlayer arg0)
    {
        // TODO Auto-generated method stub
    }
    
    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent)
    {
        // TODO Auto-generated method stub
    }
    
    @Override
    public void onPrepared(MediaPlayer arg0)
    {
        this.videoWidth = this.mediaPlayer.getVideoWidth();
        this.videoHeight = this.mediaPlayer.getVideoHeight();
        if (this.videoHeight != 0 && this.videoWidth != 0)
        {
            this.surfaceHolder.setFixedSize(this.videoWidth, this.videoHeight);
            this.mediaPlayer.start();
        }
    }
    
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        if (this.mediaPlayer != null)
        {
            this.mediaPlayer.release();
            this.mediaPlayer = null;
        }
    }
    
}




参考资料:

1.http://blog.csdn.net/cynhafa/article/details/6400050

2.http://aina-hk55hk.iteye.com/blog/706060





推荐阅读:
  1. 详解python播放音频的三种方法
  2. C#开发可播放摄像头及任意格式视频的播放器

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

播放 android 视屏

上一篇:学习Nagios(1)监控门禁

下一篇:怎么解决linux中phpcms后台验证码不显示问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》