@@ -54,6 +54,18 @@ public class MicStreamDemo : MonoBehaviour
5454
5555 private bool isRunning ;
5656
57+ public bool IsRunning
58+ {
59+ get { return isRunning ; }
60+ private set
61+ {
62+ isRunning = value ;
63+ CheckForErrorOnCall ( isRunning ? MicStream . MicPause ( ) : MicStream . MicResume ( ) ) ;
64+ }
65+ }
66+
67+ #region Unity Methods
68+
5769 private void OnAudioFilterRead ( float [ ] buffer , int numChannels )
5870 {
5971 // this is where we call into the DLL and let it fill our audio buffer for us
@@ -75,6 +87,11 @@ private void OnAudioFilterRead(float[] buffer, int numChannels)
7587 averageAmplitude = sumOfValues / buffer . Length ;
7688 }
7789
90+ private void OnEnable ( )
91+ {
92+ IsRunning = true ;
93+ }
94+
7895 private void Start ( )
7996 {
8097 CheckForErrorOnCall ( MicStream . MicInitializeCustomRate ( ( int ) StreamType , AudioSettings . outputSampleRate ) ) ;
@@ -98,11 +115,6 @@ private void Start()
98115 isRunning = true ;
99116 }
100117
101- private void OnDestroy ( )
102- {
103- CheckForErrorOnCall ( MicStream . MicDestroy ( ) ) ;
104- }
105-
106118 private void Update ( )
107119 {
108120 CheckForErrorOnCall ( MicStream . MicSetGain ( InputGain ) ) ;
@@ -129,48 +141,42 @@ private void Update()
129141 gameObject . transform . localScale = new Vector3 ( minObjectScale + averageAmplitude , minObjectScale + averageAmplitude , minObjectScale + averageAmplitude ) ;
130142 }
131143
132- private static void CheckForErrorOnCall ( int returnCode )
144+ private void OnApplicationPause ( bool pause )
133145 {
134- MicStream . CheckForErrorOnCall ( returnCode ) ;
146+ IsRunning = pause ;
135147 }
136148
137- private void ToggleMicStream ( bool pause )
149+ private void OnDisable ( )
138150 {
139- CheckForErrorOnCall ( pause ? MicStream . MicPause ( ) : MicStream . MicResume ( ) ) ;
140- isRunning = pause ;
151+ IsRunning = false ;
141152 }
142153
143- private void OnApplicationPause ( bool pause )
154+ private void OnDestroy ( )
144155 {
145- if ( isRunning )
146- {
147- ToggleMicStream ( pause ) ;
148- }
156+ CheckForErrorOnCall ( MicStream . MicDestroy ( ) ) ;
149157 }
150158
151159#if ! UNITY_EDITOR
152160 private void OnApplicationFocus ( bool focused )
153161 {
154- if ( isRunning )
155- {
156- ToggleMicStream ( ! focused ) ;
157- }
162+ IsRunning = focused ;
158163 }
159164#endif
160- private void OnDisable ( )
165+ #endregion
166+
167+ private static void CheckForErrorOnCall ( int returnCode )
161168 {
162- if ( isRunning )
163- {
164- ToggleMicStream ( true ) ;
165- }
169+ MicStream . CheckForErrorOnCall ( returnCode ) ;
166170 }
167171
168- private void OnEnable ( )
172+ public void Enable ( )
169173 {
170- if ( isRunning )
171- {
172- ToggleMicStream ( false ) ;
173- }
174+ IsRunning = true ;
175+ }
176+
177+ public void Disable ( )
178+ {
179+ IsRunning = false ;
174180 }
175181 }
176182}
0 commit comments