ios - 如何使用 (IBAction)playsound :(id)sender 播放声音

标签 ios objective-c xcode macos audio

<分区>


要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预期结果。另请参阅:Stack Overflow question checklist

关闭 9 年前

我想简单地用它的按钮放置一个声音。由于缺乏理解,我已经到了一个停止点。我尝试了大约 4 个小时试图解决此问题,但无济于事。有人能帮我吗?我收到错误消息,“使用未声明的标识符‘soundFile’”这是我的 .m 文件..

//
//  FirstViewController.m
//  simpleApp
//
//  Created by Abby Russell on 11/29/13.
//  Copyright (c) 2013 Abby Russell. All rights reserved.
//

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)playsound:(id)sender {
    SystemSoundID soundID;
    NSString *buttonName=[sender currentTitle];
    NSString *path = [[NSBundle mainBundle] pathForResource:buttonName ofType:@"wav"];

    AudioServicesCreateSystemSoundID((__bridge CFURLRef)
                                     [NSURL fileURLWithPath: soundFile], &
                                     soundID;
    AudioServicesPlaySystemSound(soundID);


}
@end

非常感谢你们这些聪明人!

最佳答案

soundFile 需要是声音文件的 NSString 路径。看起来您只是输入了文本“soundFile”,实际上没有任何名为 soundFile 的 NSString。尝试用“路径”替换 soundFile。

关于ios - 如何使用 (IBAction)playsound :(id)sender 播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20307844/

上一篇:ios - NSXMLParser 不适用于某些标签

下一篇:ios - 绘制一系列UIImages和内存问题

相关文章:

ios - Xcode Interface Builder - View 在垂直方向上不明确

ios - Swift - 如何从 Firebase 检索多个值并将它们显示在 UITABLEVIEW 上

iphone - 以表单形式呈现时难以将 View Controller 居中

iOS 重定向到设置主页

设备崩溃时的 XCODE 7 应用程序

ios - 如何在iOS中触摸屏时隐藏键盘

ios - dyld : Library not loaded: @rpath/libswiftCore. 动态库。问题依然存在

ios - 反转 CALayer 蒙版

ios - didUpdateToLocation 未在应用程序中调用

ios - iPhone开发: How to transfer information from one . m文件到另一个