Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

Commit a13dfde

Browse files
committed
Reworked to be cleaner UX
1 parent e2ca7d7 commit a13dfde

File tree

3 files changed

+122
-37
lines changed

3 files changed

+122
-37
lines changed

WebServices/CognitiveSpeechService/CognitiveSpeechService/CognitiveSpeechService/App.xaml.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public App()
1010
{
1111
InitializeComponent();
1212

13-
MainPage = new MainPage();
13+
MainPage = new NavigationPage(new MainPage());
1414
}
1515

1616
protected override void OnStart()

WebServices/CognitiveSpeechService/CognitiveSpeechService/CognitiveSpeechService/MainPage.xaml

+34-13
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,43 @@
44
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
mc:Ignorable="d"
7-
x:Class="CognitiveSpeechService.MainPage">
7+
x:Class="CognitiveSpeechService.MainPage"
8+
Title="Speech Services Transcription"
9+
Padding="10,35,10,10">
810

911
<StackLayout>
10-
<Button Text="Enable Microphone"
11-
HorizontalOptions="CenterAndExpand"
12-
VerticalOptions="Start"
13-
Clicked="EnableMicClicked" />
12+
<Frame BorderColor="DarkGray"
13+
CornerRadius="10"
14+
HeightRequest="300"
15+
WidthRequest="280"
16+
HorizontalOptions="Center"
17+
VerticalOptions="Start"
18+
BackgroundColor="LightGray">
19+
<ScrollView x:Name="scroll">
20+
<Label x:Name="transcribedText"
21+
Margin="10,10,10,10"
22+
/>
23+
</ScrollView>
24+
</Frame>
1425

15-
<Button Text="Start Transcribing"
16-
HorizontalOptions="Center"
17-
VerticalOptions="Start"
18-
Clicked="StartTranscribingClicked"/>
19-
<Label x:Name="transcribedText"
20-
Text="Press the Start Transcribing button to begin transcribing speech..."
21-
HorizontalOptions="Center"
22-
VerticalOptions="CenterAndExpand" />
26+
<ActivityIndicator x:Name="transcribingIndicator"
27+
HorizontalOptions="Center"
28+
VerticalOptions="Start"
29+
WidthRequest="300"
30+
IsRunning="False" />
31+
<Button x:Name="transcribeButton"
32+
WidthRequest="300"
33+
HeightRequest="50"
34+
Text="Transcribe"
35+
TextColor="White"
36+
CornerRadius="10"
37+
BackgroundColor="Green"
38+
BorderColor="DarkGray"
39+
BorderWidth="1"
40+
FontAttributes="Bold"
41+
HorizontalOptions="Center"
42+
VerticalOptions="Start"
43+
Clicked="TranscribeClicked"/>
2344
</StackLayout>
2445

2546
</ContentPage>

WebServices/CognitiveSpeechService/CognitiveSpeechService/CognitiveSpeechService/MainPage.xaml.cs

+87-23
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,116 @@ namespace CognitiveSpeechService
1313
public partial class MainPage : ContentPage
1414
{
1515
SpeechRecognizer recognizer;
16-
IMicrophoneService microphoneService;
16+
IMicrophoneService micService;
17+
bool isTranscribing = false;
1718

1819
public MainPage()
1920
{
2021
InitializeComponent();
2122

22-
microphoneService = DependencyService.Resolve<IMicrophoneService>();
23+
micService = DependencyService.Resolve<IMicrophoneService>();
2324
}
2425

25-
async void EnableMicClicked(object sender, EventArgs e)
26+
async void TranscribeClicked(object sender, EventArgs e)
2627
{
27-
bool isMicEnabled = await microphoneService.GetPermissionAsync();
28-
if(!isMicEnabled)
28+
bool isMicEnabled = await micService.GetPermissionAsync();
29+
30+
// Speech recognizer
31+
if(recognizer == null)
2932
{
30-
UpdateTranscription("Please grant access to the microphone!", true);
33+
var config = SpeechConfig.FromSubscription(Constants.CognitiveServicesApiKey, Constants.CognitiveServicesRegion);
34+
recognizer = new SpeechRecognizer(config);
35+
recognizer.Recognized += (obj, args) =>
36+
{
37+
UpdateTranscription(args.Result.Text);
38+
};
3139
}
32-
}
3340

34-
void StartTranscribingClicked(object sender, EventArgs e)
35-
{
36-
var config = SpeechConfig.FromSubscription(Constants.CognitiveServicesApiKey, Constants.CognitiveServicesRegion);
41+
// EARLY OUT: make sure mic is accessible
42+
if (!isMicEnabled)
43+
{
44+
UpdateTranscription("Please grant access to the microphone!");
45+
return;
46+
}
47+
48+
// if already transcribing, stop speech recognizer
49+
if(isTranscribing)
50+
{
51+
Device.BeginInvokeOnMainThread(() =>
52+
{
53+
transcribingIndicator.IsRunning = false;
54+
});
3755

38-
UpdateTranscription("", true);
56+
try
57+
{
58+
await recognizer.StopContinuousRecognitionAsync();
59+
}
60+
catch(Exception ex)
61+
{
62+
UpdateTranscription(ex.Message);
63+
}
64+
65+
isTranscribing = false;
66+
}
3967

40-
recognizer = new SpeechRecognizer(config);
41-
recognizer.StartContinuousRecognitionAsync();
42-
recognizer.Recognized += (obj, args) =>
68+
// if not transcribing, clear existing text and start speech recognizer
69+
else
4370
{
44-
UpdateTranscription(args.Result.Text);
45-
};
71+
Device.BeginInvokeOnMainThread(() =>
72+
{
73+
transcribingIndicator.IsRunning = true;
74+
InsertDateTimeRecord();
75+
});
76+
77+
try
78+
{
79+
await recognizer.StartContinuousRecognitionAsync();
80+
}
81+
catch(Exception ex)
82+
{
83+
UpdateTranscription(ex.Message);
84+
}
85+
86+
isTranscribing = true;
87+
}
88+
89+
UpdateButton();
4690
}
4791

48-
void UpdateTranscription(string newText, bool clearText = false)
92+
void UpdateTranscription(string newText)
4993
{
50-
// Force on main thread since this is likely called
51-
// from a background event
5294
Device.BeginInvokeOnMainThread(() =>
5395
{
54-
if(clearText)
96+
if(!string.IsNullOrWhiteSpace(newText))
5597
{
56-
transcribedText.Text = "";
98+
transcribedText.Text += $"{newText}\n";
99+
scroll.ScrollToAsync(0, scroll.Height, true);
57100
}
58-
59-
transcribedText.Text += newText;
60101
});
61102

62103
}
104+
105+
void InsertDateTimeRecord()
106+
{
107+
var msg = $"=================\n{DateTime.Now.ToString()}\n=================";
108+
UpdateTranscription(msg);
109+
}
110+
111+
void UpdateButton()
112+
{
113+
Device.BeginInvokeOnMainThread(() =>
114+
{
115+
if(isTranscribing)
116+
{
117+
transcribeButton.Text = "Stop";
118+
transcribeButton.BackgroundColor = Color.Red;
119+
}
120+
else
121+
{
122+
transcribeButton.Text = "Transcribe";
123+
transcribeButton.BackgroundColor = Color.Green;
124+
}
125+
});
126+
}
63127
}
64128
}

0 commit comments

Comments
 (0)