视频播放功能模块

来源:互联网 发布:且夫知不知 编辑:程序博客网 时间:2024/06/06 17:55

终于可以静下心来写一下日志了,下面是做的一块关于视频播放的功能,主要是要用到插件AVProQuickTime,已经用过多次,还是非常不错的,不过这个项目是基于4.3版本的,比较老,属于在维护。
主摄像机上挂载AVProQuickTimeManager即可,然后新建一个对象命名为MovieObject,然后挂载AVProQuickTimeGUIDisplay,和AVProQuickTimeMovie,但是在后者中,我们对视频资源的地址进行了更改,方便后面更换,更改后代码如下:

using UnityEngine;using System;using System.IO;using System.Collections;//-----------------------------------------------------------------------------// Copyright 2012 RenderHeads Ltd.  All rights reserverd.//-----------------------------------------------------------------------------[AddComponentMenu("AVPro QuickTime/Movie")]public class AVProQuickTimeMovie : MonoBehaviour{    protected AVProQuickTime _moviePlayer;    public AVProQuickTimePlugin.MovieSource _source = AVProQuickTimePlugin.MovieSource.LocalFile;    public string _folder = "";  // public string _filename = "Movie.mov";    public string _filename;    public bool _loop = false;    public AVProQuickTimePlugin.PixelFormat _colourFormat = AVProQuickTimePlugin.PixelFormat.YCbCr_HD;    public bool _loadOnStart = true;    public bool _playOnStart = true;    public bool _loadFirstFrame = true;    public bool _editorPreview = false;    public AVProQuickTime.UpdateMode _updateMode = AVProQuickTime.UpdateMode.UnityTexture;    public float _volume = 1.0f;    [NonSerializedAttribute]    public byte[] _movieData;    public Texture OutputTexture    {        get { if (_moviePlayer != null) return _moviePlayer.OutputTexture; return null; }    }    public AVProQuickTime MovieInstance    {        get { return _moviePlayer; }    }    public void Start()    {        _folder = MainMessage.folderName;        _filename = MainMessage.videoName;        if (null == FindObjectOfType(typeof(AVProQuickTimeManager)))        {            throw new Exception("You need to add AVProQuickTimeManager component to your scene.");        }        if (_loadOnStart)        {            LoadMovie();        }    }    public void LoadMovie()    {        if (_moviePlayer == null)            _moviePlayer = new AVProQuickTime();        bool loaded = false;        switch (_source)        {            case AVProQuickTimePlugin.MovieSource.LocalFile:                loaded = _moviePlayer.StartFromFile(Path.Combine(_folder, _filename), _loop, _colourFormat, _updateMode);                break;            case AVProQuickTimePlugin.MovieSource.URL:                loaded = _moviePlayer.StartFromURL(Path.Combine(_folder, _filename), _loop, _colourFormat, _updateMode);                break;            case AVProQuickTimePlugin.MovieSource.Memory:                if (_movieData != null)                {                    loaded = _moviePlayer.StartFromMemory(_movieData, _filename, _loop, _colourFormat, _updateMode);                }                break;        }        if (loaded)        {            _moviePlayer.Volume = _volume;        }        else        {            Debug.LogWarning("[AVProQuickTime] Couldn't load movie " + _filename);            UnloadMovie();        }    }    public void Update()    {        _volume = Mathf.Clamp01(_volume);        if (_moviePlayer != null)        {            if (_volume != _moviePlayer.Volume)                _moviePlayer.Volume = _volume;            if (!_moviePlayer.IsPlaying)            {                if (_loadFirstFrame)                {                    if (_moviePlayer.PlayState == AVProQuickTime.PlaybackState.Loaded)                    {                        _moviePlayer.Frame = 0;                        _loadFirstFrame = false;                    }                }                if (_playOnStart)                {                    if ((int)_moviePlayer.PlayState >= (int)AVProQuickTime.PlaybackState.Loaded && _moviePlayer.LoadedSeconds > 0f)                    {                        _moviePlayer.Play();                        _playOnStart = false;                    }                }                       }            _moviePlayer.Update(false);        }    }    public void Play()    {        if (_moviePlayer != null)            _moviePlayer.Play();    }    public void Pause()    {        if (_moviePlayer != null)            _moviePlayer.Pause();    }       public void UnloadMovie()    {        if (_moviePlayer != null)        {            _moviePlayer.Dispose();            _moviePlayer = null;        }    }    public void OnDestroy()    {        UnloadMovie();    }}

最后是关于视频快进暂停,进度条的相关控制,只需要下面的一个脚本就行,然后给相应的变量赋值,直接在inspector中拖到对应位置比较方便。

using UnityEngine;using System.Collections;using System.Runtime.InteropServices;public class AVProQuickTimePlayVideoDemo : MonoBehaviour{    public AVProQuickTimeMovie _movie;    public AVProQuickTimeGUIDisplay _display;    private bool isPlay = true;    public GUISkin mySkin;    private bool isShow = false;    private float oldScreenWidth = 1920;    private float oldScreenHeight = 1080;    private float sliderWidth;    private float sliderHeitht;    public Texture2D backLabel;    public Texture2D replay;    public Texture2D back;    public Texture2D pause;    private bool isQuite=false;    private bool isShowSlider = false;    void Awake()    {        sliderWidth = (Screen.width / oldScreenWidth) * 1200;        sliderHeitht = (Screen.height / oldScreenHeight) * 30;            }    void Update()    {        AVProQuickTime moviePlayer = _movie.MovieInstance;        if (Input.mousePosition.y < 40.0f)        {            isShowSlider = true;        }        else            isShowSlider = false;        if (Input.GetMouseButtonDown(1))        {            isQuite = !isQuite;        }        if (isQuite)        {            moviePlayer.Pause();        }        if (Input.GetMouseButtonDown(0) && isShow == false && (Input.mousePosition.y > 40.0f))        {            isPlay = !isPlay;        }        if (isPlay == true)        {            moviePlayer.Play();        }        if (isPlay == false)        {          //  moviePlayer.Pause();        }                                      // 暂停功能        if (Input.GetKey(KeyCode.LeftArrow))        {            isShowSlider = true;            moviePlayer.Pause();            if (moviePlayer.Frame > 0)                moviePlayer.Frame-=15;        }        if (Input.GetKey(KeyCode.RightArrow))        {            isShowSlider = true;            moviePlayer.Pause();            moviePlayer.Frame+=10;        }        if (moviePlayer.Frame!=0)        {            if (moviePlayer.Frame == moviePlayer.FrameCount || moviePlayer.Frame == moviePlayer.FrameCount - 1)        {            moviePlayer.Pause();            isShow = true;        }        else            isShow = false;                }    }    public void OnGUI()    {        GUI.skin=mySkin;        AVProQuickTime moviePlayer = _movie.MovieInstance;        float position = moviePlayer.PositionSeconds;        if (isShowSlider)        {            float newPosition = GUI.HorizontalSlider(new Rect(Screen.width / 2 - sliderWidth / 2, Screen.height - sliderHeitht, sliderWidth, sliderHeitht), position, 0.0f, moviePlayer.DurationSeconds, "SliderOld", "");   //返回时间            float xnewPosition = GUI.HorizontalSlider(new Rect(Screen.width / 2 - sliderWidth / 2, Screen.height - sliderHeitht, newPosition * (sliderWidth / moviePlayer.DurationSeconds), sliderHeitht), position, 0.0f, moviePlayer.DurationSeconds, "SliderNew", "");            if (position != newPosition)            {                moviePlayer.PositionSeconds = newPosition;                moviePlayer.Play();                moviePlayer.Update(true);            }        }        if (isShow || isQuite)        {            isShowSlider = true;            GUI.Label(new Rect(Screen.width / 2 - backLabel.width/2, Screen.height / 2 - backLabel.height/2,backLabel.width,backLabel.height), "", "backLabel");            if (GUI.Button(new Rect(Screen.width / 2 + replay.width/4, Screen.height / 2 - replay.height/2,replay.width, replay.height), "", "Replay"))            {                isQuite = false;                moviePlayer.Frame = 1;            }            if (GUI.Button(new Rect(Screen.width / 2 -back.width*1.25f, Screen.height / 2 - back.height/2, back.width,back.height), "", "Return"))            {               Application.LoadLevel("FrontInterface");            }        }        if (isPlay == false && isQuite==false)        {            isShowSlider = true;            moviePlayer.Pause();            GUI.Label(new Rect(Screen.width / 2 - pause.width/2, Screen.height / 2-pause.height/2, pause.width, pause.height), "", "Pause");            // 暂停弹出Pause        }    }}
0 0
原创粉丝点击