How does IOS get local audio and video of a device?

Source: Internet
Author: User

How does IOS get local audio and video of a device?

1. Retrieve audio and video

 

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 stores all image information. PHAssetMediaTypeImage is the image search type.

 

typedef NS_ENUM(NSInteger, PHAssetMediaType) {    PHAssetMediaTypeUnknown = 0,    PHAssetMediaTypeImage   = 1,    PHAssetMediaTypeVideo   = 2,    PHAssetMediaTypeAudio   = 3,} NS_ENUM_AVAILABLE_IOS(8_0);
2. obtain image 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 is a PHAsset in assetsFetchResults. targetSize is used to obtain the image size. This parameter is set based on the display method. If you need to obtain other image information, such as the title, you can use
[phAsset valueForKey:@"filename"];

3. Play a video

 

-(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];    }];}


 





 



 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.