音频播放初步认识

//
//  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

阅读更多
个人分类: iOS
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭