项目中常用功能,如:流媒体、健康数据(步数等)等-b
整理iOS开发中使用的各种流媒体和常用的高级功能。由于时间关系,目前只写了一部分功能,全部都采用的是系统方法,没用第三方,截图如下:
screen1.png
screen2.png
个人比较懒,不爱多写文字,直接上代码,哈哈。
视频
系统用AVFoundation与MediaPlayer框架实现播放视频的方案。其中AVFoundation扩展性好,都需自定义功能,而MediaPlayer集成简单,但是样式不可扩展。
1.AVFoundation使用AVPlayer播放视频,它属于view的layer层。其功能都需要自定义,如音量、暂停、播放时长等。代码如下:
- (void)addAVPlayer{
self.playerItem = [AVPlayerItem playerItemWithURL:self.movieURL]; self.player = [AVPlayer playerWithPlayerItem:self.playerItem]; AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
layer.frame = CGRectMake(0, 70, kScreenWidth, 300);
layer.videoGravity = AVLayerVideoGravityResizeAspect;
layer.backgroundColor = [[UIColor blackColor] CGColor];
[self.view.layer addSublayer:layer];
[self.player play];
}
#pragma mark - Observer//添加进度观察
- (void)addProgressObserver {
// 设置每秒执行一次
__weak LXAVPlayVideoController *wSelf = self;
[self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue: NULL usingBlock:^(CMTime time) { // NSLog(@"进度观察 + %f", wSelf.topProgressSlider.value); // 获取当前时间
CMTime currentTime = wSelf.player.currentItem.currentTime; // 转化成秒数
CGFloat currentPlayTime = (CGFloat)currentTime.value/currentTime.timescale; // 总时间
CMTime totalTime = wSelf.playerItem.duration; // 转化成秒
wSelf.totalMovieDuration = (CGFloat)totalTime.value/totalTime.timescale;
wSelf.topProgressSlider.value = currentPlayTime/wSelf.totalMovieDuration;
wSelf.progressValue = CMTimeGetSeconds(currentTime)/wSelf.totalMovieDuration;
wSelf.topPastTimeLabel.text = [LXHelpClass getTimeByProgress:currentPlayTime];
wSelf.topRemainderLabel.text = [LXHelpClass getTimeByProgress:wSelf.totalMovieDuration - currentPlayTime]; // NSLog(@"%f %f %f %f", wSelf.topProgressSlider.value, wSelf.totalMovieDuration, currentPlayTime, currentPlayTime/wSelf.totalMovieDuration);
}];
}
//播放结束后的代理回调
- (void)moviePlayDidEnd:(NSNotification *)notify {
[self setMovieParse];
}
#pragma mark - Enent response//播放进度
- (void)topSliderValueChangedAction:(UISlider *)sender {
NSLog(@"进度条进度 + %f", sender.value);
double currentTime = floor(self.totalMovieDuration * sender.value); //转换成CMTime才能给player来控制播放进度
CMTime dragedCMTime = CMTimeMake(currentTime, 1);
[self.player seekToTime:dragedCMTime completionHandler:^(BOOL finished) {
[self.player play];
}];
}
//音量slider
- (void)bottomSoundSliderAction:(UISlider *)sender {
[self.player setVolume:sender.value];
self.bottomSoundSlider.value = sender.value;
if (sender.value == 0) {
NSLog(@"静音");
}
}
//播放暂停按钮
- (void)playBtnClicked:(UIButton *)sender {
if (isPlay) {
[self setMovieParse];
} else {
[self setMoviePlay];
}
isPlay ^= 1;
}
#pragma mark - Private methods
- (void)addNotificationCenters {
// 注册观察者用来观察,是否播放完毕
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}
- (void)setMovieParse {
[self.player pause];
}
- (void)setMoviePlay {
[self.player play];
}
2.MediaPlayer框架提供MPMoviePlayerController与MPMoviePlayerViewController播 放视频,它们区别就是MPMoviePlayerViewController里面包含了一个MPMoviePlayerController,另外 MPMoviePlayerViewController可以看作是一个控制器播放视频的。系统已经为之集成好了音量、播放暂停等功能。
MPMoviePlayerController的使用
- (void)addMPMoviePlayer{
MPMoviePlayerController *mpPlayer = [[MPMoviePlayerController alloc] init];
mpPlayer.view.frame = CGRectMake(0, 20, kScreenWidth, kScreenWidth/videoSizeRate);
mpPlayer.backgroundView.backgroundColor = [UIColor blackColor];
mpPlayer.scalingMode = MPMovieScalingModeAspectFill;
[self.view addSubview:mpPlayer.view];
self.mpPlayer = mpPlayer;
}
#pragma mark - Observer
- (void)enterFullscreen:(NSNotification *)noti {
[self fullScreen:YES];
[LXHelpClass setDeviceLandscape:direction];
}
- (void)exitFullscreen:(NSNotification *)noti {
[self fullScreen:NO];
UIDeviceOrientation orient = [UIDevice currentDevice].orientation;
if (orient != UIDeviceOrientationPortrait) {
direction = UIDeviceOrientationPortrait;
[LXHelpClass setDeviceLandscape:direction]; }
direction = UIDeviceOrientationLandscapeLeft;
}
- (void)loadCompletion:(NSNotification *)noti {
[self.videoLoading stopAnimating];
}
- (void)orientChange:(NSNotification *)noti {
UIDeviceOrientation orient = [UIDevice currentDevice].orientation;
switch (orient) {
case UIDeviceOrientationPortrait:
self.mpPlayer.fullscreen = NO;
break;
case UIDeviceOrientationLandscapeLeft:
direction = UIDeviceOrientationLandscapeLeft;
self.mpPlayer.fullscreen = YES;
break;
case UIDeviceOrientationPortraitUpsideDown:
self.mpPlayer.fullscreen = NO;
break;
case UIDeviceOrientationLandscapeRight:
direction = UIDeviceOrientationLandscapeRight;
self.mpPlayer.fullscreen = YES;
break;
default:
break;
}
} - (void)mediaPlayerPlaybackStateChange:(NSNotification *)notification {
switch (self.mpPlayer.playbackState) {
case MPMoviePlaybackStatePlaying:
NSLog(@"正在播放...");
break;
case MPMoviePlaybackStatePaused:
NSLog(@"暂停播放.");//注意播放完成时的状态是暂停
break;
case MPMoviePlaybackStateStopped:
NSLog(@"停止播放.");
break;
default:
NSLog(@"播放状态:%li",self.mpPlayer.playbackState);
break;
}
}
- (void)mediaPlayerThumbnailRequestFinished:(NSNotification *)notification {
NSLog(@"视频截图完成.");
UIImage *image=notification.userInfo[MPMoviePlayerThumbnailImageKey]; //保存图片到相册(首次调用会请求用户获得访问相册权限)
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
MPMoviePlayerViewController的使用
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = bgColor;
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:self.mp4URL];
player.view.frame = CGRectMake(0, 0, kScreenWidth, 300);
[self.view addSubview:player.view];
self.player = player;
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake((kScreenWidth-100)/2.0, kScreenHeight - 64 - 100, 100, 50);
[button setTitle:@"全屏播放" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonClick {
[self presentMoviePlayerViewControllerAnimated:self.player];
}
音频
系统也提供灵活性很强的AVAudioPlayer和集成快的MPMusicPlayerController播放音乐。
具体功能实现请查看源码,这里不做过多介绍。
健康
这方面的资料比较少,只好查看官方文档。这里我只写了获取步数这个常用功能。HealthKit框架提供了许多获取健康数据的API。
LXHealthKitManager *myHealth = [LXHealthKitManager sharedLXHealthKitManager];
myHealth.startDate = [LXHealthKitManager getTodayAgoWithDays:30];//获取今天之前三十天的步数[myHealth getStepCountWithSuccess:^(NSArray *results) {
[self.modelArr setArray:results];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
} withfailure:^(NSError *error) {
NSLog(@"error:%@", error);
}];
LXHealthKitManager的代码如下:
- (instancetype)init {
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (void)setup {
if ([HKHealthStore isHealthDataAvailable]) {
NSLog(@"此设备能使用健康数据");
HKHealthStore *healthStore = [[HKHealthStore alloc] init];
[healthStore requestAuthorizationToShareTypes:[self shareTypes] readTypes:[self readTypes] completion:^(BOOL success, NSError *error) {
if (success == YES) {
NSLog(@"授权成功");
} else {
NSLog(@"授权失败");
} }];
self.healthStore = healthStore;
} else {
NSLog(@"此设备不能使用健康数据");
}
}
- (void)getStepCountWithSuccess:(void (^)(NSArray *))success withfailure:(void (^)(NSError *))failure {
HKQuantityType *quantityType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:self.startDate endDate:self.endDate options:HKQueryOptionStrictEndDate]; NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.day = 1;
HKStatisticsCollectionQuery *collectionQuery = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:quantityType quantitySamplePredicate:predicate options:HKStatisticsOptionCumulativeSum | HKStatisticsOptionSeparateBySource anchorDate:self.startDate intervalComponents:dateComponents];
collectionQuery.initialResultsHandler = ^(HKStatisticsCollectionQuery *query, HKStatisticsCollection * __nullable result, NSError * __nullable error) { NSMutableArray *returnArr = [NSMutableArray array]; for (NSInteger i = result.statistics.count - 1; i >= 0; i--) {
HKStatistics *statistic = result.statistics[i]; for (HKSource *source in statistic.sources) { if ([source.name isEqualToString:[UIDevice currentDevice].name]) {
LXHealthStepModel *model = [[LXHealthStepModel alloc] init];
model.startDateStr = [self changeToDateStrWithDate:statistic.startDate];
model.endDateStr = [self changeToDateStrWithDate:statistic.endDate];
HKQuantity *quantity = [statistic sumQuantityForSource:source]; if ([quantity isCompatibleWithUnit:[HKUnit countUnit]]) {
model.stepCount = (NSInteger)[quantity doubleValueForUnit:[HKUnit countUnit]];
}
[returnArr addObject:model];
}
}
}
if (!error) {
if (success) {
success(returnArr);
}
} else if (failure) {
failure(error);
}
};
[self.healthStore executeQuery:collectionQuery];
}
源码请点击github地址:https://github.com/SoftProgramLX/LXBaseFunction
简书作者:App开发LX
地址:http://www.jianshu.com/p/c62886e0ac3c
项目中常用功能,如:流媒体、健康数据(步数等)等-b的更多相关文章
- 项目中常用js方法整理common.js
抽空把项目中常用js方法整理成了common.js,都是网上搜集而来的,大家一起分享吧. var h = {}; h.get = function (url, data, ok, error) { $ ...
- 记录下项目中常用到的JavaScript/JQuery代码二(大量实例)
记录下项目中常用到的JavaScript/JQuery代码一(大量实例) 1.input输入框监听变化 <input type="text" style="widt ...
- 项目中常用的MySQL 优化
本文我们来谈谈项目中常用的MySQL优化方法,共19条,具体如下: 一.EXPLAIN 做MySQL优化,我们要善用EXPLAIN查看SQL执行计划. 下面来个简单的示例,标注(1.2.3.4.5)我 ...
- JAVA项目中常用的异常处理情况总结
JAVA项目中常用的异常知识点总结 1. java.lang.nullpointerexception这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用 ...
- JAVA项目中常用的异常知识点总结
JAVA项目中常用的异常知识点总结 1. java.lang.nullpointerexception这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用 ...
- ES6系列之项目中常用的新特性
ES6系列之项目中常用的新特性 ES6常用特性 平时项目开发中灵活运用ES6+语法可以让开发者减少很多开发时间,提高工作效率.ES6版本提供了很多新的特性,接下来我列举项目中常用的ES6+的特性: l ...
- Android 项目中常用到的第三方组件
项目中常用到的第三方组件 1 社会化分享ShareSDK-Core-2.5.9.jarShareSDK-QQ-2.5.9.jarShareSDK-QZone-2.5.9.jarShareSDK-Sin ...
- iOS项目开发常用功能静态库
YHDeveloperTools iOS项目开发常用功能静态库 查看源码 功能方法: 1.字符检查 [NSString checkStringWithType:Email andTargetStrin ...
- VB的一些项目中常用的通用方法-一般用于验证类
1.VB的一些项目中常用的通用方法: ' 设置校验键盘输入值,数字 Public Function kyd(key As Integer) As Integer Dim mychar mychar = ...
随机推荐
- ASP.NET MVC 4 Ajax上传文件
这两天一直纠结着表单的问题.想在一个表单里实现三个功能: 输入查询条件,点击查询: 导出查询数据: 上传文件: 方法有很多,乱花渐欲迷人眼,尝试了很多,无果.大致说的是,给不同按钮写js代码,在js代 ...
- swift项目中嵌入oc
参考资料 需要注意的是 与oc包含swift不同的是 swift包含oc需要在桥接文件中包含要使用的oc的头文件 demo:swiftPlayOc(提取码:37c6)
- 记一次大量 TCP 连接失败
背景 在一段没有日志的历史遗留代码上面加入监控部署后不久,就收到了服务调用成功率低的告警,真是哗了狗了 解决过程 client端在线上单机部署,根据监控上面的返回码比例看出失败原因都是链接失败,通过 ...
- linux 软件安装各种方法
一 简单介绍 1.软件安装卸载,分几种情况: A:RPM包 这种软件包就像windows的EXE安装文件一样,各种文件已经编译好,并打了包,哪个文件该放到哪个文件夹,都指定好了,安装非常方便,在图 ...
- uiautomator的坑和AAPT命令方式启动一个应用程序
最近在使用UIautomator完成公司的一个主流程的自动化,因为不适用H5和IOS所以会放弃这个工具的使用,现在记录在使用uiautomator的一些问题: 案列1:使用命令去启动要运用的apk包 ...
- 安卓开发错误:The type android.support.v4.app.TaskStackBuilder$SupportParentable cannot be resolved.
今天在使用低版本下的ActionBar,在继承ActionBarActivity时报了"The type Android.support.v4.app.TaskStackBuilder$Su ...
- JAXB - Calling marshal
Only a handful of source code lines is required to make a JAXB Marshaller object write a document tr ...
- scala学习笔记:match与unapply()
编写如下代码: object MatchTest { def foo(a : Any) : String = { a match { case 1 => "int:1" ca ...
- MySQL与NoSQL——SQL与NoSQL的融合
来源:http://www.cnblogs.com/sunli/archive/2011/05/11/mysql-nosql.html 写这一篇内容的原因是MySQL5.6.2突然推出了memcach ...
- hdu 1715 大菲波数(高精度数)
Problem Description Fibonacci数列,定义如下: f(1)=f(2)=1 f(n)=f(n-1)+f(n-2) n>=3. 计算第n项Fibonacci数值. Inpu ...