iphone - 代码以使用iphone上的按钮以编程方式播放声音

标签 iphone objective-c audio

我试图弄清楚如何在不使用IB的情况下连接按钮以编程方式播放声音。我有播放声音的代码,但是没有办法挂上按钮来播放声音?有什么帮助吗?

这是我正在使用的代码:

     - (void)playSound
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"boing_1" ofType:@"wav"];
        AVAudioPlayer* myAudio = [[AVAudioPlayer alloc] 
                 initWithContentsOfURL:[NSURL fileURLWithPath:path error:NULL]];
        myAudio.delegate = self;
        myAudio.volume = 2.0;
        myAudio.numberOfLoops = 1;
        [myAudio play];
    }

最佳答案

[button addTarget:self action:@selector(playSound) forControlEvents:UIControlEventTouchUpInside];

UIButton从UIControl继承其target / action方法。

关于iphone - 代码以使用iphone上的按钮以编程方式播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2626277/

相关文章:

iphone - UIImage 到 base64 不工作

iphone - 用于构建通用应用程序的最新资源/教程?

iphone - localizedNameOfStringEncoding 不起作用

ios - Pod 未在 Pods 文件中列出,但它安装在 pod install 命令上

iphone - 如何使带有触摸的 UIImageView 转到其他 ViewController?

iphone - NSManagedObject:isUpdated 和 isInserted

objective-c - 在 AVAssetReader 中设置时间范围会导致卡住

audio - FFMPEG 在 amix 中设置音量

c++ - 如何让这个音频延迟工作?

python - 使用 python 和 Qt 创建一个准确的节拍器