【问题标题】:adding a sound when pressing an image button按下图像按钮时添加声音
【发布时间】:2013-02-17 15:32:15
【问题描述】:

我在滚动视图中有这个按钮,按下它时我需要播放声音。

UIButton *profileButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [profileButton setImage:[UIImage imageNamed:@"test.jpg"] forState:UIControlStateNormal];
    [profileButton setImage:[UIImage imageNamed:@"test.jpg"] forState:UIControlStateHighlighted];
    profileButton.frame = CGRectMake(0, 620, 320, 250);

[self.scrollView addSubview:profileButton];

我怎么能这样做? 谢谢,

【问题讨论】:

    标签: ios xcode image audio button


    【解决方案1】:

    在按钮按下时添加触摸

    [profileButton addTarget:self action:@selector(onButtonPress) forControlEvents:UIControlEventTouchUpInside];
    

    // 需要这个包含文件和 AudioToolbox 框架

    #import <AudioToolbox/AudioToolbox.h>
    

    播放一些声音

    -(void) onButtonPress {
        NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"mp3"];
        SystemSoundID soundID;
        AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
        AudioServicesPlaySystemSound (soundID);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      相关资源
      最近更新 更多