Example #1
0
    /// <summary>
    /// 载入一个音频
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    private AudioClip LoadAudicClip(string name)
    {
        if (!ClipIsExit(name))
        {
            Debug.LogError("The audio name: " + name + " is not exit!");
            return(null);
        }

        AudioClip clip = null;

        if (ClipCacheDic.ContainsKey(name) && ClipCacheDic[name] != null)
        {
            AudioCache cache = ClipCacheDic[name];
            clip = cache.GetClip();
        }

        if (clip != null)
        {
            return(clip);
        }

        AudioCache temp = new AudioCache();

        ClipCacheDic.Add(name, temp);
        ResourceRequest rr = Resources.LoadAsync(configDic[name]);

        temp.Asset = rr.asset;
        clip       = temp.GetClip();

        return(clip);
    }
Example #2
0
    void Update()
    {
        for (int i = 0; i < toFreeList.Count; ++i)
        {
            AudioChannel channel = toFreeList[i];

            // 回收AudioClip
            Dictionary <string, AudioCache> .Enumerator er = ClipCacheDic.GetEnumerator();
            while (er.MoveNext())
            {
                if (er.Current.Key.Equals(channel.Name))
                {
                    AudioCache cache = er.Current.Value;
                    cache.AddClip(channel.Clip);
                    channel.Reset();
                }
            }

            // 回收AudioChannel
            freeList.Add(channel);
            useList.Remove(channel);

            if (useList.Count + freeList.Count > _maxChannel && freeList.Count > 0)
            {
                GameObject obj = freeList[0].gameObject;
                freeList.RemoveAt(0);
                GameObject.Destroy(obj);
            }
        }

        toFreeList.Clear();
    }
Example #3
0
    /// <summary>
    /// 获取音频
    /// </summary>
    /// <param name="id"></param>
    /// <param name="flag"></param>
    /// <param name="source"></param>
    public static void GetAudioPacket(int id, int flag, byte[] source)
    {
        CompressPacket packet = AudioDecoder(source);

        byte[] data = new byte[packet.DataLength];
        Buffer.BlockCopy(packet.Data, 0, data, 0, packet.CompLength);
        float[] sample = Speex.DeCompress(data, packet.CompLength);

        AudioCache Cache = null;

        if (AudioCacheDic.Keys.Contains(id))
        {
            Cache = AudioCacheDic[id];
        }
        else
        {
            //最大接收人数限制
            if (AudioCacheDic.Count < MaxSpeakNum)
            {
                Cache = new AudioCache(id, flag);
                AudioCacheDic.Add(id, Cache);
            }
        }
#if DEBUG
        Debug.Log("UpdateAudioLength: " + packet.AudioLength);
#endif
        if (Cache != null)
        {
            Cache.UpData(flag, packet.AudioLength, sample);
        }
        //#if DEBUG
        //        Debug.Log("接收:" + Cache.data.Count);
        //#endif
    }
Example #4
0
    public void PlaySound(string sound)
    {
        AudioClip c = AudioCache.GetItemClip(sound);

        if (c != null)
        {
            AudioManager.Instance.PlayOneShot(transform.position, c, 0.5f, 1f);
        }
    }
Example #5
0
    /// <summary>
    /// 预加载音效
    /// </summary>
    /// <param name="name"></param>
    public void AddPreLoad(string name)
    {
        if (ClipCacheDic.ContainsKey(name) && ClipCacheDic[name] != null)
        {
            return;
        }
        AudioCache temp = new AudioCache();

        ClipCacheDic.Add(name, temp);
        ResourceRequest rr = Resources.LoadAsync(configDic[name]);

        temp.Asset = rr.asset;
    }
Example #6
0
        public bool readPreviousTag(Stream stream)
        {
            var p = stream.Position;
            //Debug.WriteLine("readPreviousTag:{0}({0:X})+4+11=>{1}({1:X})", p,p+11+4);
            var    PreviousTagSize = stream.ReadUInt32();      //4    倒置读取位置
            FlvTag flvTag          = FlvTag.createTag(stream); //11   头部

            if (flvTag == null)
            {
                return(false);
            }
            var data = flvTag.LoadTagData(stream);

            //媒体数据
            if (flvTag is ScriptDataTag)
            {
                MediaData = flvTag as ScriptDataTag;
                return(true);
            }

            //音频头帧
            if (flvTag is AudioTag)
            {
                var tag = flvTag as AudioTag;
                if (FirstAudio == null)
                {
                    FirstAudio = tag as AudioTag;
                    return(true);
                }
                AudioCache.Enqueue(tag);
            }

            //视频头帧
            if (flvTag is VideoTag)
            {
                var tag = flvTag as VideoTag;
                if (FirstVideo == null)
                {
                    FirstVideo = flvTag as VideoTag;
                    return(true);
                }

                VideoCache.Enqueue(tag);
            }

            return(true);
        }
Example #7
0
    void Update()
    {
        for (int i = 0; i < mUseList.Count; ++i)
        {
            AudioChannel channel = mUseList[i];
            channel.UpdateChannel();
        }

        for (int i = 0; i < mToFreeList.Count; ++i)
        {
            AudioChannel channel = mToFreeList[i];

            // 回收AudioClip
            Dictionary <string, AudioCache> .Enumerator er = mClipCacheDic.GetEnumerator();
            while (er.MoveNext())
            {
                if (er.Current.Key.Equals(channel.Name))
                {
                    AudioCache cache = er.Current.Value;
                    cache.AddClip(channel.Clip);
                    channel.Reset();
                    break;
                }
            }

            // 回收AudioChannel
            mFreeList.Add(channel);
            mUseList.Remove(channel);

            if (mUseList.Count + mFreeList.Count > mMaxChannel && mFreeList.Count > 0)
            {
                GameObject obj = mFreeList[0].gameObject;
                mFreeList.RemoveAt(0);
                GameObject.Destroy(obj);
            }
        }

        mToFreeList.Clear();

        if (mPlayList.Count != 0)
        {
            AudioChannel channel = mPlayList[0];
            mPlayList.RemoveAt(0);
            channel.Play();
        }
    }
Example #8
0
    /// <summary>
    /// 载入一个音频
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    private AudioClip LoadAudicClip(string name)
    {
        if (!ClipIsExit(name))
        {
            Debugger.LogError("The audio name: " + name + " is not exit!");
            return(null);
        }

        AudioClip clip = null;

        if (mClipCacheDic.ContainsKey(name))
        {
            if (mClipCacheDic[name] != null)
            {
                AudioCache cache = mClipCacheDic[name];
                clip = cache.GetClip();
            }
            else
            {
                mClipCacheDic.Remove(name);
            }
        }

        if (clip != null)
        {
            return(clip);
        }

        ResourceMisc.AssetWrapper aw = ioo.resourceManager.LoadAsset(mConfigDic[name], typeof(AudioClip));
        if (null == aw)
        {
            Debugger.LogError("The AudioClip name: " + name + " is not exit, please check the assetbundle");
            return(null);
        }
        clip = (AudioClip)aw.GetAsset();

        AudioCache temp = new AudioCache();

        mClipCacheDic.Add(name, temp);
        temp.Asset = aw.GetAsset();
        clip       = temp.GetClip();

        return(clip);
    }
Example #9
0
 private Game()
 {
     world = GameObject.Find("World").GetComponent <World>();
     audio = new AudioCache().Load();
 }
Example #10
0
        bool Reload(double time)
        {
            Stream m = null;
            int    i = 0;

            try {
                for (i = 0; i < times.Count; i++)
                {
                    if (times[i] >= time)
                    {
                        m          = this.CreateStream();
                        m.Position = kfs[i] - 4;
                        newFrame   = true;
                        break;
                    }
                }
            }
            catch (Exception e) {
                m = null;
            }
            if (m != null)
            {
                VideoCache.Clear();
                AudioCache.Clear();
                var dm = MainStream;
                if (dm != null)
                {
                    Task.Run(async() => {
                        await Task.Delay(1000);
                        dm.Dispose();
                    });
                }
                MainStream = m;;
                Debug.WriteLine(time + "==>  TIMER:" + times[i] + "    KFS:{0}({0:X})", kfs[i]);
            }
            if (!running)
            {
                running = true;
                Debug.WriteLine("==running:true==");
                Task.Run(async() => {
                    try {
                        while (true)
                        {
                            if (VideoCache.Count > 1024 || AudioCache.Count > 1024)
                            {
                                await Task.Delay(500);
                                //Debug.WriteLine("@@@ WAIT:RUNNING:" + VideoCache.Count + "|" + AudioCache.Count + " @@@");
                                continue;
                            }/*
                              * if (MainStream != m && MainStream != null) {
                              * m = MainStream;
                              * Debug.WriteLine("==running:update==");
                              * }*/
                            if (!this.readPreviousTag(MainStream))
                            {
                                break;
                            }
                        }
                        Debug.WriteLine("==running:false==");
                        Debug.WriteLine("END:" + MainStream.Position + "/" + MainStream.Length);
                        running = false;
                    }
                    catch (Exception ERR) {
                        Debug.WriteLine("==running:catch==");
                        running = false;
                        await Task.Delay(3000);
                        Debug.WriteLine(ERR);
                        Reload(time);
                        //throw;
                    }
                });
            }
            return(m != null);
        }
Example #11
0
        bool Reload(double time)
        {
            Stream m = null;

            try {
                for (int i = 0; i < times.Count; i++)
                {
                    if (times[i] >= time)
                    {
                        m = this.CreateStream();
                        //Debug.WriteLine("====Reload:"+i+"=====:" );
                        Debug.WriteLine(time + "==>  TIMER:" + times[i] + "    KFS:{0}({0:X})", kfs[i]);
                        m.Position = kfs[i] - 4;
                        VideoCache.Clear();
                        AudioCache.Clear();
                        break;
                    }
                }
            }
            catch (Exception e) {
                m = null;
                Debug.WriteLine(e);
            }
            if (m == null)
            {
                return(false);
            }
            else
            {
                MainStream = m;
            }

            if (!running)
            {
                running = true;
                Debug.WriteLine("==running:true==");
                Task.Run(async() => {
                    try {
                        while (true)
                        {
                            if (VideoCache.Count > 1024 || AudioCache.Count > 1024)
                            {
                                await Task.Delay(500);
                                //Debug.WriteLine("@@@ WAIT:RUNNING:" + VideoCache.Count + "|" + AudioCache.Count + " @@@");
                                continue;
                            }
                            if (!this.readPreviousTag(MainStream))
                            {
                                break;
                            }
                        }
                        Debug.WriteLine("==running:false==");
                        running = false;
                    }
                    catch (Exception ERR) {
                        Debug.WriteLine("==running:false==");
                        running = false;
                        Debug.WriteLine(ERR);
                        Reload(time + 5);
                        //throw;
                    }
                });
            }

            return(true);
        }