I'm creating a speech interactive App using UWP. which can show news and weather information, also can play music (I have done that part),but I have some problems about speech recognition:
The functions that I expect:
first command must include "Jason" to activate the App, after that you just need to say the function you want not need to add Jason anymore (like"show me some news."), and after 30 secs, this activate section end, your command need to add "Jason" again to activate the App.
The function I have achieved so far:
Can continuously recognize user's speech, but every command must has "Jason" to trigger the function (like "Jason, show me some news.")
The following is the code I have used.
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
SpeechRecognizer contSpeechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();
await contSpeechRecognizer.CompileConstraintsAsync();
contSpeechRecognizer.ContinuousRecognitionSession.ResultGenerated += ContinuousRecognitionSession_ResultGenerated;
contSpeechRecognizer.ContinuousRecognitionSession.AutoStopSilenceTimeout = TimeSpan.FromDays(1);
contSpeechRecognizer.ContinuousRecognitionSession.Completed += ContinuousRecognitionSession_Completed;
await contSpeechRecognizer.ContinuousRecognitionSession.StartAsync();
}
private async void ContinuousRecognitionSession_Completed(SpeechContinuousRecognitionSession sender, SpeechContinuousRecognitionCompletedEventArgs args)
{
await contSpeechRecognizer.ContinuousRecognitionSession.StartAsync();
}
private async void ContinuousRecognitionSession_ResultGenerated(SpeechContinuousRecognitionSession sender, SpeechContinuousRecognitionResultGeneratedEventArgs args)
{
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
speechResult = args.Result.Text;
try
{
if (speechResult.Contains("jason"))
{
Functions();//Control functions
}
catch
{
}
});
}
Questions: 1.How can I change to achieve the expected function? 2. Can grammar solve this problem? 3. And how to add the grammar?
Related
In one stage of my app (Android & iOS are the ones we care about) we've got three pages which take in details and then open a webView for the user to input their card details to take a payment - this can't be done in the app due to Apple's guidelines.
I need to format the navigation in a way that when the user has finished in the webView it closes and then closes the 3 previous modals to get back to the original page. I've got it all working with the Appearing event so each page just closes itself:
this.Appearing += async (s, e) =>
{
await Navigation.PopModalAsync();
};
The issue I'm now having is that when the user presses the back button on the phone, it closes all of the pages that they've been through already & back to the original. I thought about implementing a custom nav bar and disabling the back button on the hardware but this would cause the same problem with the Appearing event.
Is there any easy way to solve this?
EDIT: Relevant code;
async void OnButtonClicked(object sender, EventArgs eventArgs)
{
if (IsConnected)
{
ActivityIndicator.IsVisible = true;
var button = (Button) sender;
button .IsEnabled = false;
await Navigation.PushModalAsync(new Page());
this.Appearing += (s, e) =>
{
ActivityIndicator.IsVisible = false;
button.IsEnabled = true;
RefreshPage();
};
}
else
{
NoInternetLabel.IsVisible = true;
}
}
Use this:
YourButton.Clicked += OpenPage;
OpenPage looks like this:
async public void OpenPage(object sender, EventArgs args)
{
await Navigation.PushAsync(new PageToShow());
}
You don't have to do anything to handle the PageToShow() closing, that happens by itself when the user presses the back button.
Managed to solve this by using Actions. In each new Page() we passed up an async method to close it once the one after had completed;
var nextPage = new Page(async () =>
{
await Navigation.PopModalAsync();
_completedSuccessfully();
});
await Navigation.PushModalAsync(nextPage);
And in the new page class;
private readonly Action _completedSuccessfully;
public Page(Action completedSuccessfully)
{
_completedSuccessfully = completedSuccessfully;
}
This meant that when the webView closed it called the completedSuccessfully() action and then chained all of them to the original page.
I am making a UWP App where I run Background Audio in the MainPage on a Button Click event. When I move to another page, there's also a different Media to play in Background Audio Task there.
How can I stop the currently playing Task to run the other? Should I define something globally? Any help regarding this issue?
Edit
I am using this sample: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BackgroundAudio While the backgroundAudio of the first Page is running, I go to the second page and on a click event I set a new List with the following code:
// First update the persisted start track
ApplicationSettingsHelper.SaveSettingsValue(ApplicationSettingsConstants.TrackId, RadioFacade.mySongs[0].MediaUri.ToString()); //here
ApplicationSettingsHelper.SaveSettingsValue(ApplicationSettingsConstants.Position, new TimeSpan().ToString());
// Start task
StartBackgroundAudioTask();
But the new song takes more than the estimated time to run and enter the else of this method:
private void StartBackgroundAudioTask()
{
AddMediaPlayerEventHandlers();
var startResult = this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
bool result = backgroundAudioTaskStarted.WaitOne(10000);
//Send message to initiate playback
if (result == true)
{
MessageService.SendMessageToBackground(new UpdatePlaylistMessage(RadioFacade.mySongs));
MessageService.SendMessageToBackground(new StartPlaybackMessage());
}
else
{
throw new Exception("Background Audio Task didn't start in expected time");
}
});
startResult.Completed = new AsyncActionCompletedHandler(BackgroundTaskInitializationCompleted);
}
and the old (first playing) song keeps playing.
I tried to Stop the current BackgroundMediaPlayer using BackgroundMediaPLayer.Shutdown() but it didn't work.
Any idea how to let the old song stop and the current song play?
You can control the background media player by sending messages to it from the foreground. For example,
From the foreground app:
BackgroundMediaPlayer.SendMessageToBackground(new ValueSet
{
{"playnew", "some value"}
});
In your background task:
public sealed class AudioPlayer : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground;
...
...
}
private async void BackgroundMediaPlayer_MessageReceivedFromForeground(object sender, MediaPlayerDataReceivedEventArgs e)
{
object value;
if (e.Data.TryGetValue("playnew", out value) && value != null)
{
// value will be whatever you pass from the foreground.
BackgroundMediaPlayer.Current.Pause();
BackgroundMediaPlayer.Current.Source = stream source;
BackgroundMediaPlayer.Current.Play();
}
}
...
}
"playnew" can be a global constant in your application. You can use the ValueSet to pass the live stream url to the background task as the value.
I'm developing windows phone 8 application.
I need to get user current location Details.
I try with following code taken from MSDN
C# :
1 private void OneShotLocationButton_Click(object sender, RoutedEventArgs e)
2 {if ((bool)IsolatedStorageSettings.ApplicationSettings["LocationConsent"] != true)
3 {
4 return;
5 }
6 Geolocator geolocator = new Geolocator();
7 geolocator.DesiredAccuracyInMeters = 50;
8 try
9 {
10 Geoposition geoposition = await geolocator.GetGeopositionAsync(
maximumAge: TimeSpan.FromMinutes(5),
timeout: TimeSpan.FromSeconds(10)
);
LatitudeTextBlock.Text = geoposition.Coordinate.Latitude.ToString("0.00");
LongitudeTextBlock.Text = geoposition.Coordinate.Longitude.ToString("0.00");
}
catch (Exception ex)
{
if ((uint)ex.HResult == 0x80004004)
{
// the application does not have the right capability or the location master switch is off
StatusTextBlock.Text = "location is disabled in phone settings.";
}
//else
{
// something else happened acquring the location
}
}
}
I Got error in line number 10.
The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.
I 'm new to windows phone application. now only i start the learn basic in WP8 .
Plz tell how to solve this ...
In your code you have used await geolocator.GetGeopositionAsync() now you can only use await with async methods.
so what you have to do is whenever you used async method just declare the method aysnc
like:private async void OneShotLocationButton_Click(object sender, RoutedEventArgs e)
instead of private void OneShotLocationButton_Click(object sender, RoutedEventArgs e)
Follow the error instruction. Change
private void OneShotLocationButton_Click
to
private async void OneShotLocationButton_Click
using c#, VS 2013
Try to add some background task to my Store app (idea to show toast if some data in my Json file contains current date and time).
What was done:
1.Create Windows Runtime Component, that implement IBackgroundTask , add reference to my Windows Store App. Inside WRC create class that contains next code :
namespace BackgroundTask
{
public sealed class EventChecker: IBackgroundTask
{
ThreadPoolTimer _periodicTimer = null;
public void Run(IBackgroundTaskInstance taskInstance)
{
_periodicTimer
= ThreadPoolTimer.CreatePeriodicTimer(new TimerElapsedHandler(PeriodicTimerCallback), TimeSpan.FromSeconds(30));
}
private void PeriodicTimerCallback(ThreadPoolTimer timer)
{
CheckEventAndShowToast();
}
....
}
2.Register task : In MainPage.xaml.cs add in method OnNavigatedTo registering of this background task. Code:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
navigationHelper.OnNavigatedTo(e);
var taskName = "EventCheckerTask";
if (BackgroundTaskRegistration.AllTasks.Count > 0)
{
foreach (var cur in BackgroundTaskRegistration.AllTasks)
{
if (cur.Value.Name != taskName)
{
BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
builder.Name = taskName;
builder.TaskEntryPoint = "BackgroundTask.EventChecker";
BackgroundTaskRegistration taskToRegister = builder.Register();
}
}
}
else
{
BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
builder.Name = taskName;
builder.TaskEntryPoint = "BackgroundTask.EventChecker";
BackgroundTaskRegistration taskToRegister = builder.Register();
}
}
Use MSDN1, MSDN2, MSDN3 links.
Also OnComplete not implement - because i don't need it (or I must to implement it anyway?)
3.Declare in manifest.
Set toast capable to "YES":
Declare background Task:
4.Check functionality of all method that i want to use for background - all Ok and work
Durring debugging all it's ok, no errors/ exceptions, but nothing happend. Try to debug step by step - looks like all it's ok, think i make some mistake in code.
So question: where i'm wrong, why i cant launch my background task that must to check data and do required action if some conditions are as required?
EDIT
Part 2 - Try to realize background task in new solution.
What was done:
Create new simple CRC :
namespace Tasks
{
public sealed class Tasks : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
//for checking place debug point
//TODO something
}
}
}
Also in main.xaml.cs placed next code:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
CheckTaskRegistration();
}
private void CheckTaskRegistration()
{
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name == "Tasks")
{
isTaskRegistered = true;
break;
}
}
if (!isTaskRegistered)
{
RegisterBackgroundTask2("Tasks", "Tasks.Tasks");
}
}
private void RegisterBackgroundTask2(string name, string entrypoint)
{
BackgroundTaskBuilder btb = new BackgroundTaskBuilder();
btb.Name = name;
btb.TaskEntryPoint = entrypoint;
//IBackgroundTrigger everyMinuteTrigger = new TimeTrigger(1, false);
// btb.SetTrigger(everyMinuteTrigger);
btb.SetTrigger(new SystemTrigger(SystemTriggerType.InternetAvailable, false));
BackgroundTaskRegistration task = btb.Register();
}
As result got, that with this trigger btb.SetTrigger(new SystemTrigger(SystemTriggerType.InternetAvailable, false)); all works - i can go inside Run method, but if I try to use TimeTrigger like
//IBackgroundTrigger everyMinuteTrigger = new TimeTrigger(1, false);
// btb.SetTrigger(everyMinuteTrigger);
nothing happend - wait few minutes try few times (placed instead prev trigger registration).
Question - Why? Do i must to do something more?
Also old questions are without answers too...
Also try to use it with my App - all worksperfect, but only if i connect to lan... But why it's not work with time trigger?
A spend a little bit more time and found few root causes for my problem:
I must to use some trigger with my BackgroundTask if I want to use it and launch. problem here that ther is not exactly what i need exist (or maybe i need to read a little bit more about triggers).
So if I add some trigger, BackgroundTask can be launched after such event happend. Example:
//Time trigger
IBackgroundTrigger everyMinuteTrigger = new TimeTrigger(15, false);
btb.SetTrigger(everyMinuteTrigger);
//one of the standart tirgger
btb.SetTrigger(new SystemTrigger(SystemTriggerType.InternetAvailable, false));
If I want to use TimeTrigger, in EDIT i write code with TimeTrigger(1, false);, but after reading some more detailed documentation found "that the time trigger only accepts values greater than or equal to 15; smaller values fail during Register." MSDN
Also if I want to use TimeTrigger i must to add LockScreenNotification support to my App
Currently I can launch backgroundTask every 15 min, but it's not exactly what i want...
So, regarding this post quation - i found answer, but still need to read more deeply about BackgroundTask
Is your CheckEventAndShowToast(); calling anything asynchronously?
If that is the case then you should follow step 4 from the MSDN2 link you posted.
"4. If you run any asynchronous code in your background task, then your background task needs to use a deferral. If you don't use a deferral, then the background task process can terminate unexpectedly if the Run method completes before your asynchronous method call has completed."
I working on a Windows Phone 7 app with Text-To-Speech capabilities. I'm using Text-To-Speech with Microsoft Translator Service and the following C# code...
// Text-To-Speech with Microsoft Translator Service (http://translatorservice.codeplex.com/)
private void TextToSpeech_Play(object sender, EventArgs e)
{
SpeechSynthesizer speech = new SpeechSynthesizer(CLIENT_ID, CLIENT_SECRET);
//string content = "This is a beautiful day!";
string language = "en";
//speech.SpeakAsync(content, language);
foreach (UIElement element in todayschapter.Children)
{
if (element is TextBlock)
{
string content = (element as TextBlock).Text;
speech.SpeakAsync(content, language);
}
}
}
In this instance, todayschapter is a StackPanel and its Children are TextBlocks. I'm wanting to simply play audio of each TextBlock, in succession. The problem is that it is play the audio of EVERY TextBlock at the same time.
I have a sneaking suspicion that the problem is SpeakAsync(), but I'm not sure. The documentation shows Speak(), but that isn't available (maybe a different version) in the Visual Studio methods helper dropdown (little thing that shows as you type - not sure what it's called).
Is there a way to make it wait for each play to finish before playing the next? Is foreach not the right choice, for this?
As always, if my code just looks stupid, please recommend better ways. I'm very much a beginner programmer.
Just use the Speak instead of the async call, since you want to have it one after another anyway.
The SpeakAsync call is indeed the problem. Unfortunately, since SpeakAsync doesn't return a Task, you can't just convert this to await SpeakAsync() (which would be the most straightforward conversion).
And, looking at the source code, it doesn't fire an event to tell when it's done. So let's add one (in SpeechSynthesizer.cs):
public event EventHandler<SpeechEventArgs> SpeakCompleted;
public void SpeakAsync(string text, string language)
{
this.GetSpeakStreamAsyncHelper(text, language, result =>
{
if (result.Error == null)
{
SoundEffect effect = SoundEffect.FromStream(result.Stream);
FrameworkDispatcher.Update();
effect.Play();
this.OnSpeakCompleted(new SpeechEventArgs(result.Error)); // added to call completion handler
}
else
{
this.OnSpeakFailed(new SpeechEventArgs(result.Error));
}
});
}
// new function
private void OnSpeakCompleted(SpeechEventArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
if (SpeakCompleted != null)
SpeakCompleted(this, e);
});
}
Now, you'll need to handle the SpeakCompleted event and start speaking the next string.
Something like this (I haven't even compiled this, so be warned):
private Queue<string> utterances;
private SpeechSynthesizer speech;
private void TextToSpeech_Play(object sender, EventArgs e)
{
speech = new SpeechSynthesizer(CLIENT_ID, CLIENT_SECRET);
speech.SpeechCompleted += new EventHandler<SpeechEventArgs>(TextToSpeech_Completed);
foreach (UIElement element in todayschapter.Children)
{
if (element is TextBlock)
{
string content = (element as TextBlock).Text;
utterances.Enqueue(content);
}
}
TextToSpeech_Completed(null, null); // start speaking the first one
}
private void TextToSpeech_Completed(object sender, SpeechEventArgs e)
{
if (utterances.Any())
{
string contents = utterances.Dequeue();
speech.SpeakAsync(contents);
}
}