ios播放音乐

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

#import <AVFoundation/AVFoundation.h>;app

 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]; //播放

 

二、音效播放        短声音框架

#import <AudioToolbox/AudioToolbox.h>   oop

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。它必须知足下面几个条件。url

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

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

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

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

 

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

相关文章
相关标签/搜索