码迷,mamicode.com
首页 > 移动开发 > 详细

IOS-音乐

时间:2016-03-21 21:33:03      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

一、音乐的播放

音乐播放用到一个叫做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;
 
 
 1 //
 2 //  AVAudioPlayerTool.h
 3 //  IOS_0318_AVAudioPlayer
 4 //
 5 //  Created by ma c on 16/3/18.
 6 //  Copyright © 2016年 博文科技. All rights reserved.
 7 //  播放本地音乐/音效
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @interface AVAudioPlayerTool : NSObject
12 
13 + (BOOL)playerMusic:(NSString *)fileName;
14 + (void)pauseMusic:(NSString *)fileName;
15 + (void)stopMusic:(NSString *)fileName;
16 
17 @end
18 
19 @interface CustomSoundsID : NSObject
20 
21 ///播放音效
22 + (void)playerSound:(NSString *)fileName;
23 ///销魂音效
24 + (void)disposeSound:(NSString *)fileName;
25 
26 
27 @end
  1 //
  2 //  AVAudioPlayerTool.m
  3 //  IOS_0318_AVAudioPlayer
  4 //
  5 //  Created by ma c on 16/3/18.
  6 //  Copyright © 2016年 博文科技. All rights reserved.
  7 //
  8 
  9 #import "AVAudioPlayerTool.h"
 10 #import <AVFoundation/AVFoundation.h>
 11 
 12 @implementation AVAudioPlayerTool
 13 ///存放所有音乐文件
 14 static NSMutableDictionary *_musicPlayers;
 15 
 16 + (NSMutableDictionary *)musicPlayers
 17 {
 18     if(!_musicPlayers){
 19         _musicPlayers = [NSMutableDictionary dictionary];
 20         
 21     }
 22     return _musicPlayers;
 23 }
 24 ///播放音乐
 25 + (BOOL)playerMusic:(NSString *)fileName
 26 {
 27     //判断fileName是否为空
 28     if(!fileName) return NO;
 29     
 30     //1.取出对应的播放器
 31     AVAudioPlayer *player = [self musicPlayers][fileName];
 32     
 33     //2.如果播放器不存在,就初始化
 34     if (!player) {
 35         
 36         NSURL *url = [[NSBundle mainBundle] URLForResource:fileName withExtension:nil];
 37         //判断url是否存在
 38         if (!url)  return NO;
 39         //创建播放器(一个AVAudioPlayer只能播放一个URL)
 40         player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
 41         
 42         //缓冲
 43         if (![player prepareToPlay]) return NO;
 44         
 45         //存入字典
 46         [self musicPlayers][fileName] = player;
 47 
 48     }
 49     
 50     //3.播放
 51     if (!player.isPlaying) {
 52         [player play];
 53     }
 54     return YES;
 55 }
 56 ///暂停音乐
 57 + (void)pauseMusic:(NSString *)fileName
 58 {
 59     //判断fileName是否为空
 60     if(!fileName) return;
 61     
 62     //1.取出对应的播放器
 63     AVAudioPlayer *player = [self musicPlayers][fileName];
 64     
 65     //2.暂停
 66     if (player.isPlaying) {
 67         [player pause];
 68     }
 69     
 70 }
 71 ///停止音乐
 72 + (void)stopMusic:(NSString *)fileName
 73 {
 74     //判断fileName是否为空
 75     if(!fileName) return;
 76     
 77     //1.取出对应的播放器
 78     AVAudioPlayer *player = [self musicPlayers][fileName];
 79     
 80     //2.暂停
 81     [player stop];
 82     
 83     
 84     //3.将播放器从字典中移除
 85     [[self musicPlayers] removeObjectForKey:fileName];
 86 }
 87 
 88 
 89 @end
 90 
 91 
 92 @implementation CustomSoundsID
 93 
 94 ///存放所有音效
 95 static NSMutableDictionary *_soundIDs;
 96 
 97 + (NSMutableDictionary *)soundIDs
 98 {
 99     if (!_soundIDs) {
100         _soundIDs = [NSMutableDictionary dictionary];
101     }
102     return _soundIDs;
103 }
104 
105 
106 ///播放音效
107 + (void)playerSound:(NSString *)fileName
108 {
109     if (!fileName)  return;
110     
111     //1.取出对应的音效ID
112     SystemSoundID soundID = [[self soundIDs][fileName] unsignedIntValue];
113     
114     //2.初始化
115     if (!soundID) {
116         NSURL *url = [[NSBundle mainBundle] URLForResource:fileName withExtension:nil];
117         
118         if (!url)  return;
119         AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID);
120         
121         //存入字典
122         [self soundIDs][fileName] = @(soundID);
123     }
124     //3.播放
125     AudioServicesPlayAlertSound(soundID);
126 }
127 ///销魂音效
128 + (void)disposeSound:(NSString *)fileName
129 {
130     if (!fileName)  return;
131     
132     //1.取出对应的音效ID
133     SystemSoundID soundID = [[self soundIDs][fileName] unsignedIntValue];
134     
135     //2.销毁
136     if (soundID) {
137         AudioServicesDisposeSystemSoundID(soundID);
138         [[self soundIDs] removeObjectForKey:fileName];
139     }
140 }
141 
142 
143 
144 @end

 

IOS-音乐

标签:

原文地址:http://www.cnblogs.com/oc-bowen/p/5303682.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!