【问题标题】:AudioServicesPlaySystemSound sound not audible when called from IBAction method从 IBAction 方法调用 AudioServicesPlaySystemSound 声音时听不到声音
【发布时间】:2013-01-31 13:45:07
【问题描述】:

我的 iOS 应用中的 AudioServicesPlaySystemSound 出现问题。

我定义了一个播放系统声音的方法。当我从 viewDidLoad 调用该方法时,我可以听到声音播放,但是当我从按钮处理程序调用它时,我听不到声音播放。

这是来自我的视图控制器的代码:

SystemSoundID startSound = 0;

-(void)playStartSound
{        
    AudioServicesPlaySystemSound(startSound);
}

-(void)viewDidLoad 
{           
    NSURL *urlStart = [[NSBundle mainBundle] URLForResource:@"Beep" withExtension:@"wav"];
    AudioServicesCreateSystemSoundID((CFURLRef)urlStart, &startSound);

    [self playStartSound];    

    [super viewDidLoad];
}

- (IBAction)start:(id)sender
{
    [self playStartSound];
}

IBAction 方法与按钮相关联。当我选择按钮时,会调用 playStartSound 方法,但听不到声音。

在调用 dealloc 方法之前,系统声音不会使用 AudioServicesDisposeSystemSoundID 进行处理。

我尝试从 playStartSound 中调用 AudioServicesCreateSystemSoundID,但没有任何区别。

该应用还使用 AV Foundation、Media Player、Core Media、Core Video 和 Core Location。我想知道其中之一是否会干扰 AudioToolbox?

更新:我只是把一个简单的应用程序放在一起,它只包含上面的代码和一个按钮,当我选择按钮时声音播放得很好。

然后我从控制器中删除了所有视频捕获代码,现在音频可以正常播放了。

【问题讨论】:

  • 这实际上是您声明 SystemSoundID 变量的方式吗?
  • 我最初将它作为班级成员,但将其移至全局以查看是否有任何不同。它没有。
  • 这是因为录音没有执行...

标签: iphone ios objective-c audiotoolbox


【解决方案1】:

这不起作用,因为我的应用程序中有一个活动的视频捕获会话。

AudioServicesPlaySystemSound 不会在有活动的视频捕获会话时播放。

【讨论】:

  • 录制视频时有什么方法可以播放声音吗?
【解决方案2】:
   NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"7777" ofType:@"mp3"];
   SystemSoundID soundID;
   AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath: soundPath]), &soundID);
   AudioServicesPlaySystemSound (soundID);

嘿试试这样它会播放

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多