音效播放小操作

//  ViewController.m
//  音效播放

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

@interface ViewController ()

@end

@implementation ViewController

- (IBAction)palyAudioOne:(id)sender {
    [self playAudio:@"btnAudio.wav"];
}
- (IBAction)palyAudioTwo:(id)sender {
    [self playAudio:@"AJ3.WAV"];
}
-(void)playAudio:(NSString*)audioStr
{
    //1. 得到音頻的URL路徑
    NSString *audioPath = [[NSBundle mainBundle] pathForResource:audioStr ofType:nil];
    NSURL *audioUrl = [NSURL fileURLWithPath:audioPath];
    
    //2. 獲得系統聲音的ID
    SystemSoundID soundID = 0;
    
    //3. 把音頻文件加入到系統音頻服務中,並且返回長整型的聲音ID
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(audioUrl), &soundID);
    
    //4. 播放音效
    AudioServicesPlaySystemSound(soundID);
}

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

@end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章