IOS 獲取設備本地音視頻


1、檢索音視頻

PHFetchOptions *allPhotosOptions;
@property (nonatomic, strong) PHFetchResult *assetsFetchResults;
if (allPhotosOptions == nil) {

allPhotosOptions = [[PHFetchOptions alloc] init];
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
}
self.assetsFetchResults = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions];

其中assetsFetchResults存儲了所有的圖片信息,PHAssetMediaTypeImage為搜索圖片的類型,同樣的搜索其他類的可以根據

typedef NS_ENUM(NSInteger, PHAssetMediaType) {    PHAssetMediaTypeUnknown = 0,    PHAssetMediaTypeImage   = 1,    PHAssetMediaTypeVideo   = 2,    PHAssetMediaTypeAudio   = 3,} NS_ENUM_AVAILABLE_IOS(8_0);
2、獲取圖片UIImage

[[PHCachingImageManager defaultManager] requestImageForAsset:asset                                    targetSize:AssetGridThumbnailSize                                   contentMode:PHImageContentModeAspectFill                                       options:nil                                 resultHandler:^(UIImage *result, NSDictionary *info) {                                     // Set the cell's thumbnail image if it's still showing the same asset.                                     if ([cell.representedAssetIdentifier isEqualToString:asset.localIdentifier]) {                                         cell.thumbnailImage = result;                                     }                                 }];
asset為assetsFetchResults中的一個PHAsset,targetSize是獲取圖片的大小,這個根據你顯示的方式設置,如果需要獲取圖片其他信息比如標題可以采用
[phAsset valueForKey:@"filename"];

3、播放視頻

-(void)setVideoAsset:(PHAsset *)videoAsset{    _videoAsset = videoAsset;    [[PHImageManager defaultManager] requestPlayerItemForVideo:_videoAsset options:nil resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) {        self.currentItem = playerItem;        [self.player replaceCurrentItemWithPlayerItem:self.currentItem];        [self.currentItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];    }];}









注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com