播放系统声音而不导入自己的声音

Oks*_*ana 43 iphone objective-c ios

是否可以在不导入自己的声音的情况下播放现有的系统声音?

小智 53

我发现这个systemSoundID列表对于直接访问声音ID非常有用.
http://iphonedevwiki.net/index.php/AudioServices

例如,播放按键音响.

#define systemSoundID    1104
AudioServicesPlaySystemSound (systemSoundID);
Run Code Online (Sandbox Code Playgroud)

您还需要在项目中添加AudioToolbox框架,并添加#include <AudioToolbox.h>到.m或.h文件中.


Kri*_*dra 39

此代码播放苹果系统声音"Tock.aiff"..我相信你可以使用它播放不同的系统声音

NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:@"Tock" ofType:@"aiff"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
AudioServicesDisposeSystemSoundID(soundID);
Run Code Online (Sandbox Code Playgroud)

看到这个帖子

Apple关于系统声音的文档

https://developer.apple.com/documentation/audiotoolbox/system_sound_services

  • 不要立即"处置"soundID,你不会听到任何声音,(至少当我这样做的时候)你必须把它处理掉......可能在dealloc或viewDidUnload中. (7认同)
  • 在链接的示例项目中,它在读取中明确指出没有可用的系统声音(在ios4中).我不认为使用uikit包中的命名文件是一个好主意,因为它可以随时更改.链接线程中从模拟器包中提取声音并将其包含在您自己的包中的想法更好,如果仍然有点不稳定. (6认同)
  • 这个答案在ios 7中对我不起作用.你可以看看这个:https://github.com/TUNER88/iOSSystemSoundsLibrary (3认同)

小智 28

您可以将此用于所有默认系统音频.

例如,对于tap声音用户:

AudioServicesPlaySystemSound(1104);
Run Code Online (Sandbox Code Playgroud)

对于正面声音,请使用:

 AudioServicesPlaySystemSound(1054);
Run Code Online (Sandbox Code Playgroud)

并且,负面声音使用此:

 AudioServicesPlaySystemSound(1053);
Run Code Online (Sandbox Code Playgroud)

您可以在此处查看完整列表.


TUN*_*R88 16

所有系统声音列表:iOSSystemSoundsLibrary

您可以播放所有这些声音:

AudioServicesPlaySystemSound (systemSoundID);
Run Code Online (Sandbox Code Playgroud)


Sru*_*Suk 6

改编自@ yannc2021

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

如果你想在Swift中使用系统声音

// import this
import AVFoundation

// add this method
required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

// declared system sound here
let systemSoundID: SystemSoundID = 1104

// to play sound
AudioServicesPlaySystemSound (systemSoundID)
Run Code Online (Sandbox Code Playgroud)


Jac*_*ack 6

斯威夫特 4 +

注意:仅在真实设备上试用:

import AVKit
AudioServicesPlaySystemSound(1007);
Run Code Online (Sandbox Code Playgroud)

或者您可以尝试使用 URL 作为 -

let url = URL(fileURLWithPath: "/System/Library/Audio/UISounds/payment_success.caf")
var soundID: SystemSoundID = 0
AudioServicesCreateSystemSoundID(url as CFURL, &soundID)
AudioServicesPlaySystemSound(soundID);
Run Code Online (Sandbox Code Playgroud)

https://github.com/klaas/SwiftySystemSounds/blob/master/README.md