xcode 在 iOS 中播放系统声音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15304111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:55:56  来源:igfitidea点击:

playing system sounds in iOS

iphoneiosobjective-cxcodeipad

提问by moshikafya

I am trying to understand how to play those sounds in iOS. I did found a few code snippets but they dont play anything when I use it. What am I doing wrong?

我试图了解如何在 iOS 中播放这些声音。我确实找到了一些代码片段,但是当我使用它时它们没有播放任何内容。我究竟做错了什么?

http://iphonedevwiki.net/index.php/AudioServices

http://iphonedevwiki.net/index.php/AudioServices

- (IBAction) playSystemSound: (id) sender {

    AudioServicesPlaySystemSound (1100);
}

采纳答案by nsgulliver

If you are using valid soundID and playing the sound on device then the code you have presented should work.

如果您使用有效的 soundID 并在设备上播放声音,那么您提供的代码应该可以工作。

Alternatively you can use the following code to play the custom sound or audio file if it helps you

或者,如果对您有帮助,您可以使用以下代码播放自定义声音或音频文件

- (IBAction) playSystemSound: (id) sender {
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"audio" ofType:@"mp3"];
    SystemSoundID soundID;
    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
    AudioServicesPlaySystemSound (soundID);
}

Apple has provided sample codefor playing sounds you could check that out also.

Apple 提供了播放声音的示例代码,您也可以查看。