您的位置:首页 > 移动开发 > Unity3D

Unity3D 实现视频播放和声音同步(笔记)

2013-10-31 13:21 453 查看
这个代码有些杂乱 ,是工作中的一个文件,我特意拿出来保存在这里,希望自己和别人可以以后直接借鉴。

当然如果想要使用本文件 , 还要经过一些修改,因为这个文件单独拿出来是不能独立运行的,不过经过简单的修改就可以实现上述功能了,比如把按钮控制播放停止等等太加进来就基本可以使用。

仔细说下我的这个 代码的大概意思吧,首先 , 我这个文件是赋给一个plane对象的,在这个plane对象上面我想去播放视频,嗯,想法就是这么简单,然后主要功能就是完成 从 本地获得绝对路径 之后, 利用WWW类, 下载本地磁盘的视频,然后利用 renderer.material.mainTexture = www.movie;

把视频赋到plane的纹理里面, 这样Plane就可以播放视频了。但是我发现那个视频放出来没有声音,所以在网上面查了半天终于查到资料,

http://hi.baidu.com/ccoooolleerr/item/b19806a13506fcac29ce9dbb, 我参考这个人写的,模仿的把音频组件添加进来了,aud = gameObject.AddComponent<AudioSource>();
, 这句话,然后声音就都可以放出来了。

using UnityEngine;
using System.Collections;

public class pl_portmov : MonoBehaviour {

// Use this for initialization
void Start ()
{
transform.parent = GameObject.Find("loving_movies").transform;

transform.localPosition = new Vector3(0.6f, 0.009f, -0.7f);
transform.localScale = new Vector3(0.84f, 0.001f, 0.8f);
transform.localRotation = Quaternion.identity;

transform.Rotate(0f, 0f, 0f, Space.World);		//旋转角度
name = "movie_part";

aud = gameObject.AddComponent<AudioSource>();	//这里添加音频组件组件
}

// Update is called once per frame
void Update () {

}

/*接收 Button_queding传送的消息*/

private string filename;
public WWW www;
public static MovieTexture movtex;
public AudioSource aud;

public IEnumerator Getflnam(string flname)
{
filename = flname;	//接收文件路径

yield return www = new WWW("file://" + flname);
if (www.error != null)
{
Debug.Log("www error!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}

if (www.isDone)
Debug.Log("www.isDone");

movtex = www.movie as MovieTexture;
renderer.material.mainTexture =movtex;

movtex.loop = true;

//同步播放音频
aud.audio.clip = movtex.audioClip;
if (movtex.isReadyToPlay)
{
movtex.Play();
aud.audio.Play();
}
}

public void mov_play()
{
if (movtex.isReadyToPlay)
{
movtex.Play();
aud.audio.Play();
Debug.Log("movtex.Play() success");
}
}

public void mov_pause()
{
movtex.Pause();
aud.audio.Pause();
Debug.Log("movtex.Pause() success");
}

public void mov_stop()
{
movtex.Stop();
aud.audio.Stop();
Debug.Log("movtex.Stop() success");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: