关闭

音频播放初步认识

309人阅读 评论(0) 收藏 举报
分类:
//
//  ViewController.m


#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()
@property(nonatomic,assign) SystemSoundID soundID;
@end

@implementation ViewController

- (SystemSoundID)soundID
{
    if (!_soundID) {
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &_soundID);
    }
    return _soundID;
}

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //1.加载音效文件(短音频)
    //1个音效文件 对应一个 soundID
//    SystemSoundID soundID;
//    NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];
//    AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID);
    
    //2.拿到音效ID播放
    AudioServicesPlaySystemSound(self.soundID);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
    //销毁音频ID
    AudioServicesDisposeSystemSoundID(self.soundID);
    self.soundID = 0;
}

@end

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1285944次
    • 积分:23519
    • 等级:
    • 排名:第303名
    • 原创:1234篇
    • 转载:156篇
    • 译文:0篇
    • 评论:123条
    最新评论