首页 文章

如何在 Objective-C 中以较低的音量播放系统声音?

提问于
浏览
2

我正在制作 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];

3 回答

  • 3

    您的 UPDATE 代码创建player作为局部变量,当方法(您在其中调用此代码)返回时,该变量将超出范围。

    一旦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];
    
  • 2

    从“播放 UI 声音效果或使用系统声音服务调用振动”下的多媒体编程指南

    此外,当您使用 AudioServicesPlaySystemSound 函数时:

    • 声音播放当前系统音量,没有可用的编程音量控制

    因此,根据 Apple Docs,似乎不可能。但是AVAudioPlayer类允许你通过它的volume属性来实现。

    在您当前的代码中,您所要做的就是添加

    AVAudioPlayer *newPlayer = 
            [[AVAudioPlayer alloc] initWithContentsOfURL: scanSoundURL
                                                   error: nil];
    [newPlayer play];
    
  • 0

    目标 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);
    
    }
    

相关问题