1.录音
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
path = [path stringByAppendingPathComponent:@"123.MP3"];
NSURL *url = [NSURL URLWithString:path];
//1.1创建录音对象
// settings 录音文件质量的
NSDictionary *setting = [NSDictionary dictionary];
AVAudioRecorder *recorder = [[AVAudioRecorder alloc]initWithURL:url settings:setting error:nil];
//2.开始
path = [path stringByAppendingPathComponent:@"123.MP3"];
NSURL *url = [NSURL URLWithString:path];
//1.1创建录音对象
// settings 录音文件质量的
NSDictionary *setting = [NSDictionary dictionary];
AVAudioRecorder *recorder = [[AVAudioRecorder alloc]initWithURL:url settings:setting error:nil];
//2.开始
[recorder record];
2.播放音效
//1.获取文件
NSString *path = @"/Users/apple/Desktop/123.MP3";
NSURL *url = [NSURL fileURLWithPath:path];
SystemSoundID soundID = 0;
//1.返回值 2.&
//桥接
AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID);
//2.播放
//没有震动效果
AudioServicesPlaySystemSound(soundID);
//有震动效果
NSString *path = @"/Users/apple/Desktop/123.MP3";
NSURL *url = [NSURL fileURLWithPath:path];
SystemSoundID soundID = 0;
//1.返回值 2.&
//桥接
AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID);
//2.播放
//没有震动效果
AudioServicesPlaySystemSound(soundID);
//有震动效果
// AudioServicesPlayAlertSound(soundID);
3.播放多个文件,工具类
#import <Foundation/Foundation.h>
@interface CZPlayMusicTool : NSObject
//播放音乐
+ (void)playAudioWithName:(NSString *)name;
//暂停音乐
+ (void)pauseAudioWithName:(NSString *)name;
//停止音乐
+ (void)stopAudioWithNmae:(NSString *)name;
@interface CZPlayMusicTool : NSObject
//播放音乐
+ (void)playAudioWithName:(NSString *)name;
//暂停音乐
+ (void)pauseAudioWithName:(NSString *)name;
//停止音乐
+ (void)stopAudioWithNmae:(NSString *)name;
@end
#import "CZPlayMusicTool.h"
#import <AVFoundation/AVFoundation.h>
static NSMutableDictionary *_players;
@implementation CZPlayMusicTool
+ (void)initialize{
_players = [NSMutableDictionary dictionary];
}
//播放音乐
+ (void)playAudioWithName:(NSString *)name{
AVAudioPlayer *player = _players[name];
if (player == nil) {
NSString *path = [[NSBundle mainBundle]pathForResource:name ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
[_players setObject:player forKey:name];
}
//开始播放
[player play];
}
//暂停音乐
+ (void)pauseAudioWithName:(NSString *)name{
AVAudioPlayer *player = _players[name];
if (player) {
[player pause];
}
}
//停止音乐
+ (void)stopAudioWithNmae:(NSString *)name{
AVAudioPlayer *player = _players[name];
if (player) {
[player stop];
}
#import <AVFoundation/AVFoundation.h>
static NSMutableDictionary *_players;
@implementation CZPlayMusicTool
+ (void)initialize{
_players = [NSMutableDictionary dictionary];
}
//播放音乐
+ (void)playAudioWithName:(NSString *)name{
AVAudioPlayer *player = _players[name];
if (player == nil) {
NSString *path = [[NSBundle mainBundle]pathForResource:name ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
[_players setObject:player forKey:name];
}
//开始播放
[player play];
}
//暂停音乐
+ (void)pauseAudioWithName:(NSString *)name{
AVAudioPlayer *player = _players[name];
if (player) {
[player pause];
}
}
//停止音乐
+ (void)stopAudioWithNmae:(NSString *)name{
AVAudioPlayer *player = _players[name];
if (player) {
[player stop];
}
}
- (IBAction)play:(id)sender {
[CZPlayMusicTool playAudioWithName:@"咖啡.mp3"];
}
- (IBAction)stop:(id)sender {
[CZPlayMusicTool playAudioWithName:@"煎饼侠.mp3"];
}
- (IBAction)anting:(id)sender {
[CZPlayMusicTool pauseAudioWithName:@"煎饼侠.mp3"];
}
- (IBAction)djkfs:(id)sender {
[CZPlayMusicTool stopAudioWithNmae:@"咖啡.mp3"];
}
- (IBAction)stop2:(id)sender {
[CZPlayMusicTool stopAudioWithNmae:@"煎饼侠.mp3"];
[CZPlayMusicTool playAudioWithName:@"咖啡.mp3"];
}
- (IBAction)stop:(id)sender {
[CZPlayMusicTool playAudioWithName:@"煎饼侠.mp3"];
}
- (IBAction)anting:(id)sender {
[CZPlayMusicTool pauseAudioWithName:@"煎饼侠.mp3"];
}
- (IBAction)djkfs:(id)sender {
[CZPlayMusicTool stopAudioWithNmae:@"咖啡.mp3"];
}
- (IBAction)stop2:(id)sender {
[CZPlayMusicTool stopAudioWithNmae:@"煎饼侠.mp3"];
}