示例

1import { Button, List, Navigation, NavigationStack, Path, Script, Section, Text, } from "scripting"
2
3function Example() {
4  const dismiss = Navigation.useDismiss()
5
6  return <NavigationStack>
7    <List
8      navigationTitle={"Speech Example"}
9      navigationBarTitleDisplayMode={"inline"}
10      toolbar={{
11        cancellationAction: <Button
12          title={"Done"}
13          action={dismiss}
14        />
15      }}
16    >
17      <Section
18        footer={
19          <Text>Activate the SharedAudioSeesion, and speak a text.</Text>
20        }
21      >
22        <Button
23          title={"Speak a text"}
24          action={async () => {
25            console.present()
26            if (await Speech.isSpeaking) {
27              await Speech.stop('immediate')
28              console.log("Stopped.")
29              return
30            }
31
32            await SharedAudioSession.setActive(true)
33            await SharedAudioSession.setCategory('playback', ['mixWithOthers'])
34
35            const listener = () => {
36              console.log("Speak completed!")
37              Speech.removeListener('finish', listener)
38            }
39
40            Speech.addListener('finish', listener)
41
42            await Speech.speak('Hi there, welcome to Scripting! I wish this app is helpful to you.', {
43              voiceLanguage: 'en-US',
44            })
45
46            console.log("Started, tap the run button to stop.")
47          }}
48        />
49      </Section>
50      <Section
51        footer={
52          <Text>Synthesize text to the file stored in local documents directory.</Text>
53        }
54      >
55        <Button
56          title={"synthesize to File"}
57          action={async () => {
58            console.present()
59            const filePath = Path.join(FileManager.documentsDirectory, 'greeting.caf')
60            const listener = () => {
61              if (FileManager.existsSync(filePath)) {
62                console.log("Audio file is saved to " + filePath + ". Start to play it.")
63
64                let player = new AVPlayer()
65                player.setSource(filePath)
66                player.onReadyToPlay = () => {
67                  player.play()
68                }
69                player.onEnded = () => {
70                  player.dispose()
71                }
72              } else {
73                console.log("Failed to save audio file.")
74              }
75              Speech.removeListener('finish', listener)
76            }
77
78            Speech.addListener('finish', listener)
79
80            await Speech.synthesizeToFile(
81              'Hi there, welcome to Scripting! I wish this app is helpful to you.',
82              filePath, {
83              voiceLanguage: 'en-US',
84            })
85          }}
86        />
87      </Section>
88    </List>
89  </NavigationStack>
90}
91
92async function run() {
93  await Navigation.present({
94    element: <Example />
95  })
96
97  Script.exit()
98}
99
100run()