最近看了不少关于AVFoundation的demo,下面总结一下音视频播放
- <h2>录音功能
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
/** 录音对象 */
@property (nonatomic, strong) AVAudioRecorder *recorder;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)start {
// 开始录音
[self.recorder record];
}
- (IBAction)stop {
[self.recorder stop];
}
#pragma mark - 懒加载代码
- (AVAudioRecorder *)recorder
{
if (_recorder == nil) {
// 1.创建沙盒路径
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 2.拼接音频文件
NSString *filePath = [path stringByAppendingPathComponent:@"123.caf"];
// 3.转化成url file://
NSURL *url = [NSURL fileURLWithPath:filePath];
// 4.设置录音的参数
NSDictionary *settingRecorder = @{
AVEncoderAudioQualityKey : [NSNumber numberWithInteger:AVAudioQualityLow],
AVEncoderBitRateKey : [NSNumber numberWithInteger:16],
AVSampleRateKey : [NSNumber numberWithFloat:8000],
AVNumberOfChannelsKey : [NSNumber numberWithInteger:2]
};
// 5.创建录音对象
self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settingRecorder error:nil];
}
return _recorder;
}
- <h2>音频播放
<h6>这里多用于音药的播放,提供音药名就能播放
<h6>整了三个方法,分别是开始、暂停、停止
- (IBAction)start {
[ZJAudioTool playMusicWithFileName:@"1201111234.mp3"];
}
- (IBAction)pause {
[ZJGAudioTool pauseMusicWithFileName:@"1201111234.mp3"];
}
- (IBAction)stop {
[ZJAudioTool stopMusicWithFileName:@"1201111234.mp3"];
}
<h6>这里开放几个方法,分别是开始、暂停、停止、和播放音效
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
@interface ZJAudioTool : NSObject
// 播放音乐 fileName:音乐文件
+ (AVAudioPlayer *)playMusicWithFileName:(NSString *)fileName;
// 暂停音乐 fileName:音乐文件
+ (void)pauseMusicWithFileName:(NSString *)fileName;
// 停止音乐 fileName:音乐文件
+ (void)stopMusicWithFileName:(NSString *)fileName;
// 播放音效 soundName:音效文件
+ (void)playSoundWithSoundName:(NSString *)soundName;
@end
#import "ZJAudioTool.h"
@implementationZJAudioTool
static NSMutableDictionary *_soudIDs;
static NSMutableDictionary *_players;
+ (void)initialize
{
_soudIDs = [NSMutableDictionary dictionary];
_players = [NSMutableDictionary dictionary];
}
+ (AVAudioPlayer *)playMusicWithFileName:(NSString *)fileName
{
// 1.创建空的播放器
AVAudioPlayer *player = nil;
// 2.从字典中取出播放器
player = _players[fileName];
// 3.判断播放器是否为空
if (player == nil) {
// 4.生成对应音乐资源
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:fileName withExtension:nil];
if (fileUrl == nil) return nil;
// 5.创建对应的播放器
player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil];
// 6.保存到字典中
[_players setObject:player forKey:fileName];
// 7.准备播放
[player prepareToPlay];
}
// 8.开始播放
[player play];
return player;
}
+ (void)pauseMusicWithFileName:(NSString *)fileName
{
// 1.从字典中取出播放器
AVAudioPlayer *player = _players[fileName];
// 2.暂停音乐
if (player) {
[player pause];
}
}
+ (void)stopMusicWithFileName:(NSString *)fileName
{
// 1.从字典中取出播放器
AVAudioPlayer *player = _players[fileName];
// 2.停止音乐
if (player) {
[player stop];
[_players removeObjectForKey:fileName];
player = nil;
}
}
+ (void)playSoundWithSoundName:(NSString *)soundName
{
// 1.创建soundID = 0
SystemSoundID soundID = 0;
// 2.从字典中取出soundID
soundID = [_soudIDs[soundName] unsignedIntValue];;
// 3.判断soundID是否为0
if (soundID == 0) {
// 3.1生成soundID
CFURLRef url = (__bridge CFURLRef)[[NSBundle mainBundle] URLForResource:soundName withExtension:nil];
if (url == nil) return;
AudioServicesCreateSystemSoundID(url, &soundID);
// 3.2将soundID保存到字典中
[_soudIDs setObject:@(soundID) forKey:soundName];
}
// 4.播放音效
AudioServicesPlaySystemSound(soundID);
}
@end
- <h2>视频播放
#import "ViewController.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
/** 播放器 */
@property (nonatomic, strong) AVPlayerViewController *playerVC;
@end
@implementation ViewController
- (IBAction)play {
[self presentViewController:self.playerVC animated:YES completion:nil];
}
- (AVPlayerViewController *)playerVC
{
if (_playerVC == nil) {
NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/19954d8f-e2c2-4c0a-b8c1-a4c826b5ca8b/L.mp4"];
AVPlayerItem *playItem = [AVPlayerItem playerItemWithURL:url];
AVPlayer *player = [AVPlayer playerWithPlayerItem:playItem];
_playerVC = [[AVPlayerViewController alloc] init];
_playerVC.player = player;
}
return _playerVC;
}
@end
网友评论