Skip to content

Commit b2780dd

Browse files
authored
Merge pull request #196 from GetStream/feature/uni-142-add-various-improvements-to-the-example-project
[Example Project] Disable audio & video publishing and audio playback…
2 parents 8287a60 + d70df52 commit b2780dd

File tree

3 files changed

+112
-18
lines changed

3 files changed

+112
-18
lines changed

Assets/Samples/Stream Video & Audio Chat SDK/0.8.18.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Samples/Stream Video & Audio Chat SDK/0.8.18/Video & Audio Chat Example Project/Scripts/StreamVideoManager.cs

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public async Task JoinAsync(string callId, bool create = true)
6666

6767
Debug.Log($"Join call, create: {create}, callId: {callId}");
6868
await Client.JoinCallAsync(StreamCallType.Default, callId, create, ring: true, notify: false);
69-
69+
7070
if (_autoEnableMicrophone)
7171
{
7272
Client.AudioDeviceManager.SetEnabled(true);
@@ -111,7 +111,7 @@ public void ToggleMusic(bool loop = true, bool forceStop = false)
111111
{
112112
throw new ArgumentNullException($"{nameof(_musicClip)} is not assigned.");
113113
}
114-
114+
115115
var audioSource = gameObject.GetComponent<AudioSource>();
116116
if (audioSource == null)
117117
{
@@ -123,7 +123,7 @@ public void ToggleMusic(bool loop = true, bool forceStop = false)
123123
audioSource.Stop();
124124
return;
125125
}
126-
126+
127127
audioSource.clip = _musicClip;
128128
audioSource.loop = loop;
129129
audioSource.Play();
@@ -175,7 +175,45 @@ protected async void OnDestroy()
175175
Client.Dispose();
176176
Client = null;
177177
}
178-
178+
179+
#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
180+
protected void OnApplicationPause(bool pauseStatus)
181+
{
182+
if (Client == null)
183+
{
184+
return;
185+
}
186+
187+
if (pauseStatus)
188+
{
189+
// App is going to background
190+
Client.PauseAndroidAudioPlayback();
191+
_wasAudioPublishEnabledOnPause = Client.AudioDeviceManager.IsEnabled;
192+
_wasVideoPublishEnabledOnPause = Client.VideoDeviceManager.IsEnabled;
193+
194+
Client.AudioDeviceManager.SetEnabled(false);
195+
Client.VideoDeviceManager.SetEnabled(false);
196+
}
197+
else
198+
{
199+
// App is coming to foreground
200+
Client.ResumeAndroidAudioPlayback();
201+
202+
if (_wasAudioPublishEnabledOnPause)
203+
{
204+
Client.AudioDeviceManager.SetEnabled(true);
205+
_wasAudioPublishEnabledOnPause = false;
206+
}
207+
208+
if (_wasVideoPublishEnabledOnPause)
209+
{
210+
Client.VideoDeviceManager.SetEnabled(true);
211+
_wasVideoPublishEnabledOnPause = false;
212+
}
213+
}
214+
}
215+
#endif
216+
179217
/// <summary>
180218
/// API success response template when using Stream's Demo Credentials
181219
/// </summary>
@@ -212,24 +250,27 @@ private string _info
212250

213251
[SerializeField]
214252
private string _userToken = "";
215-
253+
216254
[Header("Background Music in a call")]
217255
[SerializeField]
218256
private AudioClip _musicClip = null;
219-
257+
220258
[SerializeField]
221259
private bool _playOnCallStart = false;
222-
260+
223261
[Header("Auto-enable devices on joining a call")]
224262
[SerializeField]
225263
private bool _autoEnableCamera = false;
226-
264+
227265
[SerializeField]
228266
private bool _autoEnableMicrophone = false;
229267

230268
private StreamClientConfig _clientConfig;
231269
private IStreamCall _activeCall;
232270

271+
private bool _wasAudioPublishEnabledOnPause;
272+
private bool _wasVideoPublishEnabledOnPause;
273+
233274
private async Task ConnectToStreamAsync(AuthCredentials credentials)
234275
{
235276
var credentialsEmpty = string.IsNullOrEmpty(credentials.ApiKey) &&
@@ -299,9 +340,11 @@ private void OnCallEnded(IStreamCall call)
299340

300341
if (_activeCall.Participants == null)
301342
{
302-
Debug.LogError("Active call participants were null when trying to end it. call is null " + (call == null));
343+
Debug.LogError("Active call participants were null when trying to end it. call is null " +
344+
(call == null));
303345
return;
304346
}
347+
305348
var callId = _activeCall.Id;
306349
var localParticipant = _activeCall.Participants.First(p => p.IsLocalParticipant);
307350
Client.SendDebugLogs(call.Id, localParticipant.SessionId);

Packages/StreamVideo/Samples~/VideoChat/Scripts/StreamVideoManager.cs

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public async Task JoinAsync(string callId, bool create = true)
6666

6767
Debug.Log($"Join call, create: {create}, callId: {callId}");
6868
await Client.JoinCallAsync(StreamCallType.Default, callId, create, ring: true, notify: false);
69-
69+
7070
if (_autoEnableMicrophone)
7171
{
7272
Client.AudioDeviceManager.SetEnabled(true);
@@ -111,7 +111,7 @@ public void ToggleMusic(bool loop = true, bool forceStop = false)
111111
{
112112
throw new ArgumentNullException($"{nameof(_musicClip)} is not assigned.");
113113
}
114-
114+
115115
var audioSource = gameObject.GetComponent<AudioSource>();
116116
if (audioSource == null)
117117
{
@@ -123,7 +123,7 @@ public void ToggleMusic(bool loop = true, bool forceStop = false)
123123
audioSource.Stop();
124124
return;
125125
}
126-
126+
127127
audioSource.clip = _musicClip;
128128
audioSource.loop = loop;
129129
audioSource.Play();
@@ -175,7 +175,45 @@ protected async void OnDestroy()
175175
Client.Dispose();
176176
Client = null;
177177
}
178-
178+
179+
#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
180+
protected void OnApplicationPause(bool pauseStatus)
181+
{
182+
if (Client == null)
183+
{
184+
return;
185+
}
186+
187+
if (pauseStatus)
188+
{
189+
// App is going to background
190+
Client.PauseAndroidAudioPlayback();
191+
_wasAudioPublishEnabledOnPause = Client.AudioDeviceManager.IsEnabled;
192+
_wasVideoPublishEnabledOnPause = Client.VideoDeviceManager.IsEnabled;
193+
194+
Client.AudioDeviceManager.SetEnabled(false);
195+
Client.VideoDeviceManager.SetEnabled(false);
196+
}
197+
else
198+
{
199+
// App is coming to foreground
200+
Client.ResumeAndroidAudioPlayback();
201+
202+
if (_wasAudioPublishEnabledOnPause)
203+
{
204+
Client.AudioDeviceManager.SetEnabled(true);
205+
_wasAudioPublishEnabledOnPause = false;
206+
}
207+
208+
if (_wasVideoPublishEnabledOnPause)
209+
{
210+
Client.VideoDeviceManager.SetEnabled(true);
211+
_wasVideoPublishEnabledOnPause = false;
212+
}
213+
}
214+
}
215+
#endif
216+
179217
/// <summary>
180218
/// API success response template when using Stream's Demo Credentials
181219
/// </summary>
@@ -212,24 +250,27 @@ private string _info
212250

213251
[SerializeField]
214252
private string _userToken = "";
215-
253+
216254
[Header("Background Music in a call")]
217255
[SerializeField]
218256
private AudioClip _musicClip = null;
219-
257+
220258
[SerializeField]
221259
private bool _playOnCallStart = false;
222-
260+
223261
[Header("Auto-enable devices on joining a call")]
224262
[SerializeField]
225263
private bool _autoEnableCamera = false;
226-
264+
227265
[SerializeField]
228266
private bool _autoEnableMicrophone = false;
229267

230268
private StreamClientConfig _clientConfig;
231269
private IStreamCall _activeCall;
232270

271+
private bool _wasAudioPublishEnabledOnPause;
272+
private bool _wasVideoPublishEnabledOnPause;
273+
233274
private async Task ConnectToStreamAsync(AuthCredentials credentials)
234275
{
235276
var credentialsEmpty = string.IsNullOrEmpty(credentials.ApiKey) &&
@@ -299,9 +340,11 @@ private void OnCallEnded(IStreamCall call)
299340

300341
if (_activeCall.Participants == null)
301342
{
302-
Debug.LogError("Active call participants were null when trying to end it. call is null " + (call == null));
343+
Debug.LogError("Active call participants were null when trying to end it. call is null " +
344+
(call == null));
303345
return;
304346
}
347+
305348
var callId = _activeCall.Id;
306349
var localParticipant = _activeCall.Participants.First(p => p.IsLocalParticipant);
307350
Client.SendDebugLogs(call.Id, localParticipant.SessionId);

0 commit comments

Comments
 (0)