Skip to content

Commit 41364cb

Browse files
committed
feat(SpeechToText Examples): Refactored WWW to UnityWebRequest
1 parent da2784d commit 41364cb

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

Examples/ServiceExamples/Scripts/ExampleSpeechToText.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
using System.IO;
2525
using System.Collections.Generic;
2626
using IBM.Watson.DeveloperCloud.Connection;
27+
using UnityEngine.Networking;
28+
using Utility = IBM.Watson.DeveloperCloud.Utilities.Utility;
2729

2830
public class ExampleSpeechToText : MonoBehaviour
2931
{
@@ -700,25 +702,27 @@ private IEnumerator Delay(float delayTime)
700702
private IEnumerator DownloadAcousticResource()
701703
{
702704
Log.Debug("ExampleSpeechToText.DownloadAcousticResource()", "downloading acoustic resource from {0}", _acousticResourceUrl);
703-
WWW www = new WWW(_acousticResourceUrl);
704-
yield return www;
705+
using (UnityWebRequest unityWebRequest = UnityWebRequest.Get(_acousticResourceUrl))
706+
{
707+
yield return unityWebRequest.SendWebRequest();
705708

706-
Log.Debug("ExampleSpeechToText.DownloadAcousticResource()", "acoustic resource downloaded");
707-
_acousticResourceData = www.bytes;
708-
_isAudioLoaded = true;
709-
www.Dispose();
709+
Log.Debug("ExampleSpeechToText.DownloadAcousticResource()", "acoustic resource downloaded");
710+
_acousticResourceData = unityWebRequest.downloadHandler.data;
711+
_isAudioLoaded = true;
712+
}
710713
}
711714

712715
private IEnumerator DownloadOggResource()
713716
{
714717
Log.Debug("ExampleSpeechToText", "downloading ogg resource from {0}", _oggResourceUrl);
715-
WWW www = new WWW(_oggResourceUrl);
716-
yield return www;
718+
using (UnityWebRequest unityWebRequest = UnityWebRequest.Get(_oggResourceUrl))
719+
{
720+
yield return unityWebRequest.SendWebRequest();
717721

718-
Log.Debug("ExampleSpeechToText", "ogg resource downloaded");
719-
_oggResourceData = www.bytes;
720-
_isOggLoaded = true;
721-
www.Dispose();
722+
Log.Debug("ExampleSpeechToText", "ogg resource downloaded");
723+
_oggResourceData = unityWebRequest.downloadHandler.data;
724+
_isOggLoaded = true;
725+
}
722726
}
723727

724728
private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)

Scripts/UnitTests/TestSpeechToText.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
using System.Collections.Generic;
2626
using System.IO;
2727
using UnityEngine;
28+
using UnityEngine.Networking;
29+
using Utility = IBM.Watson.DeveloperCloud.Utilities.Utility;
2830

2931
namespace IBM.Watson.DeveloperCloud.UnitTests
3032
{
@@ -704,23 +706,20 @@ private IEnumerator Delay(float delayTime)
704706

705707
private IEnumerator DownloadAcousticResource()
706708
{
707-
Log.Debug("ExampleSpeechToText.DownloadAcousticResource()", "downloading acoustic resource from {0}", _acousticResourceUrl);
708-
WWW www = new WWW(_acousticResourceUrl);
709-
while (!www.isDone)
709+
Log.Debug("TestSpeechToText.DownloadAcousticResource()", "downloading acoustic resource from {0}", _acousticResourceUrl);
710+
using (UnityWebRequest unityWebRequest = UnityWebRequest.Get(_acousticResourceUrl))
710711
{
711-
yield return null;
712-
}
713-
yield return www;
712+
yield return unityWebRequest.SendWebRequest();
714713

715-
Log.Debug("ExampleSpeechToText.DownloadAcousticResource()", "acoustic resource downloaded");
716-
_acousticResourceData = www.bytes;
717-
_isAudioLoaded = true;
718-
www.Dispose();
714+
Log.Debug("TestSpeechToText.DownloadAcousticResource()", "acoustic resource downloaded");
715+
_acousticResourceData = unityWebRequest.downloadHandler.data;
716+
_isAudioLoaded = true;
717+
}
719718
}
720719

721720
private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
722721
{
723-
Log.Error("ExampleSpeechToText.OnFail()", "Error received: {0}", error.ToString());
722+
Log.Error("TestSpeechToText.OnFail()", "Error received: {0}", error.ToString());
724723
}
725724
}
726725
}

0 commit comments

Comments
 (0)