Created
June 26, 2014 19:54
-
-
Save Clancey/9cd2d6d5aaaba6bb2c89 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static List<AVAudioPlayer> players = new List<AVAudioPlayer>(); | |
static async Task<bool> PlayAudioFile(string path) | |
{ | |
var tcs = new TaskCompletionSource<bool> (); | |
var player = AVAudioPlayer.FromUrl (NSUrl.FromFilename (path)); | |
players.Add (player); | |
player.FinishedPlaying += (object sender, AVStatusEventArgs e) => { | |
Console.WriteLine ("done"); | |
tcs.TrySetResult(true); | |
}; | |
Console.WriteLine ("play"); | |
player.Play (); | |
var result = await tcs.Task; | |
Console.WriteLine ("dispose"); | |
players.Remove (player); | |
player.Dispose(); | |
return result; | |
} | |
public static Task Speak (string text) { | |
var speechSynthesizer = new AVSpeechSynthesizer (); | |
var tcs = new TaskCompletionSource<bool> (); | |
var speechUtterance = new AVSpeechUtterance (text) { | |
Rate = AVSpeechUtterance.MaximumSpeechRate/4, | |
//Voice = AVSpeechSynthesisVoice.FromLanguage ("en-AU"), | |
//Volume = 0.5f, | |
//PitchMultiplier = 1.5f | |
}; | |
EventHandler<AVSpeechSynthesizerUteranceEventArgs> del = null; | |
del = (object sender, AVSpeechSynthesizerUteranceEventArgs e) => { | |
if(e.Utterance == speechUtterance){ | |
tcs.TrySetResult(true); | |
speechSynthesizer.DidFinishSpeechUtterance -= del; | |
} | |
}; | |
speechSynthesizer.DidFinishSpeechUtterance += del; | |
speechSynthesizer.SpeakUtterance (speechUtterance); | |
return tcs.Task; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment