如何使用 AVAudioPlayer 播放单词数组或 NSString?

How to play array of words or NSString using AVAudioPlayer?

我有一系列的单词,我想一个接一个地选择并使用 AVAudioPlayer 播放,我怎样才能使用 AVAudioPlayer 播放 NSString?

我没有音频文件,但我有单词列表,我想使用 AVAudioPlayer 编程播放每个单词。

我发现可以使用 AVAudioPlayer 播放音频文件,但无法在 AVAudioPlayer 中播放 NSSting 或 word

下面是用于播放音频片段的代码:

NSString *path = [NSString stringWithFormat:@"%@/audio.mp3", [[NSBundle mainBundle] resourcePath]];
NSURL *soundUrl = [NSURL fileURLWithPath:path];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];

您需要创建一个 AVSpeechUtterance object using the words that you want the device to "speak," and then pass that object to AVSpeechSynthesizer。那些 类 提供了你需要的控制语音的方法;没有必要使用 AVAudioPlayer.