Beispiel #1
0
        protected override bool PlaySource(Entity e)
        {
            var mgr = EntityManager;

            if (mgr.HasComponent <AudioSource>(e))
            {
                AudioSource audioSource = mgr.GetComponentData <AudioSource>(e);

                Entity clipEntity = audioSource.clip;
                if (mgr.HasComponent <AudioHTMLClip>(clipEntity))
                {
                    AudioHTMLClip clip = mgr.GetComponentData <AudioHTMLClip>(clipEntity);
                    if (clip.clipID > 0)
                    {
                        if (!unlocked)
                        {
                            AudioHTMLNativeCalls.Unlock();
                            unlocked = AudioHTMLNativeCalls.IsUnlocked();
                            if (unlocked)
                            {
                                TinyEnvironment env = World.TinyEnvironment();
                                AudioConfig     ac  = env.GetConfigData <AudioConfig>();
                                ac.unlocked = unlocked;
                                env.SetConfigData(ac);
                            }
                        }

                        if (unlocked)
                        {
                            // If there is an existing source, it should re-start.
                            // Do this with a Stop() and let it play below.
                            if (mgr.HasComponent <AudioHTMLSource>(e))
                            {
                                AudioHTMLSource ans = mgr.GetComponentData <AudioHTMLSource>(e);
                                AudioHTMLNativeCalls.Stop(ans.sourceID, true);
                            }

                            int sourceID = ++IDPool.sourceID;
                            AudioHTMLNativeCalls.Play(clip.clipID, sourceID, audioSource.volume, audioSource.loop);
                            AudioHTMLSource audioNativeSource = new AudioHTMLSource()
                            {
                                sourceID = sourceID
                            };
                            // Need a native source as well.
                            if (mgr.HasComponent <AudioHTMLSource>(e))
                            {
                                mgr.SetComponentData(e, audioNativeSource);
                            }
                            else
                            {
                                PostUpdateCommands.AddComponent(e, audioNativeSource);
                            }

                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Beispiel #2
0
        protected static int PlaySource(EntityManager mgr, Entity e)
        {
            if (mgr.HasComponent <AudioSource>(e))
            {
                AudioSource audioSource = mgr.GetComponentData <AudioSource>(e);

                Entity clipEntity = audioSource.clip;
                if (mgr.HasComponent <AudioHTMLClip>(clipEntity))
                {
                    AudioHTMLClip clip = mgr.GetComponentData <AudioHTMLClip>(clipEntity);
                    if (clip.clipID > 0)
                    {
                        // If there is an existing source, it should re-start.
                        // Do this with a Stop() and let it play below.
                        if (mgr.HasComponent <AudioSourceID>(e))
                        {
                            AudioSourceID ans = mgr.GetComponentData <AudioSourceID>(e);
                            AudioHTMLNativeCalls.Stop((int)ans.sourceID, 1);
                        }

                        float volume = audioSource.volume;
                        float pan    = mgr.HasComponent <Audio2dPanning>(e) ? mgr.GetComponentData <Audio2dPanning>(e).pan : 0.0f;
                        float pitch  = mgr.HasComponent <AudioPitch>(e) ? mgr.GetComponentData <AudioPitch>(e).pitch : 1.0f;

                        // For 3d sounds, we start at volume zero because we don't know if this sound is close or far from the listener.
                        // It is much smoother to ramp up volume from zero than the alternative.
                        if (mgr.HasComponent <Audio3dPanning>(e))
                        {
                            volume = 0.0f;
                        }

                        int sourceID = ++SharedIDPool.Value.Data.sourceID;

                        // Check the return value from Play because it fails sometimes at startup for AudioSources with PlayOnAwake set to true.
                        // If initial attempt fails, try again next frame.
                        if (AudioHTMLNativeCalls.Play(clip.clipID, sourceID, volume, pitch, pan, audioSource.loop ? 1 : 0) == 0)
                        {
                            return(0);
                        }

                        return(sourceID);
                    }
                }
            }

            return(0);
        }