ios播放音乐

1、背景音乐播放    循环播放长音乐  支持mp3格式 

#import <AVFoundation/AVFoundation.h>;

 1 NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:@"changan" ofType:@"mp3"]; //创建音乐文件路径
 2 NSURL *musicURL = [[NSURL alloc] initFileURLWithPath:musicFilePath];
 3 
 4 AVAudioPlayer *thePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
 5 
 6 //创建播放器
 7 self.myBackMusic = thePlayer; //赋值给自己定义的类变量
 8 
 9 [musicURL release];
10 [thePlayer release];
11 
12 [myBackMusic prepareToPlay];
13 [myBackMusic setVolume:1]; //设置音量大小
14 myBackMusic.numberOfLoops = -1;//设置音乐播放次数 -1为一直循环
15 [myBackMusic play]; //播放

 

2、音效播放        短声音

#import <AudioToolbox/AudioToolbox.h>   

NSString *thesoundFilePath = [[NSBundle mainBundle] pathForResource:@"Clapping Crowd Studio 01" ofType:@"caf"]; //创建音乐文件路径
CFURLRef thesoundURL = (CFURLRef) [NSURL fileURLWithPath:thesoundFilePath];
AudioServicesCreateSystemSoundID(thesoundURL, &sameViewSoundID);

//变量SoundID与URL对应
AudioServicesPlaySystemSound(sameViewSoundID); //播放SoundID声音

 

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

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

2、数据必须是 PCM或者IMA4流格式

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

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

 

另详见: http://www.cnblogs.com/zhuqil/archive/2011/07/23/2115021.html

 

转载于:https://www.cnblogs.com/edgarli/p/3779285.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值