コード例 #1
0
    private void DrawButton(Rect position, SerializedProperty prop)
    {
        var obj = prop.objectReferenceValue;

        if (obj == null)
        {
            return;
        }

        position.x     += 4;
        position.width -= 5;

        AudioClip clip = obj as AudioClip;

        Rect buttonRect = new Rect(position);

        buttonRect.width = 20;

        Rect waveformRect = new Rect(position);

        waveformRect.x     += 22;
        waveformRect.width -= 22;
        GUI.DrawTexture(waveformRect, AssetPreview.GetAssetPreview(obj));

        bool   isPlaying  = AudioUtility.IsClipPlaying(clip) && (CurrentClip == prop.propertyPath);
        string buttonText = "";
        Action <SerializedProperty, AudioClip> buttonAction;

        if (isPlaying)
        {
            EditorUtility.SetDirty(prop.serializedObject.targetObject);
            //EditorApplication.QueuePlayerLoopUpdate();
            buttonAction = GetStateInfo(ButtonState.Stop, out buttonText);

            Rect  progressRect = new Rect(waveformRect);
            float time         = AudioUtility.GetClipPosition(clip);
            float length       = clip.length;
            float progress     = time / length;
            float width        = progressRect.width * progress;
            progressRect.width = Mathf.Clamp(width, 3, width);
            GUI.Box(progressRect, "", "SelectionRect");
            DrawLabelOutline(waveformRect, TimeSpan.FromSeconds(length - time).ToString(@"mm\:ss\:fff"));
        }
        else
        {
            buttonAction = GetStateInfo(ButtonState.Play, out buttonText);
            float length = clip.length;
            DrawLabelOutline(waveformRect, TimeSpan.FromSeconds(length).ToString(@"mm\:ss\:fff"));
        }

        if (GUI.Button(buttonRect, buttonText))
        {
            AudioUtility.StopAllClips();
            buttonAction(prop, clip);
        }
    }
コード例 #2
0
        private void DrawTimeline()
        {
            GUILayout.BeginVertical();
            var soundTime = this.m_SoundAudio == null ? 1 : this.m_SoundAudio.length;

            GUILayout.Label("File length: " + (this.m_SoundAudio == null ? "0s" : (int)this.m_SoundAudio.length + "s") + "\n" +
                            "Position: " + (int)this.m_SoundTimeLine + "s");
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("<<", GUILayout.Width(30f)))
            {
                if (this.m_SoundAudio == null)
                {
                    return;
                }
                this.m_SoundTimeLine = this.m_SoundTimeLine <= 1 ? 0 : this.m_SoundTimeLine - 1;
                // Set sound line
                var clipPosition = this.m_SoundTimeLine * AudioUtility.GetFrequency(this.m_SoundAudio);
                AudioUtility.SetClipSamplePosition(this.m_SoundAudio, (int)clipPosition);
            }
            // Sound play line
            this.m_SoundTimeLine = GUILayout.HorizontalSlider(this.m_SoundTimeLine, 0, soundTime);
            this.m_SoundLineRect = GUILayoutUtility.GetLastRect();
            if (this.m_SoundAudio != null)
            {
                if (this.m_IsPause == false)
                {
                    // Update sound line
                    this.m_SoundTimeLine = AudioUtility.GetClipPosition(this.m_SoundAudio);
                    if (this.m_PreviousSoundTimeLine != (int)this.m_SoundTimeLine)
                    {
                        this.m_PreviousSoundTimeLine = (int)this.m_SoundTimeLine;
                    }
                }
                else
                {
                    // Set sound line
                    var clipPosition = this.m_SoundTimeLine * AudioUtility.GetFrequency(this.m_SoundAudio);
                    AudioUtility.SetClipSamplePosition(this.m_SoundAudio, (int)clipPosition);
                }
            }
            if (GUILayout.Button(">>", GUILayout.Width(30f)))
            {
                if (this.m_SoundAudio == null)
                {
                    return;
                }
                this.m_SoundTimeLine = this.m_SoundTimeLine >= this.m_SoundAudio.length - 1 ? this.m_SoundAudio.length : this.m_SoundTimeLine + 1;
                // Set sound line
                var clipPosition = this.m_SoundTimeLine * AudioUtility.GetFrequency(this.m_SoundAudio);
                AudioUtility.SetClipSamplePosition(this.m_SoundAudio, (int)clipPosition);
            }
            GUILayout.EndHorizontal();
            GUILayout.EndVertical();
        }
コード例 #3
0
    void Update()
    {
        float deltaTime = Time.realtimeSinceStartup - prevTime;

        prevTime = Time.realtimeSinceStartup;

        if (seekPosition != oldSeekPosition)
        {
            oldSeekPosition = seekPosition;

            if (!isPlaying)
            {
                if (!previewing && lsdTarget.clip != null)
                {
                    AudioUtility.PlayClip(lsdTarget.clip);
                }

                previewing = true;
                stopTimer  = 0.1f;
                prevTime   = Time.realtimeSinceStartup;
                resetTime  = seekPosition;
            }

            if (lsdTarget.clip)
            {
                AudioUtility.SetClipSamplePosition(lsdTarget.clip, (int)(seekPosition * AudioUtility.GetSampleCount(lsdTarget.clip)));
            }
        }

        if (previewing)
        {
            stopTimer -= deltaTime;
            if (lsdTarget.clip)
            {
                seekPosition    = AudioUtility.GetClipPosition(lsdTarget.clip) / lsdTarget.length;
                oldSeekPosition = seekPosition;
            }

            if (stopTimer <= 0)
            {
                previewing      = false;
                seekPosition    = resetTime;
                oldSeekPosition = seekPosition;
                if (lsdTarget.clip != null)
                {
                    AudioUtility.PauseClip(lsdTarget.clip);
                    AudioUtility.SetClipSamplePosition(lsdTarget.clip, (int)(seekPosition * AudioUtility.GetSampleCount(lsdTarget.clip)));
                }
            }
        }

        if (isPlaying && lsdTarget.clip == null)
        {
            seekPosition   += deltaTime / lsdTarget.length;
            oldSeekPosition = seekPosition;
            if (seekPosition >= 1)
            {
                isPlaying    = false;
                seekPosition = 0;
            }
        }
        else if (isPlaying)
        {
            seekPosition    = AudioUtility.GetClipPosition(lsdTarget.clip) / lsdTarget.length;
            oldSeekPosition = seekPosition;

            isPlaying = AudioUtility.IsClipPlaying(lsdTarget.clip);
        }

        if (isPlaying && visualPreview || previewing && visualPreview)
        {
            UpdatePreview(seekPosition);
        }
    }