我正在制作一个 iOS 8 Objective-C 应用程序(部署在我的 iPhone 5 上),我正在使用此代码通过应用程序的手机播放声音:
@property (assign) SystemSoundID scanSoundID;
...
- (void)someFunction {
...
//Play beep sound.
NSString *scanSoundPath = [[NSBundle mainBundle]
pathForResource:@"beep" ofType:@"caf"];
NSURL *scanSoundURL = [NSURL fileURLWithPath:scanSoundPath];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)scanSoundURL, &_scanSoundID);
AudioServicesPlaySystemSound(self.scanSoundID);
...
}
这段代码运行良好,但 beep.caf 的声音很大。我想以 50% 的音量播放哔声(不改变 iPhone 的音量)。换句话说,我不想触摸 iPhone 的实际音量,我只想播放幅度较小的声音。
我怎样才能做到这一点(最好使用我现在使用的音频服务)?
更新
在尝试实施路易斯的回答后,这段代码没有播放任何音频(即使我的手机没有静音并且我的音量已经调高):
NSString *scanSoundPath = [[NSBundle mainBundle] pathForResource:@"beep"
ofType:@"caf"];
NSURL *scanSoundURL = [NSURL fileURLWithPath:scanSoundPath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:scanSoundURL
error:nil];
player.volume = 0.5;
[player play];