简体   繁体   English

iOS声音播放音量不尊重音量变化或静音

[英]iOS sound playback volume not respecting volume change or mute

This is my current code 这是我目前的代码

void playSound(NSString* myString) {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    NSString *string = myString;
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, 
                        (__bridge CFStringRef) string, CFSTR ("wav"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

It works, but it doesn't let you change the volume on the device or mute it with the mute switch. 它可以工作,但它不允许您更改设备上的音量或使用静音开关将其静音。 What is the simplest way to enable these features? 启用这些功能的最简单方法是什么? Thanks. 谢谢。

Why don't you use AVFoundation ? 你为什么不用AVFoundation If all you want is to play simple sound effects, it would be a much better choice as it is a higher level API. 如果您只想播放简单的声音效果,那么它将是一个更好的选择,因为它是更高级别的API。

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:filePath];

// Assuming audioPlayer is an ivar.
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;

if (audioPlayer == nil)
    NSLog([error description]);             
else 
    [audioPlayer play];

AVAudioPlayer should respect the volume settings of the user. AVAudioPlayer应该尊重用户的音量设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM