• IOS-音乐


    一、音乐的播放

    音乐播放用到一个叫做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
  • 相关阅读:
    一个FLAG #03# 蛇形填数
    一个FLAG #02# 逆序输出
    一个FLAG #01# 重学C/C++以及算法
    MAVLink笔记 #01# 维基百科翻(译)
    编译原理 #04# 中缀表达式转化为四元式(JavaScript实现)
    Java开发:手机电话号码校验
    解决java poi循环遍历行getLastRowNum出现不准确的问题
    Redis的安装和简单测试
    JS解析xml字符串,并把xml展示在HTML页面上
    解决cxf+springmvc发布的webservice,缺少types,portType和message标签的问题
  • 原文地址:https://www.cnblogs.com/oc-bowen/p/5303682.html
Copyright © 2020-2023  润新知