如何在 Objective-C 中以较低的音量播放系统声音?
How can I play a system sound at lower volume in Objective-C?
我正在制作一个 iOS 8 Objective-C 应用程序(部署在我的 iPhone 5 上)并且我正在使用此代码通过 phone 播放声音来自应用:
@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 的实际音量,我只想播放可以说幅度较小的声音。
我怎样才能做到这一点(最好使用我现在使用的音频服务)?
更新
在尝试实现 Louis 的回答后,这段代码没有播放任何音频(即使我的 phone 是 not 静音并且我的音量调高):
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];
来自"Playing UI Sound Effects or Invoking Vibration Using System Sound Services"下的Multimedia Programming Guide:
In addition, when you use the AudioServicesPlaySystemSound function:
- Sounds play at the current system audio volume, with no programmatic volume control available
因此,根据 Apple 文档,这似乎是不可能的。但是 AVAudioPlayer
class 让你可以通过它的 volume
属性 来做到这一点。
在您当前的代码中,您只需添加
AVAudioPlayer *newPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL: scanSoundURL
error: nil];
[newPlayer play];
您的更新代码将 player
创建为局部变量,当方法(您在其中调用此代码)returns.
时,该变量将超出范围
一旦 player
超出范围,它就会被释放,音频甚至没有机会开始播放。
您需要 retain
某处播放器:
@property AVAudioPlayer* player;
...
- (void)initializePlayer
{
NSString *scanSoundPath = [[NSBundle mainBundle] pathForResource:@"beep"
ofType:@"caf"];
NSURL *scanSoundURL = [NSURL fileURLWithPath:scanSoundPath];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:scanSoundURL
error:nil];
self.player.volume = 0.5;
}
然后你会打电话给其他地方:
[self.player play];
Objective C
使用系统音量播放声音
@interface UIViewController () {
AVAudioPlayer *audioPlayer;
}
-(void) PlayCoinSound {
//#import <AVFoundation/AVFoundation.h>
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"CoinSound" ofType:@"mp3"];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:soundPath] error: nil];
audioPlayer.volume = 1.0;
[audioPlayer prepareToPlay];
[audioPlayer play];
}
全音播放
- (void)PlayWithFullVolume {
//#import <AudioToolbox/AudioToolbox.h>
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
}
我正在制作一个 iOS 8 Objective-C 应用程序(部署在我的 iPhone 5 上)并且我正在使用此代码通过 phone 播放声音来自应用:
@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 的实际音量,我只想播放可以说幅度较小的声音。
我怎样才能做到这一点(最好使用我现在使用的音频服务)?
更新
在尝试实现 Louis 的回答后,这段代码没有播放任何音频(即使我的 phone 是 not 静音并且我的音量调高):
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];
来自"Playing UI Sound Effects or Invoking Vibration Using System Sound Services"下的Multimedia Programming Guide:
In addition, when you use the AudioServicesPlaySystemSound function:
- Sounds play at the current system audio volume, with no programmatic volume control available
因此,根据 Apple 文档,这似乎是不可能的。但是 AVAudioPlayer
class 让你可以通过它的 volume
属性 来做到这一点。
在您当前的代码中,您只需添加
AVAudioPlayer *newPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL: scanSoundURL
error: nil];
[newPlayer play];
您的更新代码将 player
创建为局部变量,当方法(您在其中调用此代码)returns.
一旦 player
超出范围,它就会被释放,音频甚至没有机会开始播放。
您需要 retain
某处播放器:
@property AVAudioPlayer* player;
...
- (void)initializePlayer
{
NSString *scanSoundPath = [[NSBundle mainBundle] pathForResource:@"beep"
ofType:@"caf"];
NSURL *scanSoundURL = [NSURL fileURLWithPath:scanSoundPath];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:scanSoundURL
error:nil];
self.player.volume = 0.5;
}
然后你会打电话给其他地方:
[self.player play];
Objective C
使用系统音量播放声音
@interface UIViewController () {
AVAudioPlayer *audioPlayer;
}
-(void) PlayCoinSound {
//#import <AVFoundation/AVFoundation.h>
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"CoinSound" ofType:@"mp3"];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:soundPath] error: nil];
audioPlayer.volume = 1.0;
[audioPlayer prepareToPlay];
[audioPlayer play];
}
全音播放
- (void)PlayWithFullVolume {
//#import <AudioToolbox/AudioToolbox.h>
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
}