示例

1import { Button, List, Navigation, NavigationStack, Script, Section, Text, } from "scripting"
2
3function Example() {
4  const dismiss = Navigation.useDismiss()
5
6  return <NavigationStack>
7    <List
8      navigationTitle={"SpeechRecognition"}
9      navigationBarTitleDisplayMode={"inline"}
10      toolbar={{
11        cancellationAction: <Button
12          title={"Done"}
13          action={dismiss}
14        />
15      }}
16    >
17      <Section
18        footer={
19          <Text>Returns the list of locales that are supported by the speech recognizer.</Text>
20        }
21      >
22        <Button
23          title={"SpeechRecognition.supportedLocales"}
24          action={() => {
25            console.clear()
26            console.present()
27            console.log(JSON.stringify(SpeechRecognition.supportedLocales, null, 2))
28          }}
29        />
30      </Section>
31
32      <Section
33        footer={
34          <Text>Returns a boolean that indicates whether the recognizer is running.</Text>
35        }
36      >
37        <Button
38          title={"SpeechRecognition.isRecognizing"}
39          action={() => {
40            console.clear()
41            console.present()
42            console.log(
43              "SpeechRecognition.isRecognizing",
44              SpeechRecognition.isRecognizing
45            )
46          }}
47        />
48      </Section>
49
50      <Section
51        footer={
52          <Text>Start a speech audio buffer recognition request. Return a boolean value that indicates whether the operation was successfully.</Text>
53        }
54      >
55        <Button
56          title={"SpeechRecognition.start"}
57          action={async () => {
58            console.clear()
59            console.present()
60            console.log("Speech recognizing is started, it will stop after 5s.")
61
62            if (await SpeechRecognition.start({
63              locale: "en-US",
64              partialResults: false,
65              onResult: result => {
66                console.log("Result: " + result.text)
67              }
68            })) {
69              setTimeout(async () => {
70                await SpeechRecognition.stop()
71                console.log("Stoped")
72              }, 5000)
73            } else {
74              console.error("Failed to start recognizing")
75            }
76          }}
77        />
78      </Section>
79
80      <Section
81        footer={
82          <Text>Start a request to recognize speech in a recorded audio file.</Text>
83        }
84      >
85        <Button
86          title={"SpeechRecognition.recognizeFile"}
87          action={async () => {
88            console.clear()
89            console.present()
90            console.log("SpeechRecognition is started, it will stop after 5s.")
91
92            let audioFilePathToRecognize = await DocumentPicker.pickFiles({
93              types: ["public.audio"]
94            })
95
96            if (audioFilePathToRecognize.length === 0) {
97              console.log("Please pick a audio file.")
98              return
99            }
100
101            if (await SpeechRecognition.recognizeFile({
102              filePath: audioFilePathToRecognize[0],
103              partialResults: true,
104              onResult: (result) => {
105                console.log("Recognized result: " + result.text)
106              }
107            })) {
108              console.log("Started recognizing file...")
109            } else {
110              console.error("Failed to start recognizing",)
111            }
112          }}
113        />
114      </Section>
115
116      <Section
117        footer={
118          <Text>Stop speech recognition request. Return a boolean value that indicates whether the operation was successfully.</Text>
119        }
120      >
121        <Button
122          title={"SpeechRecognition.stop"}
123          action={async () => {
124            if (SpeechRecognition.isRecognizing) {
125              await SpeechRecognition.stop()
126              Dialog.alert({
127                message: "SpeechRecognition is stopped."
128              })
129            } else {
130              Dialog.alert({
131                message: "No progressing recognition."
132              })
133            }
134          }}
135        />
136      </Section>
137    </List>
138  </NavigationStack>
139}
140
141
142async function run() {
143  await Navigation.present({
144    element: <Example />
145  })
146
147  Script.exit()
148}
149
150run()