CLMediaPicker 是一个开源的(几乎无缝整合)iOS 中 MediaPlayer 框架中 MPMediaPickerController 的替代品。它可用于从用户的媒体库中选择音频文件(音乐、播客、有声读物)。
1. Displays podcasts and audiobooks (unlike MPMediaPickerController in iOS 8.4+).
2. Can be used as a modal view controller or within a UINavigationController.
3. Colors can optionally be customized to match the rest of your app.
4. Images can optionally be provided as replacements for Back, Done and Cancel buttons.
5. Displays number of items chosen in title when choosing multiple items.
6. Supports landscape and portrait.
7. Supports subclassing for easier customization.
1. Displays top-level audio choices for easy browsing (Artists, Albums, Songs, Playlists, etc.)
2. Can be configured to choose only one or multiple items at once.
3. Can filter out only audio types requested.
4. Can filter out cloud items.
5. Provides a + button to include all items below the current level.
6. Provides a search bar for searching in addition to browsing.
1. Provides localization in 12 different languages.
或者,您只需将 CLMediaPicker 子目录复制到您的项目中,并确保包含 MediaPlayer 框架。
第 1 步:包含头文件
如果您使用 cocoaPods
#import <CLMediaPicker/CLMediaPicker.h>
或者如果您手动安装
#import "CLMediaPicker.h"
第 2 步:将您的类标记为实现 CLMediaPickerDelegate 协议
@interface ViewController : UIViewController<CLMediaPickerDelegate>
第 3 步:设置代理
#pragma mark - CLMediaPickerDelegate
- (void)clMediaPicker:(CLMediaPicker *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
if (mediaItemCollection)
{
// Do what you want with the collection
}
}
- (void)clMediaPickerDidCancel:(CLMediaPicker *)mediaPicker
{
[self dismissViewControllerAnimated:NO completion:nil];
}
第 4 步:实例化并显示视图控制器
CLMediaPicker *picker = [[CLMediaPicker alloc] init];
picker.mediaTypes = CLMediaPickerAll;
picker.delegate = self;
picker.allowsPickingMultipleItems = YES;
picker.showsCloudItems = NO;
[self.navigationController pushViewController:picker animated:YES];
或者如果您想以模态方式显示
CLMediaPicker *picker = [[CLMediaPicker alloc] init];
picker.mediaTypes = CLMediaPickerAll;
picker.delegate = self;
picker.allowsPickingMultipleItems = YES;
picker.showsCloudItems = NO;
picker.isModal = YES;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:picker];
[self presentViewController:navController animated:YES completion:nil];