添加按钮声音  播放声音

AudioToolbox framework

    使用AudioToolbox framework。这个框架可以将比较短的声音注册到 system sound服务上。被注册到system sound服务上的声音称之为 system sounds。它必须满足下面几个条件。

1、 播放的时间不能超过30秒

2、数据必须是 PCM或者IMA4流格式, mp3也可以

3、必须被打包成下面三个格式之一:Core Audio Format (.caf), Waveform audio (.wav), 或者 Audio Interchange File (.aiff),mp3格式也可以

    声音文件必须放到设备的本地文件夹下面。通过AudioServicesCreateSystemSoundID方法注册这个声音文件,AudioServicesCreateSystemSoundID需要声音文件的url的CFURLRef对象。看下面注册代码:

 

- (void)playSoundWithName:(NSString *)name type:(NSString *)type

{

    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:type];

    

    if([[NSFileManager defaultManager] fileExistsAtPath:path]) {

        NSURL *url = [NSURL fileURLWithPath:path];

        SystemSoundID sound;

        AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &sound);

        AudioServicesPlaySystemSound(sound);

    }

    else {

        NSLog(@"**** Sound Error: file not found: %@", path);

    }

}


AVFoundation framework

   对于压缩过Audio文件,或者超过30秒的音频文件,可以使用AVAudioPlayer类。这个类定义在AVFoundation framework中。

    下面我们使用这个类播放一个mp3的音频文件。首先要引入AVFoundation framework,然后MediaPlayerViewController.h中添加下面代码:

#import <</span>AVFoundation/AVFoundation.h>
@interface MediaPlayerViewController : UIViewController <</span>AVAudioPlayerDelegate>
{
IBOutlet UIButton
*audioButton;
SystemSoundID shortSound;
AVAudioPlayer
*audioPlayer;

    AVAudioPlayer类也是需要知道音频文件的路径,使用下面代码创建一个AVAudioPlayer实例:

- (id)init
{
self
= [super initWithNibName:@"MediaPlayerViewController" bundle:nil];

if (self) {

NSString
*musicPath = [[NSBundle mainBundle] pathForResource:@"Music"
ofType:
@"mp3"];
if (musicPath) {
NSURL
*musicURL = [NSURL fileURLWithPath:musicPath];
audioPlayer
= [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL
error:nil];
[audioPlayer setDelegate:self];
}
NSString
*soundPath = [[NSBundle mainBundle] pathForResource:@"Sound12"
ofType:
@"aif"];

我们可以在一个button的点击事件中开始播放这个mp3文件,如:

- (IBAction)playAudioFile:(id)sender
{
if ([audioPlayer isPlaying]) {
// Stop playing audio and change text of button
[audioPlayer stop];
[sender setTitle:
@"Play Audio File"
forState:UIControlStateNormal];
}
else {
// Start playing audio and change text of button so
// user can tap to stop playback
[audioPlayer play];
[sender setTitle:
@"Stop Audio File"
forState:UIControlStateNormal];
}
}

这样运行我们的程序,就可以播放音乐了。

这个类对应的AVAudioPlayerDelegate有两个委托方法。一个是 audioPlayerDidFinishPlaying:successfully: 当音频播放完成之后触发。当播放完成之后,可以将播放按钮的文本重新回设置成:Play Audio File

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player
successfully:(BOOL)flag
{
[audioButton setTitle:
@"Play Audio File"
forState:UIControlStateNormal];
}

另一个是audioPlayerEndInterruption:,当程序被应用外部打断之后,重新回到应用程序的时候触发。在这里当回到此应用程序的时候,继续播放音乐。

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{
[audioPlayer play];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值