private void timer_Tick(object sender, System.EventArgs e)
        {
            FMOD.RESULT    result;
            FMOD.OPENSTATE openstate       = 0;
            uint           percentbuffered = 0;
            bool           starving        = false;
            bool           busy            = false;

            if (soundcreated)
            {
                result = sound.getOpenState(ref openstate, ref percentbuffered, ref starving, ref busy);
                ERRCHECK(result);

                if (openstate == FMOD.OPENSTATE.READY && channel == null)
                {
                    result = system.playSound(FMOD.CHANNELINDEX.FREE, sound, false, ref channel);
                    ERRCHECK(result);
                }
            }

            if (channel != null)
            {
                uint ms      = 0;
                bool playing = false;
                bool paused  = false;

                for (;;)
                {
                    FMOD.TAG tag = new FMOD.TAG();
                    if (sound.getTag(null, -1, ref tag) != FMOD.RESULT.OK)
                    {
                        break;
                    }
                    if (tag.datatype == FMOD.TAGDATATYPE.STRING)
                    {
                        textBox.Text = tag.name + " = " + Marshal.PtrToStringAnsi(tag.data) + " (" + tag.datalen + " bytes)";
                    }
                    else
                    {
                        break;
                    }
                }

                result = channel.getPaused(ref paused);
                ERRCHECK(result);
                result = channel.isPlaying(ref playing);
                ERRCHECK(result);
                result = channel.getPosition(ref ms, FMOD.TIMEUNIT.MS);
                ERRCHECK(result);

                statusBar.Text = "Time " + (ms / 1000 / 60) + ":" + (ms / 1000 % 60) + ":" + (ms / 10 % 100) + (openstate == FMOD.OPENSTATE.BUFFERING ? " Buffering..." : (openstate == FMOD.OPENSTATE.CONNECTING ? " Connecting..." : (paused ? " Paused       " : (playing ? " Playing      " : " Stopped      ")))) + "(" + percentbuffered + "%)" + (starving ? " STARVING" : "        ");
            }

            if (system != null)
            {
                system.update();
            }
        }
Пример #2
0
        public void PlaySound()
        {
            bool state = true;

            while (state)
            {
                FMOD.OPENSTATE openstate       = 0;
                uint           percentbuffered = 0;
                bool           starving        = false;
                bool           busy            = false;
                FMOD.RESULT    result          = sounds.getOpenState(ref openstate, ref percentbuffered, ref starving, ref busy);
                ErrorCheck(result);

                if (openstate == FMOD.OPENSTATE.READY && channels == null)
                {
                    result = system.playSound(FMOD.CHANNELINDEX.FREE, sounds, false, ref channels);
                    ErrorCheck(result);
                    channels.setVolume(0.0f);
                    state = false;
                }
            }
        }
        private void timer_Tick(object sender, System.EventArgs e)
        {
            FMOD.RESULT    result;
            FMOD.OPENSTATE openstate = 0;

            if (soundcreated)
            {
                uint percentbuffered = 0;
                bool starving        = false;
                bool busy            = false;

                result = sound.getOpenState(ref openstate, ref percentbuffered, ref starving, ref busy);
                ERRCHECK(result);

                if (openstate == FMOD.OPENSTATE.READY && channel == null)
                {
                    result = system.playSound(FMOD.CHANNELINDEX.FREE, sound, false, ref channel);
                    ERRCHECK(result);
                }
            }

            if (channel != null)
            {
                uint ms          = 0;
                bool playing     = false;
                bool paused      = false;
                int  tagsupdated = 0;
                int  numtags     = 0;

                result = sound.getNumTags(ref numtags, ref tagsupdated);
                ERRCHECK(result);

                if (tagsupdated != 0)
                {
                    for (;;)
                    {
                        FMOD.TAG tag = new FMOD.TAG();

                        if (sound.getTag(null, -1, ref tag) != FMOD.RESULT.OK)
                        {
                            break;
                        }

                        if (tag.datatype == FMOD.TAGDATATYPE.STRING)
                        {
                            FMOD.SOUND_FORMAT format = FMOD.SOUND_FORMAT.NONE;
                            int channels             = 0;
                            int bits = 0;

                            sound.getFormat(ref gSoundType, ref format, ref channels, ref bits);

                            if (tag.name == "ARTIST")
                            {
                                if (Marshal.PtrToStringAnsi(tag.data) != gCurrentTrackArtist)
                                {
                                    gCurrentTrackArtist = Marshal.PtrToStringAnsi(tag.data);
                                    gUpdateFileName     = true;
                                }
                            }
                            if (tag.name == "TITLE")
                            {
                                if (Marshal.PtrToStringAnsi(tag.data) != gCurrentTrackTitle)
                                {
                                    gCurrentTrackTitle = Marshal.PtrToStringAnsi(tag.data);
                                    gUpdateFileName    = true;
                                }
                            }
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                result = channel.isPlaying(ref playing);
                if (result != FMOD.RESULT.OK || !playing)
                {
                    sound.release();
                    sound   = null;
                    channel = null;
                }
                else
                {
                    result = channel.getPaused(ref paused);
                    ERRCHECK(result);
                    result = channel.getPosition(ref ms, FMOD.TIMEUNIT.MS);
                    ERRCHECK(result);

                    statusBar.Text = "Time " + (ms / 1000 / 60) + ":" + (ms / 1000 % 60) + ":" + (ms / 10 % 100) + (paused ? " Paused " : playing ? " Playing" : " Stopped");
                }
            }

            if (sound != null)
            {
                uint percentbuffered = 0;
                bool starving        = false;
                bool busy            = false;

                sound.getOpenState(ref openstate, ref percentbuffered, ref starving, ref busy);

                if (openstate == FMOD.OPENSTATE.ERROR)
                {
                    sound.release();
                    sound   = null;
                    channel = null;
                }
            }

            if (openstate == FMOD.OPENSTATE.ERROR)
            {
                statusBar.Text = "Error occurred or stream ended.  Restarting stream..";
                result         = system.createSound(url, (FMOD.MODE.HARDWARE | FMOD.MODE._2D | FMOD.MODE.CREATESTREAM | FMOD.MODE.NONBLOCKING), ref exinfo, ref sound);
                ERRCHECK(result);
            }

            if (system != null)
            {
                system.update();
            }
        }
Пример #4
0
        /// <summary>
        /// Stop and try to release FMOD sound resources
        /// </summary>
        void Stop_Internal()
        {
            this.bufferFillPercentage = 0;
            this.isPlaying            = false;
            this.isPaused             = false;
            this.finished             = false;
            this.starving             = false;
            this.deviceBusy           = false;
            this.tags = new string[tagcount];

            /*
             *  Stop the channel, then wait for it to finish opening before we release it.
             */
            if (channel != null)
            {
                result = channel.stop();
                // ERRCHECK(result, "channel.stop", false);
            }

            channel = null;

            /*
             * on wrong requested audio type getOpenState is still returning BUFFERING/LOADING here
             * it is not possible to release sound and system due to FMOD deadlocking
             */
            this.unstableShutdown = false;

            LOG(LogLevel.DEBUG, "Waiting for sound to finish opening before trying to release it....");

            System.Threading.Thread.Sleep(50);

            int c = 0;

            do
            {
                if (++c > 5)
                {
                    break;
                }

                System.Threading.Thread.Sleep(10);

                result    = FMOD.RESULT.OK;
                openstate = FMOD.OPENSTATE.READY;

                if (system != null)
                {
                    result = system.update();
                    // ERRCHECK(result, "system.update", false);
                }

                if (sound != null)
                {
                    result = sound.getOpenState(out openstate, out bufferFillPercentage, out starving, out deviceBusy);
                    // ERRCHECK(result, "sound.getOpenState", false);

                    LOG(LogLevel.DEBUG, "Stream open state: {0}, buffer fill {1} starving {2} networkBusy {3}", openstate, bufferFillPercentage, starving, deviceBusy);
                }
            }while (openstate != FMOD.OPENSTATE.READY || result != FMOD.RESULT.OK);

            if (openstate == FMOD.OPENSTATE.BUFFERING)
            {
                if (result != FMOD.RESULT.ERR_NET_URL)
                {
                    this.unstableShutdown = true;
                    LOG(LogLevel.ERROR, "AudioStreamer is in unstable state - please restart editor/application. [{0} {1}]", openstate, result);
                }
            }


            /*
             *  Shut down
             */

            if (sound != null && !this.unstableShutdown)
            {
                result = sound.release();
                // ERRCHECK(result, "sound.release", false);
            }

            sound = null;
        }