端午节前,把公司的项目忙完了,这几天开始继续DDPlayer的开发,熟悉代码之后,首先要解决的是:获取并播放相册里面的视频。
对于相册中的视频,我需要关注视频的名称、时常、格式、缩略图等信息,因此,定义了表示视频信息的对象。

//AlbumVideoInfo.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>@interface AlbumVideoInfo : NSObject@property(nonatomic, copy) NSString *name;
@property(nonatomic, assign) long long size; //Bytes
@property(nonatomic, strong) NSNumber *duration;
@property(nonatomic, copy) NSString *format;
@property(nonatomic, strong) UIImage *thumbnail;
@property (nonatomic, strong) NSURL *videoURL;@end//AlbumVideoInfo.m
#import "AlbumVideoInfo.h"@implementation AlbumVideoInfo@end

然后,从相册load视频信息,并保存在数组中,相关关键函数有:

//加载视频
- (void)loadVideos{ALAssetsLibrary *library1 = [[ALAssetsLibrary alloc] init];[library1 enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {if (group) {[group setAssetsFilter:[ALAssetsFilter allVideos]];[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {if (result) {AlbumVideoInfo *videoInfo = [[AlbumVideoInfo alloc] init];videoInfo.thumbnail = [UIImage imageWithCGImage:result.thumbnail];
//                    videoInfo.videoURL = [result valueForProperty:ALAssetPropertyAssetURL];videoInfo.videoURL = result.defaultRepresentation.url;videoInfo.duration = [result valueForProperty:ALAssetPropertyDuration];videoInfo.name = [self getFormatedDateStringOfDate:[result valueForProperty:ALAssetPropertyDate]];videoInfo.size = result.defaultRepresentation.size; //BytesvideoInfo.format = [result.defaultRepresentation.filename pathExtension];[_albumVideoInfos addObject:videoInfo];}}];} else {//没有更多的group时,即可认为已经加载完成。NSLog(@"after load, the total alumvideo count is %ld",_albumVideoInfos.count);dispatch_async(dispatch_get_main_queue(), ^{[self showAlbumVideos];});}} failureBlock:^(NSError *error) {NSLog(@"Failed.");}];
}
//将创建日期作为文件名
-(NSString*)getFormatedDateStringOfDate:(NSDate*)date{NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];[dateFormatter setDateFormat:@"yyyyMMddHHmmss"]; //注意时间的格式:MM表示月份,mm表示分钟,HH用24小时制,小hh是12小时制。NSString* dateString = [dateFormatter stringFromDate:date];return dateString;
}

最后,就是播放视频,由于相册视频不能获取到绝对地址,而KxMovieViewController初始化的参数只能是NSString类型的path(本地、远程都可以),我尝试使用相册视频url的absulutstring来初始化它,没有成功,故改为使用系统自带的MPMoviePlayerController。相关关键代码如下:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if (self) {// Custom initialization_albumVideoInfos = [[NSMutableArray alloc] initWithCapacity:50];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playDidChangeNotification:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(playMovieFinishedCallback:)name:MPMoviePlayerPlaybackDidFinishNotificationobject:nil];//MPMoviePlayerController fullscreen 模式下,点击左上角的done按钮,会调用exitFullScreen通知。[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitFullScreen:) name: MPMoviePlayerDidExitFullscreenNotification object:nil];}return self;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{NSInteger row = indexPath.row;if (row < _albumVideoInfos.count) {AlbumVideoInfo *albumVideoInfo = _albumVideoInfos[row];if (!_moviePlayer) {_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:albumVideoInfo.videoURL];}else{[_moviePlayer setContentURL:albumVideoInfo.videoURL];}_moviePlayer.view.frame = self.view.bounds;_moviePlayer.backgroundView.backgroundColor = [UIColor orangeColor];[self.view addSubview:_moviePlayer.view];_moviePlayer.controlStyle = MPMovieControlStyleFullscreen;_moviePlayer.shouldAutoplay = YES;_moviePlayer.repeatMode = MPMovieRepeatModeOne;[_moviePlayer setFullscreen:YES animated:YES];_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;[_moviePlayer play];}
}

相关通知的处理:

#pragma mark - handle notification- (void)playDidChangeNotification:(NSNotification *)notification {MPMoviePlayerController *moviePlayer = notification.object;MPMoviePlaybackState playState = moviePlayer.playbackState;if (playState == MPMoviePlaybackStateStopped) {NSLog(@"停止");} else if(playState == MPMoviePlaybackStatePlaying) {NSLog(@"播放");} else if(playState == MPMoviePlaybackStatePaused) {NSLog(@"暂停");}
}- (void)playMovieFinishedCallback:(NSNotification *)notification{NSLog(@"finish");
}- (void)exitFullScreen:(NSNotification *)notification{NSLog(@"exitFullScreen");[_moviePlayer stop];[_moviePlayer.view removeFromSuperview];
}

OK,重要的事情都说完了,but,在强调一下一个小细节。
//MPMoviePlayerController fullscreen 模式下,点击左上角的done按钮,会调用exitFullScreen通知。

ios获取所有相册的视频并播放相关推荐

  1. iOS 获取本地相册图片或视频

    1.先判断iOS 相机相册的权限检测 相关链接 https://www.jianshu.com/p/8a944534e0ee 2.iOS 获取本地相册图片 iOS 获取本地相册图片一 iOS 获取本地 ...

  2. iOS 获取系统相册

    在iOS开发中经常会用到相册的图片,但是原生的UIImagePikerViewDelegate只能选取一张照片进行处理,这样管理起来比较麻烦,所以本次带来获取系统所有相册. 首先建一个继承NSObje ...

  3. iOS——微信朋友圈小视频的播放和聊天窗口小视频的播放

    小视频是微信6.0版本重大功能之一,在开发过程中遇到不少问题.本文先叙述小视频的产品需求,介绍了几个实现方案,分析每个方案的优缺点,最后总结出最优的解决方案. 小视频播放需求 可以同时播放多个视频 用 ...

  4. iOS - 获取系统相册照片名称,路径以及各项信息

    最近做的项目 , 在我看来都停偏的 , 因为需要的都不仅仅是展示和业务逻辑 , 而主要都是网络和存储,文件,流媒体操作方面的东西 . 所以今天想要获取下照片的名称 , 还找了挺久的 . 以此记录下 , ...

  5. iOS 录视频,相册选择视频,视频压缩,存储本地文件,播放,上传

    iOS 录视频,相册选择视频,视频压缩,存储本地文件,播放,上传 工程中用到了这部分的功能,也纠结了几天后做完了,现在总结下这部分的东西. 先说描述下需求: (1)从相册取视频.录视频: (2)视频转 ...

  6. ios 获取相册第一个视频_历史上第一个视频游戏

    ios 获取相册第一个视频 重点 (Top highlight) Looking closely at the oscilloscope, you what function the picture ...

  7. RequestBody获取前端数据_360视频云Web前端HEVC播放器实践剖析

    360视频云前端团队围绕HEVC前端播放及解密实现了一套基于WebAssembly.WebWorker的通用模块化Web播放器,在LiveVideoStackCon2019深圳的演讲中360奇舞团We ...

  8. react项目中使用阿里播放器播放视频,包括切换视频,播放定时跳转(兼容ios和andro),播放完成

    react项目中使用阿里播放器播放视频,包括切换视频,播放定时跳转(兼容ios和andro),播放完成 1.index.html引入阿里播放器的cdn <link rel="style ...

  9. iOS 微信 音视频自动播放 原生接口WeixinJSBridge API(一些整理 小技巧)

    原文链接1:https://www.w3ctech.com/topic/1165 原文链接2:https://www.cnblogs.com/jasonduan/p/5635048.html 做一下整 ...

最新文章

  1. jQuery简单实现iframe的高度根据页面内容自适应的方法(转)
  2. OCS 2007 聊天记录查看工具 OCSMessage
  3. 使用Filter跟踪Asp.net MVC页面加载(转)
  4. C语言在VS2017环境下写俄罗斯方块的感悟
  5. Qt Creator使用自定义着色器
  6. stm32 led屏控制卡_室内LED显示屏如何安装?
  7. 苹果手机耗电快_iPhone12用5G耗电快,苹果回应
  8. 宇视科技android面试_宇视科技软件笔试面试
  9. u3d中如何添加avatar和状态机
  10. Problem B: 取石子
  11. Atitit 获取数据库表主键功能的实现 数据库模块 艾提拉attilax总结 package com.attilax.sql; import java.sql.Connection; im
  12. java使用百度翻译接口实现前后端翻译功能
  13. 解决桌面单击右键反应慢的问题
  14. 产业安全专家谈丨数字经济高速发展,数据要素安全该如何保障?
  15. 怎么设置路由器当交换机用
  16. java unicode 藏文_UNICODE 区域对照表
  17. 对比学习知识扩展——一堆奇奇怪怪的loss,快把我压死了orz...
  18. 2020 A survey on HAR Based on Temporal Signals of Portable Inertial Sensors
  19. 近期Android错误
  20. Linux依赖包的下载方式

热门文章

  1. 深入剖析Auto Layout,分析iOS各版本新增特性
  2. matlab:曲线拟合
  3. idea中用java不能自动导包的解决办法
  4. SQLaichemy三种排序方式
  5. (已解决)oracle 查询数据一直提示:“error code [17004]; 无效的列类型”
  6. python打印古诗_python打印古诗_python教程:利用python基础知识取出对应诗句
  7. [Mysql] 创建和操纵数据表
  8. ORACLE各种常见java.sql.SQLException归纳
  9. 线程池主要参数及作用
  10. matlab三维曲线的绘制