美文网首页iOS 宝典iOS进阶指南程序员
项目中常用功能,如:流媒体、健康数据(步数等)等

项目中常用功能,如:流媒体、健康数据(步数等)等

作者: lixuCoding | 来源:发表于2016-08-10 20:26 被阅读262次

整理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地址下载。


QQ:2239344645 我的github

相关文章

  • 项目中常用功能,如:流媒体、健康数据(步数等)等

    整理iOS开发中使用的各种流媒体和常用的高级功能。由于时间关系,目前只写了一部分功能,全部都采用的是系统方法,没用...

  • iOS CMPedometer获取运动步数

    CMPedometer是iOS 8.0推出的,用来统计用户的的运动数据(步数、楼层、公里数等)。最近项目需...

  • 电商数据仓库项目简介

    项目简介 基于hadoop生态搭建的电商数据仓库,整体功能架构包含数据采集、数仓搭建、数据导出、数据可视化等。 g...

  • AJAX

    伪ajax教程 ​ 能大致学会ajax是什么,怎么用,项目中常用ajax插件以及如何mock数据等技巧.这里只...

  • 【经验】iOS HealthKit读取运动步数的问题记录

    一、前言 最近公司的项目需要做一个读取用户健康数据中运动步数的功能,过程中遇到一个获取到的步数始终不准确的问题,经...

  • 读取用户步数

    现在很多App都会有获取用户步数的功能,比如微信,爱奇艺、淘宝等。那么如果要获取用户步数,就要用到苹果等Healt...

  • 数据产品经理,该如何搭建数据指标体系

    数据产品经理,除了应该具备的如:沟通/原型/文档/项目管理等基本技能外,还应该具有精于“数据”的核心专业技能如【数...

  • 车间数字化MES项目概述

    车间MES项目 1,数据采集模块:设备信息如报警,产量,刀具等信息的实时上传2,网页功能模块:采用Django +...

  • 为Beespeed App提供技术支持

    介绍: Beespeed是匹配智能产品的应用,记录并管理计步、卡路里、距离等运动数据;心率、睡眠等健康数据实时监测...

  • 弱网测试方案探索

    最近在搞一个智能穿戴项目。手环手表等智能穿戴项目中最核心的功能是运动计步功能。 计步功能的业务逻辑是主要流程是通过...

网友评论

    本文标题:项目中常用功能,如:流媒体、健康数据(步数等)等

    本文链接:https://www.haomeiwen.com/subject/eeejsttx.html