【问题标题】:How can I play a system sound at lower volume in Objective-C?如何在 Objective-C 中以较低的音量播放系统声音?
【发布时间】:2015-03-01 05:50:30
【问题描述】:

我正在制作一个 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 的实际音量,我只想播放幅度较小的声音。

我怎样才能做到这一点(最好使用我现在使用的音频服务)?

更新
在尝试执行 Louis 的回答后,这段代码没有播放任何音频(即使我的手机没有静音并且我的音量已经调高):

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];

【问题讨论】:

  • hmm... 尝试实现 AVAudioPlayers 委托方法,特别是 audioPlayerDecodeErrorDidOccur:error,看看您在尝试播放时是否遇到错误。 Idk 如果这很重要,但也尝试在 [NSOperationQueue mainQueue] addOperationWithBlock{}]; 中坚持播放电话
  • 没关系.. 尝试断点并确保您的 URL 不为零。并检查 BuildPhases 下的项目设置,并确保您的 caf 文件列在 Copy Bundle Resources 下

标签: ios objective-c audio system-sounds


【解决方案1】:

您的 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 音效或使用系统声音服务调用振动”下的 Multimedia Programming Guide

另外,当你使用AudioServicesPlaySystemSound函数时:

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

因此,根据 Apple Docs,这似乎是不可能的。但是AVAudioPlayer 类允许您通过它的volume 属性来完成它。

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

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

【讨论】:

  • 谢谢路易斯。不知道更改volume属性会不会影响iPhone的实际音量。
  • 不确定 :( 试一试,尽管@EthanG 告诉我
【解决方案3】:

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 2012-07-17
    • 2012-09-07
    • 1970-01-01
    • 2021-02-04
    • 2019-11-03
    • 2022-12-24
    相关资源
    最近更新 更多