您的位置:首页 > 移动开发 > IOS开发

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;

//
//  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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: