在app中播放声音(Play sound in app)

这是我在我的应用程序中播放声音的方式

NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], @"/sound.wav"]; SystemSoundID soundID; NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID); AudioServicesPlaySystemSound(soundID);

上面的代码在模拟器中运行良好,但在设备上运行不正常。 你能推荐一种简单的声音播放方式吗? 我知道AVAudioPlayer,但我正在寻找更简单的东西。 像上面的代码一样。

Here is how I play a sound in my app

NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], @"/sound.wav"]; SystemSoundID soundID; NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID); AudioServicesPlaySystemSound(soundID);

The code above works well in simulator, but not on device. Can you recommend me a simple way to play sound ? I know about AVAudioPlayer, but I'm looking for something simpler. Like the code above.

最满意答案

我就是这样做的,它在模拟器和真实设备上都能正常工作

SystemSoundID sounds[10]; NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"applause" ofType:@"mp3"]; CFURLRef soundURL = (__bridge CFURLRef)[NSURL fileURLWithPath:soundPath]; AudioServicesCreateSystemSoundID(soundURL, &sounds[0]); AudioServicesPlaySystemSound(sounds[0]);

希望这可以帮助

This is how I do it and it works fine both on Simulator and real Device

SystemSoundID sounds[10]; NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"applause" ofType:@"mp3"]; CFURLRef soundURL = (__bridge CFURLRef)[NSURL fileURLWithPath:soundPath]; AudioServicesCreateSystemSoundID(soundURL, &sounds[0]); AudioServicesPlaySystemSound(sounds[0]);

Hope this helps

更多推荐