IOS 多媒体 使用总结
一、音频播放
1.音效播放(短时间的音频文件)
1> AudioServicesCreateSystemSoundID
2> AudioServicesPlaySystemSound
2.音乐播放(长时间的音频文件)
1> AVAudioPlayer
只能播放本地的音频文件
2> AVPlayer
能播放本地、远程的音频、视频文件
基于Layer显示,得自己去编写控制面板
3> MPMoviePlayerController
能播放本地、远程的音频、视频文件
自带播放控制面板(暂停、播放、播放进度、是否要全屏)
4> MPMoviewPlayerViewController
能播放本地、远程的音频、视频文件
内部是封装了MPMoviePlayerController
播放界面默认就是全屏的
如果播放功能比较简单,仅仅是简单地播放远程、本地的视频文件,建议用这个
实例:AVPlayer MPMoviePlayerController, MPMoviewPlayerViewController 本地视频播放
5> DOUAudioStreamer
能播放远程、本地的音频文件
监听缓冲进度、下载速度、下载进度
#import "HMViewController.h"
#import "HMAudioFile.h" #import "UIView+Extension.h" #define MJStatusProp @"status"
#define MJBufferingRatioProp @"bufferingRatio" #import "DOUAudioStreamer.h" @interface HMViewController ()
/** 播放器 */
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
@property (nonatomic, strong) DOUAudioStreamer *streamer;
@property (weak, nonatomic) IBOutlet UIView *positionProgressView;
@property (weak, nonatomic) IBOutlet UIView *downloadProgressView;
@property (weak, nonatomic) IBOutlet UIView *progressBg;
@property (strong, nonatomic) NSTimer *currentTimeTimer; @end @implementation HMViewController #pragma mark - 定时器
- (void)addTimer
{
self.currentTimeTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCurrentTime) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.currentTimeTimer forMode:NSRunLoopCommonModes];
} - (void)removeTimer
{
[self.currentTimeTimer invalidate];
self.currentTimeTimer = nil;
} - (void)viewDidLoad
{
[super viewDidLoad]; // 创建音频文件模型(提供音频文件路径)
HMAudioFile *file = [[HMAudioFile alloc] init];
file.audioFileURL = [NSURL URLWithString:@"http://y1.eoews.com/assets/ringtones/2012/5/18/34045/hi4dwfmrxm2citwjcc5841z3tiqaeeoczhbtfoex.mp3"]; // 创建播放器
self.streamer = [DOUAudioStreamer streamerWithAudioFile:file]; // KVO监听streamer的属性(Key value Observing)
[self.streamer addObserver:self forKeyPath:MJStatusProp options:NSKeyValueObservingOptionOld context:nil];
[self.streamer addObserver:self forKeyPath:MJBufferingRatioProp options:NSKeyValueObservingOptionOld context:nil]; // 播放
[self.streamer play]; [self addTimer]; UIWebView *webView;
webView.scalesPageToFit = YES;
} - (void)dealloc
{
[self.streamer removeObserver:self forKeyPath:MJStatusProp];
[self.streamer removeObserver:self forKeyPath:MJBufferingRatioProp];
} /**
利用KVO监听的属性值改变了,就会调用
*/
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
dispatch_async(dispatch_get_main_queue(), ^{ if ([keyPath isEqualToString:MJStatusProp]) { // 监听到播放器状态改变了
// NSLog(@"状态改变了----%d", self.streamer.status);
} else if ([keyPath isEqualToString:MJBufferingRatioProp]) { // 监听到缓冲比例改变
double unit = 1000.0; // 总长度
double expectedLength = self.streamer.expectedLength / unit / unit;
// 已经下载长度
double receivedLength = self.streamer.receivedLength / unit / unit;
// 下载速度
double downloadSpeed = self.streamer.downloadSpeed / unit; self.infoLabel.text = [NSString stringWithFormat:@"缓冲:%.2fMB/%.2fMB(%.0f%%)\n下载速度:%.2fKB/s", receivedLength, expectedLength, (receivedLength/ expectedLength) * , downloadSpeed]; self.downloadProgressView.width = self.progressBg.width * (receivedLength/ expectedLength);
}
});
} - (void)updateCurrentTime
{
self.positionProgressView.width = self.progressBg.width * (self.streamer.currentTime / self.streamer.duration);
} // AVAudioPlayer : 只能播放本地的音频文件
// AVPlayer : 能播放远程\本地的音频、视频文件
// MPMoviePlayerController : 能播放远程\本地的音频、视频文件
// MPMoviePlayerViewController : 能播放远程\本地的音频、视频文件
// self.player = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://file.qianqian.com/data2/music/42275287/42275287.mp3?xcode=e41a36a1198cf7f07c498ac14eafd4a5398370073e4f3405"]]; @end
二、视频播放
1.音乐播放中2> 3> 4>
2.VLC
#import "HMViewController.h"
#import <MobileVLCKit/MobileVLCKit.h> @interface HMViewController ()
@property (nonatomic, strong) VLCMediaPlayer *player;
@end @implementation HMViewController - (void)viewDidLoad
{
[super viewDidLoad]; self.player = [[VLCMediaPlayer alloc] init];
// 设置需要播放的多媒体文件
// NSURL *url = [NSURL URLWithString:@"http://streams.videolan.org/streams/mp4/Mr_MrsSmith-h264_aac.mp4"];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"minion_01.mp4" withExtension:nil];
self.player.media = [VLCMedia mediaWithURL:url];
// 设置播放界面的载体
self.player.drawable = self.view;
// 播放
[self.player play];
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
} // NSURL *url = [NSURL URLWithString:@"http://y1.eoews.com/assets/ringtones/2012/5/18/34045/hi4dwfmrxm2citwjcc5841z3tiqaeeoczhbtfoex.mp3"];
// NSURL *url = [NSURL URLWithString:@"http://streams.videolan.org/streams/mp4/Mr_MrsSmith-h264_aac.mp4"];
@end
3.FFmpeg
kxmovie
Vitamio
三、录音
1.AVAudioRecorder
#import "HMViewController.h"
#import <AVFoundation/AVFoundation.h> @interface HMViewController ()
- (IBAction)startRecord;
- (IBAction)stopRecord;
@property (nonatomic, strong) AVAudioRecorder *recorder;
@property (nonatomic, strong) CADisplayLink *timer;
@property (nonatomic, strong) NSTimer *stopRecordTimer;
/** 静音的持续时间 */
@property (nonatomic, assign) CGFloat slientDuration;
@end @implementation HMViewController - (void)viewDidLoad
{
[super viewDidLoad]; } - (void)addTimer
{
self.timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
[self.timer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
} - (void)removeTimer
{
[self.timer invalidate];
self.timer = nil;
} - (void)update
{
// 更新测试值
[self.recorder updateMeters]; // 如果分贝不超过-20
float power = [self.recorder averagePowerForChannel:];
if (power <= -) { // 几乎为静音
self.slientDuration += self.timer.duration; if (self.slientDuration >= ) {
// 停止录音
[self.recorder stop];
}
} else { // 有说话
self.slientDuration = ; }
} //- (void)update
//{
// // 更新测试值
// [self.recorder updateMeters];
//
// // 如果分贝不超过-20
// float power = [self.recorder averagePowerForChannel:0];
// if (power <= -20) { // 几乎为静音
// if (!self.stopRecordTimer) {
// self.stopRecordTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self.recorder selector:@selector(stopRecord) userInfo:nil repeats:NO];
// }
// } else { // 有说话
//// [self.stopRecordTimer invalidate];
//// self.stopRecordTimer = nil;
// NSDate *time = [NSDate dateWithTimeIntervalSinceNow:2.0];
// [self.stopRecordTimer setFireDate:time];
// }
//} - (IBAction)startRecord {
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [doc stringByAppendingPathComponent:@"test.caf"];
NSURL *url = [NSURL fileURLWithPath:path]; AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:url settings:nil error:nil];
// 缓冲
[recorder prepareToRecord];
// 开启分贝测量功能
recorder.meteringEnabled = YES;
// 开始录音
[recorder record];
self.recorder = recorder; // 开启定时器
[self addTimer];
} - (IBAction)stopRecord {
// [self.recorder stop];
}
IOS 多媒体 使用总结的更多相关文章
- IOS多媒体
概览 随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制,还是对麦克风.摄像 ...
- (转载)iOS 多媒体
音频:(音效.音乐) 在iOS中音频播放从形式上可以分为音效播放和音乐播放.前者主要指的是一些短音频播放,通常作为点缀音频,对于这类音频不需要进行进度.循环等控制.后者指的是一些较长的音频,通常是主音 ...
- iOS多媒体框架介绍
媒体层 媒体层包含图形技术.音频技术和视频技术,这些技术相互结合就可为移动设备带来最好的多媒体体验,更重要的是,它们让创建外观音效俱佳的应用程序变得更加容易.您可以使用iOS的高级框架更快速地创建高级 ...
- iOS多媒体总结&进入后台播放音乐
1. 播放mp3需要导入框架,AVFoundation支持音频文件(.caf..aif..wav..wmv和.mp3)的播放. #import <AVFoundation/AVFoundatio ...
- iOS,多媒体,地图相关
1.本地音频播放 2.本地视频播放 3.使用UIImagePickerController摄像头拍照,录像,照片库浏览 4.使用AVFunction,AVCaptureVideoDataOutput实 ...
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
--iOS多媒体 概览 随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制, ...
- 《转》iOS音频视频初级开发
代码改变世界 Posts - 73, Articles - 0, Comments - 1539 Cnblogs Dashboard Logout HOME CONTACT GALLERY RSS ...
- iOS应用开发详解
<iOS应用开发详解> 基本信息 作者: 郭宏志 出版社:电子工业出版社 ISBN:9787121207075 上架时间:2013-6-28 出版日期:2013 年7月 开本:16开 ...
- IOS音频1:之采用四种方式播放音频文件(一)AudioToolbox AVFoundation OpenAL AUDIO QUEUE
本文转载至 http://blog.csdn.net/u014011807/article/details/40187737 在本卷你可以学到什么? 采用四种方法设计应用于各种场合的音频播放器: 基于 ...
随机推荐
- svn的branch truck tag
对于branch truck tag一直迷迷糊糊的,想搞明白,但是一直又没来弄明白,最近就用了这种方式来开发 可以我又不是完全了解怎么操作,所以查看了下资料,这个解释得很详细呀,连我都看得懂的东西,真 ...
- Java中使用nextLine(); 没有输入就自动跳过的问题
转自:https://www.cnblogs.com/1020182600HENG/p/6564795.html [问题分析] 必要的知识:in.nextLine();不能放在in.nextInt() ...
- linux输出之 printf 讲解--->与 echo 的区别
printf 你接触过printf没呢?? 如果你学了c语言的话你肯定就熟悉了,如果没有的话,不要急,,我保证你马上就会了! 我们来看一下案例: 这个可以看出来吧,echo输出的话会对文本换行哦,但是 ...
- myeclipse更改后台代码不用重启tomcat的方法
myeclipse更改后台代码不用重启tomcat的方法 方法1:在WebRoot下的META-INF文件夹中新建一个名为context.xml文件,里面添加如下内容(要区分大小写): <C ...
- QT跟VC++结合来进行插件的验证机制
由于最近公司要开发一个以C++插件机制为主的,主要有一个问题就是C++的二进制兼容性的问题.一旦类使用虚函数,只要随便改动下增删查改下头文件的虚函数,就会导致程序在跑的时候进行乱跳,因为这个时候exe ...
- (转)远程SSH连接服务与基本排错
远程SSH连接服务与基本排错 原文:https://www.cnblogs.com/chensiqiqi/p/6224474.html#top 1.1 为什么要远程连接Linux系统 在实际的工作场景 ...
- Android模拟器访问本机服务器
Android模拟器访问本机服务器,用127.0.0.1访问不到,因为127.0.0.1已经被映射到模拟器了. 可以用以下两种方式访问 1. 用 10.0.2.2 2. 直接用 本机的IP地址,如:1 ...
- Hashtable元素的删除
2中方法 Remove(); Clear(); static void Main(string[] args) { Hashtable ht = new Hashtable(); ht.Add(1,& ...
- 调用WCF错误-There was no endpoint listening
问题描述: 今天在调用WCF服务时候出现了下面的错误. 原因: 调用服务的客户端ip设置成了固定ip.(至于固定ip为什么会导致这个错误,没能去研究) 解决方法: 将客户端ip设置成自动获取.
- EFCodeFirst 各种命令整理
1.Enable-Migrations (创建迁移目录:Migrations,如果有多个数据上下文可以用 -ContextTypeName 命令迁移对应的数据上下文 ) 2.Add-Migratio ...