AVAudioPlayer(播放本地音频)
若要后台播放
//-------------------1----------------------
//修改info.plist文件,
//添加一个字段 Required background modes
//向这个字段的数组中添加一个元素 App plays audio or streams audio/video using AirPlay
//-------------------2----------------------
//设置音频会话的类型为后台播放
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
@interface AVAudioPlayer : NSObject
//音量(0-1)
@property float volume; /* The volume for the sound. The nominal range is from 0.0 to 1.0. */
//目前播放的时间,可通过改变播放点
@property NSTimeInterval currentTime;
//总时长
@property(readonly) NSTimeInterval duration; /* the duration of the sound. */
//
@property(readonly) NSTimeInterval deviceCurrentTime;
//初始化方法
- (nullable instancetype)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError;
//准备播放
- (BOOL)prepareToPlay;
//播放
- (BOOL)play;
//暂停
- (void)pause;
//停止
- (void)stop;
AVPlayer(可通过网络地址播放音乐)
- (void)play;
- (void)pause;
系统声音队列
(函数,直接调用)通过地址注册系统声音
//inFileURL:声音地址
//outSystemSoundID:SystemSoundID对象
AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)
(函数,直接调用)播放注册的系统声音
AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)
播放振动
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
移除系统声音
//当不需要再使用系统声音时,需要移除
- (void)dealloc {
AudioServicesRemoveSystemSoundCompletion(soundID);
soundID = 0;
}