實作一個音效播放的例子,此處主要是使用SystemSound
Xcode版本4.5.1
參考http://furnacedigital.blogspot.tw/2010/12/core-audio.html
及http://bearuwork.blogspot.tw/2012/10/audio.html
音效檔下載參考區
http://www.mcjh.kl.edu.tw/usr/wdh/techedu/wavsound/index.html
http://twfilmgroup.lolforum.net/t505-topic
http://billor.chsh.chc.edu.tw/sound/p.htm
http://www.khjh.kh.edu.tw/mewawa/flash/music.htm
http://www.mpbus.com/Ringtones/downloads-7-1.htm
http://www.flashkit.com/soundfx/
http://www.flashkit.com/loops/
System Sound 是在 iOS Core Audio 中用來播放音效的一種方法, 它支援 wav、caf 和 aif 等音效格式,System Sound 主要被用來播放一些簡短的音並,所以它不支援音量的控制,也沒有 Pause 和 Playback 等功能。
1. 開啓一個專案,並新增一個Button到UIView上
2. 新增一個AudioToolbox的Framework
3. 加入音效檔
4. 在audioViewController.h加入Framework的定義檔
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface audioViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *playButton;
@end
5. 在audioViewController.m加入Button的IBAction及音效播放程式碼
#import "audioViewController.h"
@interface audioViewController ()
@end
@implementation audioViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)playSound:(id)sender {
NSString *soundfilename;
SystemSoundID soundID;
soundfilename = @"NOTIFY.wav";
NSString *Path=[[NSBundle mainBundle] bundlePath];
//NSURL *tapSound=[NSURL fileURLWithPath:[Path stringByAppendingPathComponent:soundfilename]];
NSURL *sound_url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"NOTIFY" ofType:@"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)sound_url, &soundID);
//播放
AudioServicesPlaySystemSound(soundID);
}
@end



沒有留言:
張貼留言