IOS-音乐
2016-03-21 21:01
706 查看
一、音乐的播放
音乐播放用到一个叫做AVAudioPlayer的类, 能够用于播放本地音频文件
AVAudioPlayer常用方法
加载音乐文件
- (id)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError;
- (id)initWithData:(NSData *)data error:(NSError **)outError;
准备播放(缓冲,提高播放的流畅性)
- (BOOL)prepareToPlay;
播放(异步播放)
- (BOOL)play;
暂停
- (void)pause;
停止
- (void)stop;
是否正在播放
@property(readonly, getter=isPlaying) BOOL playing;
时长
@property(readonly) NSTimeInterval duration;
当前的播放位置
@property NSTimeInterval currentTime;
播放次数(-1代表无限循环播放,其他代表播放numberOfLoops+1次)
@property NSInteger numberOfLoops;
音量
@property float volume;
是否允许更改速率
@property BOOL enableRate;
播放速率(1是正常速率,0.5是一般速率,2是双倍速率)
@property float rate;
有多少个声道
@property(readonly) NSUInteger numberOfChannels;
音乐播放用到一个叫做AVAudioPlayer的类, 能够用于播放本地音频文件
AVAudioPlayer常用方法
加载音乐文件
- (id)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError;
- (id)initWithData:(NSData *)data error:(NSError **)outError;
准备播放(缓冲,提高播放的流畅性)
- (BOOL)prepareToPlay;
播放(异步播放)
- (BOOL)play;
暂停
- (void)pause;
停止
- (void)stop;
是否正在播放
@property(readonly, getter=isPlaying) BOOL playing;
时长
@property(readonly) NSTimeInterval duration;
当前的播放位置
@property NSTimeInterval currentTime;
播放次数(-1代表无限循环播放,其他代表播放numberOfLoops+1次)
@property NSInteger numberOfLoops;
音量
@property float volume;
是否允许更改速率
@property BOOL enableRate;
播放速率(1是正常速率,0.5是一般速率,2是双倍速率)
@property float rate;
有多少个声道
@property(readonly) NSUInteger numberOfChannels;
// // AVAudioPlayerTool.h // IOS_0318_AVAudioPlayer // // Created by ma c on 16/3/18. // Copyright © 2016年 博文科技. All rights reserved. // 播放本地音乐/音效 #import <Foundation/Foundation.h> @interface AVAudioPlayerTool : NSObject + (BOOL)playerMusic:(NSString *)fileName; + (void)pauseMusic:(NSString *)fileName; + (void)stopMusic:(NSString *)fileName; @end @interface CustomSoundsID : NSObject ///播放音效 + (void)playerSound:(NSString *)fileName; ///销魂音效 + (void)disposeSound:(NSString *)fileName; @end
// // AVAudioPlayerTool.m // IOS_0318_AVAudioPlayer // // Created by ma c on 16/3/18. // Copyright © 2016年 博文科技. All rights reserved. // #import "AVAudioPlayerTool.h" #import <AVFoundation/AVFoundation.h> @implementation AVAudioPlayerTool ///存放所有音乐文件 static NSMutableDictionary *_musicPlayers; + (NSMutableDictionary *)musicPlayers { if(!_musicPlayers){ _musicPlayers = [NSMutableDictionary dictionary]; } return _musicPlayers; } ///播放音乐 + (BOOL)playerMusic:(NSString *)fileName { //判断fileName是否为空 if(!fileName) return NO; //1.取出对应的播放器 AVAudioPlayer *player = [self musicPlayers][fileName]; //2.如果播放器不存在,就初始化 if (!player) { NSURL *url = [[NSBundle mainBundle] URLForResource:fileName withExtension:nil]; //判断url是否存在 if (!url) return NO; //创建播放器(一个AVAudioPlayer只能播放一个URL) player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; //缓冲 if (![player prepareToPlay]) return NO; //存入字典 [self musicPlayers][fileName] = player; } //3.播放 if (!player.isPlaying) { [player play]; } return YES; } ///暂停音乐 + (void)pauseMusic:(NSString *)fileName { //判断fileName是否为空 if(!fileName) return; //1.取出对应的播放器 AVAudioPlayer *player = [self musicPlayers][fileName]; //2.暂停 if (player.isPlaying) { [player pause]; } } ///停止音乐 + (void)stopMusic:(NSString *)fileName { //判断fileName是否为空 if(!fileName) return; //1.取出对应的播放器 AVAudioPlayer *player = [self musicPlayers][fileName]; //2.暂停 [player stop]; //3.将播放器从字典中移除 [[self musicPlayers] removeObjectForKey:fileName]; } @end @implementation CustomSoundsID ///存放所有音效 static NSMutableDictionary *_soundIDs; + (NSMutableDictionary *)soundIDs { if (!_soundIDs) { _soundIDs = [NSMutableDictionary dictionary]; } return _soundIDs; } ///播放音效 + (void)playerSound:(NSString *)fileName { if (!fileName) return; //1.取出对应的音效ID SystemSoundID soundID = [[self soundIDs][fileName] unsignedIntValue]; //2.初始化 if (!soundID) { NSURL *url = [[NSBundle mainBundle] URLForResource:fileName withExtension:nil]; if (!url) return; AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID); //存入字典 [self soundIDs][fileName] = @(soundID); } //3.播放 AudioServicesPlayAlertSound(soundID); } ///销魂音效 + (void)disposeSound:(NSString *)fileName { if (!fileName) return; //1.取出对应的音效ID SystemSoundID soundID = [[self soundIDs][fileName] unsignedIntValue]; //2.销毁 if (soundID) { AudioServicesDisposeSystemSoundID(soundID); [[self soundIDs] removeObjectForKey:fileName]; } } @end
相关文章推荐
- IOS-音效
- iOS进程间通信之CFMessagePort/还有 url schema
- IOS-第三方(SDWebImage)
- ios三方插件
- ios管理插件
- IOS-Alcatraz(插件管理工具)
- IOS-数据缓存
- iOS 开发之 Xcode断点调试以及动态输出
- IOS学习之iOS开发中用户密码应该保存在哪里
- iOS--归档和解档(Archiver)、(UnArchiver)
- iOS开发-音视频学习
- iOS 数据管理
- ios判断某个坐标是否在某块区域内方法
- ios自定义View:init和initWithFrame方法
- iOS开发中的细节键盘的影藏的几种方式
- iOS刷新tableView某个cell或section
- 61.iOS 保持界面流畅的技巧
- 60.iOS应用性能调优的25个建议和技巧
- iOS CoreData数据迁移
- iOS动态部署方案