Terraria v1.4.4.9
Terraria source code documentation
All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros
MusicCueHolder.cs
Go to the documentation of this file.
1using System;
2using Microsoft.Xna.Framework.Audio;
3
4namespace Terraria.Audio;
5
6public class MusicCueHolder
7{
8 private SoundBank _soundBank;
9
10 private string _cueName;
11
12 private Cue _loadedCue;
13
14 private float _lastSetVolume;
15
16 public bool IsPlaying
17 {
18 get
19 {
20 if (_loadedCue != null)
21 {
22 return _loadedCue.IsPlaying;
23 }
24 return false;
25 }
26 }
27
28 public bool IsOngoing
29 {
30 get
31 {
32 if (_loadedCue != null)
33 {
34 if (!_loadedCue.IsPlaying)
35 {
36 return !_loadedCue.IsStopped;
37 }
38 return true;
39 }
40 return false;
41 }
42 }
43
44 public MusicCueHolder(SoundBank soundBank, string cueName)
45 {
46 _soundBank = soundBank;
47 _cueName = cueName;
48 _loadedCue = null;
49 }
50
51 public void Pause()
52 {
53 if (_loadedCue == null || _loadedCue.IsPaused || !_loadedCue.IsPlaying)
54 {
55 return;
56 }
57 try
58 {
59 _loadedCue.Pause();
60 }
61 catch (Exception)
62 {
63 }
64 }
65
66 public void Resume()
67 {
68 if (_loadedCue == null || !_loadedCue.IsPaused)
69 {
70 return;
71 }
72 try
73 {
74 _loadedCue.Resume();
75 }
76 catch (Exception)
77 {
78 }
79 }
80
81 public void Stop()
82 {
83 if (_loadedCue != null)
84 {
85 SetVolume(0f);
86 _loadedCue.Stop(AudioStopOptions.Immediate);
87 }
88 }
89
90 public void RestartAndTryPlaying(float volume)
91 {
92 PurgeCue();
93 TryPlaying(volume);
94 }
95
96 private void PurgeCue()
97 {
98 if (_loadedCue != null)
99 {
100 _loadedCue.Stop(AudioStopOptions.Immediate);
101 _loadedCue.Dispose();
102 _loadedCue = null;
103 }
104 }
105
106 public void Play(float volume)
107 {
108 LoadTrack(forceReload: false);
109 SetVolume(volume);
110 _loadedCue.Play();
111 }
112
113 public void TryPlaying(float volume)
114 {
115 LoadTrack(forceReload: false);
116 if (_loadedCue.IsPrepared)
117 {
118 SetVolume(volume);
119 if (!_loadedCue.IsPlaying)
120 {
121 _loadedCue.Play();
122 }
123 }
124 }
125
126 public void SetVolume(float volume)
127 {
128 _lastSetVolume = volume;
129 if (_loadedCue != null)
130 {
131 _loadedCue.SetVariable("Volume", _lastSetVolume);
132 }
133 }
134
135 private void LoadTrack(bool forceReload)
136 {
137 if (forceReload || _loadedCue == null)
138 {
140 }
141 }
142}
void RestartAndTryPlaying(float volume)
MusicCueHolder(SoundBank soundBank, string cueName)
void LoadTrack(bool forceReload)