Unity AVProVideo脚本方法调用

1.搭建场景


2.脚本

项目地址

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using RenderHeads.Media.AVProVideo;
using System.IO;

public class ButtonControl : MonoBehaviour {
    Button _btnDownLoad;
    Button _btnPlay;
    Button _btnStop;
    Button _btnScreen;//是否全屏
    MediaPlayer _mp;
    Slider _sliVideo;//进度条
    //Slider _sliSound;//声音控制
    private float _setAudioVolumeSliderValue;

    Text _txtNowTime;
    Text _txtAllTime;

    private float _setVideoSeekSliderValue;
    private bool _wasPlayingOnScrub;

    Animation _ani;
    bool _isOvered = true;

    void Start () {
        _btnDownLoad = this.transform.Find("BtnDownload").GetComponent<Button>();
        _btnPlay = this.transform.Find("BtnPlay").GetComponent<Button>();
        _btnStop = this.transform.Find("BtnStop").GetComponent<Button>();
        _btnScreen = this.transform.Find("AVProVideo/BtnScreen").GetComponent<Button>();
        _mp = GameObject.Find("MediaPlayer").GetComponent<MediaPlayer>();
        _sliVideo = this.transform.Find("SliVideo").GetComponent <Slider>();
        //_sliSound = this.transform.Find("SliSound").GetComponent<Slider>();

        _txtNowTime = this.transform.Find("TxtNowTime").GetComponent<Text>();
        _txtAllTime = this.transform.Find("TxtAllTime").GetComponent<Text>();

        _ani= this.transform.Find ("AVProVideo/BlackImg").GetComponent<Animation>();

        _btnDownLoad.onClick.AddListener(BtnDownLoadClick);
        _btnPlay.onClick.AddListener(BtnPlayClick);
        _btnStop.onClick.AddListener(BtnStopClick);
        _btnScreen.onClick.AddListener(BtnScreenClick);
    }

    //public void OnAudioVolumeSlider()
    //{
    //    if (_mp && _sliSound && _sliSound.value != _setAudioVolumeSliderValue)
    //    {
    //        _mp.Control.SetVolume(_sliSound.value);
    //    }
    //}

    public void Replay()
    {
        _isOvered = true;
        this.transform.Find("AVProVideo/BlackImg/BtnReplay").gameObject.SetActive(false);
        _ani.gameObject.GetComponent<Image>().color = new Color(0, 0, 0, 0);
        _mp.Control.Rewind();
        _mp.Control.Play();
    }

    public void OnVideoSeekSlider()
    {
        if (_mp && _sliVideo && _sliVideo.value != _setVideoSeekSliderValue)
        {
            _mp.Control.Seek(_sliVideo.value * _mp.Info.GetDurationMs());
        }
    }

    public void OnVideoSliderDown()
    {
        if (_mp)
        {
            _wasPlayingOnScrub = _mp.Control.IsPlaying();
            if (_wasPlayingOnScrub)
            {
                _mp.Control.Pause();
            }
            OnVideoSeekSlider();
        }
    }
    public void OnVideoSliderUp()
    {
        if (_mp && _wasPlayingOnScrub)
        {
            _mp.Control.Play();
            _wasPlayingOnScrub = false;
        }
    }

    //全屏/非全屏显示
    private void BtnScreenClick()
    {
        Text tx = _btnScreen.gameObject.transform.Find("Text").GetComponent<Text>();
        if (tx.text == "全屏")
        {
            tx.text = "非全屏";
            Screen.orientation = ScreenOrientation.LandscapeLeft;
        }
        else
        {
            tx.text = "全屏";
            Screen.orientation = ScreenOrientation.Portrait;
        }
        Debug.Log("BtnScreenClick");
    }

    private void BtnStopClick()
    {
        _mp.Stop();
        _mp.OpenVideoFromFile(MediaPlayer.FileLocation.AbsolutePathOrURL, @"http://kanyikan.oss-cn-shanghai.aliyuncs.com/%E6%96%B0%E5%AE%89%E5%8E%BF/%E9%BE%99%E6%BD%AD%E5%A4%A7%E5%B3%A1%E8%B0%B7.mp4", false);
    }

    private void BtnPlayClick()
    {
        string[] src = new string[1] { "Android" };
        string[] srcs = Application.persistentDataPath.Split(src, StringSplitOptions.None);
        string path = srcs[0] + @"/Video";

        if (File.Exists(path + @"/aaa.mp4"))
        {
            _mp.OpenVideoFromFile(MediaPlayer.FileLocation.RelativeToProjectFolder, path + "/aaa.mp4", true);
        }
        else
        {
            _mp.OpenVideoFromFile(MediaPlayer.FileLocation.AbsolutePathOrURL, @"http://kanyikan.oss-cn-shanghai.aliyuncs.com/%E6%96%B0%E5%AE%89%E5%8E%BF/%E9%BE%99%E6%BD%AD%E5%A4%A7%E5%B3%A1%E8%B0%B7.mp4", true);
        }
    }

    /// <summary>
    /// 下载文件
    /// </summary>
    private void BtnDownLoadClick()
    {
        //下载文件
        System.Net.WebClient myWebClient = new System.Net.WebClient();
        string[] src = new string[1] { "Android" };
        string[] srcs = Application.persistentDataPath.Split(src, StringSplitOptions.None);
        string path = srcs[0] + @"/Video";
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        myWebClient.DownloadFile(@"http://kanyikan.oss-cn-shanghai.aliyuncs.com/%E6%96%B0%E5%AE%89%E5%8E%BF/%E9%BE%99%E6%BD%AD%E5%A4%A7%E5%B3%A1%E8%B0%B7.mp4", path + @"/aaa.mp4");
        //下载end
    }

    void Update () {
        if (_mp && _mp.Info != null && _mp.Info.GetDurationMs() > 0f)
        {
            float time = _mp.Control.GetCurrentTimeMs();
            float d = time / _mp.Info.GetDurationMs();
            _setVideoSeekSliderValue = d;
            _sliVideo.value = d;
       
            int _allTime = ((int)_mp.Info.GetDurationMs() / 1000);
            _txtAllTime.text = GetTime(_allTime);

            int _nowTime = ((int)time / 1000);
            _txtNowTime.text = GetTime(_nowTime);

            //if (_isOvered && _mp.Control.IsFinished())
            //{
            //    BtnScreenClick();
            //    _ani.Play();
            //    _isOvered = false;
            //}
        }
    }

    // 获取总的时间字符串  
    string GetTime(float time)
    {
        return GetMinute(time) + GetSecond(time);
    }

    //获取分钟   
    string GetMinute(float time)
    {
        int timer = (int)((time % 3600) / 60);
        string timerStr;
        if (timer < 10)
        {
            timerStr = "0" + timer.ToString() + ":";
        }
        else
        {
            timerStr = timer.ToString() + ":";
        }         
        return timerStr;
    }

    // 获取秒  
    string GetSecond(float time)
    {
        int timer = (int)((time % 3600) % 60);
        string timerStr;
        if (timer < 10)
        {
            timerStr = "0" + timer.ToString();
        }
        else
        {
            timerStr = timer.ToString();
        }
        return timerStr;
    }
}


猜你喜欢

转载自blog.csdn.net/Until_/article/details/79390058