[英]iOS Control Sound Volume
我正在播放這樣的聲音:
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
..
..
SystemSoundID soundID;
NSString *path = [[NSBundle mainBundle]
pathForResource:@"ClickSound" ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID);
AudioServicesPlaySystemSound (soundID);
它似乎從'Ringer'獲取它的音量,但是當我使用物理音量按鈕時它控制'音量'(所以我可以'靜音'音量 - 但我仍然聽到聲音)。
我想控制正確的音量,我不希望它在靜音時播放(順便說一下 - 當我使用靜音切換它工作時,我聽不到聲音)。
我該如何解決?
AudioServicesCreateSystemSound僅適用於振鈴器音量。
您可以使用AVAudioPlayer。 以下是一些示例代碼:
AVAudioPlayer *buttonClick=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithFormat:@"buttonClick"] ofType:@"mp3"]] error:NULL];
[buttonClick prepareToPlay];
[buttonClick play];
您無法控制代碼中的音量。 您可以提供UI控件以允許用戶更改音量。
音頻按類別管理,並且行為因尊重硬件設置而異。
Ambient
類別將遵循靜音開關; 其他人不會。
沒有類別允許您確定靜音開關的位置。
您可能應該使用以下代碼注冊音量按鈕的回撥,
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(volumeChanged:)
name:@"AVSystemController_SystemVolumeDidChangeNotification"
object:nil];
}
- (void)volumeChanged:(NSNotification *)notification
{
float volume =
[[[notification userInfo]
objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]
floatValue];
}
希望它會有所幫助。 快樂編碼:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.