2

我正在尝试在我的 iphone 应用程序中播放一个简短的警报声,我正在使用我找到的这段代码,但它不会播放任何声音。

NSString *toneFilename = [[NSBundle mainBundle] pathForResource:@"alarm" ofType:@"wav"];
    NSURL *toneURLRef = [NSURL fileURLWithPath:toneFilename];
    SystemSoundID Sound;

    AudioServicesCreateSystemSoundID(
                                     (__bridge CFURLRef) toneURLRef,
                                     &camerSound
                                     );

    AudioServicesPlaySystemSound(Sound);

当我尝试使其振动时,它可以工作:

AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

谢谢

4

3 回答 3

3

使用 AVFoundationFramework

添加AVFoundation.framework到项目。

#import <AVFoundation/AVFoundation.h>

NSString *toneFilename = [[NSBundle mainBundle] pathForResource:@"alarm" ofType:@"wav"];
NSURL *toneURLRef = [NSURL fileURLWithPath:toneFilename];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL: toneURLRef error: nil];
player.currentTime = 0;
player.volume = 1.0f;
[player play];
于 2012-12-09T15:16:05.743 回答
1

Sound创建声音时,您没有正确处理变量。相反,您正在使用camerSound. AudioServicesPlayAlertSound使用正确的参数调用应该可以。

于 2012-12-09T15:39:54.423 回答
1

这对我来说很完美。

将 AVFoundation 和 AudioToolBox 框架添加到项目中

#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

@interface TestViewController : UIViewController
{
    SystemSoundID SoundID;
}

@implementation TestViewController
-(void) viewDidLoad {
    [super viewDidLoad];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"caf"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &SoundID);  //When using ARC, make sure to use __bridge
 }

-(void) playSound {
     AudioServicesPlaySystemSound(SoundID);
 }
于 2014-06-20T06:26:20.103 回答