我在Ionic4中开发音乐播放器,试图从Library(iOS设备)和iCloud中选择文件(歌曲)。
删除了以下内容:
使用iOSFilePicker(https://github.com/jcesarmobile/FilePicker-Phonegap-iOS-Plugin),大约需要1秒钟从iCloud中选择歌曲并将其加载到播放器中。没关系。
使用cordova-plugin-mediapicker(https://github.com/an-rahulpandey/cordova-plugin-mediapicker),从iOS库中选取歌曲并将其加载到播放器中大约需要6秒钟。
问题-为什么从iOS库中选取缓慢?
我仍在调查此问题,我的代码如下:
#import "MediaPicker.h"
#import <AVFoundation/AVFoundation.h>
@implementation MediaPicker
- (void) getAudio:(CDVInvokedUrlCommand *)command
{
callbackID = command.callbackId;
NSString *msong = [command argumentAtIndex:0];
NSString *iCloudItems = [command argumentAtIndex:1];
MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];
mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = [msong isEqualToString:@"true"];
mediaPicker.showsCloudItems = [iCloudItems isEqualToString:@"true"];
[self.viewController presentViewController:mediaPicker animated:YES completion:nil];
}
- (void) deleteSongs:(CDVInvokedUrlCommand *)command
{
CDVPluginResult *pluginResult = nil;
NSString *multiple = [command argumentAtIndex:0];
if ([multiple isEqualToString:@"true"]) {
NSArray *filePath = [command argumentAtIndex:1];
for(NSString *file in filePath)
{
NSString *result = [self delSingleSong:file];
NSLog(@"Delete Result = %@",result);
}
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"deleting"];
}
else
{
NSString *filePath = [command argumentAtIndex:1];
NSString *delResult = [self delSingleSong:filePath];
if([delResult isEqualToString:@"deleted"])
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"deleted"];
else
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:delResult];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (NSString *)delSingleSong:(NSString*)path
{
if([[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath:path error:&error];
if (error) {
return [error localizedDescription];
} else {
return @"deleted";
}
}
else
{
return [NSString stringWithFormat:@"File doesn't exists at the location %@",path];
}
}
- (void) mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
if (mediaItemCollection) {
NSLog(@"2a");
songsList = [[NSMutableArray alloc] init];
NSLog(@"2b");
plresult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Loading"];
NSLog(@"2c");
[plresult setKeepCallbackAsBool:YES];
NSLog(@"2d");
[self.commandDelegate sendPluginResult:plresult callbackId:callbackID];
NSLog(@"2e");
NSArray *allSelectedSongs = [mediaItemCollection items];
NSLog(@"2f");
int selcount = [allSelectedSongs count];
__block int completed = 0;
NSLog(@"2");
for(MPMediaItem *song in allSelectedSongs)
{
BOOL artImageFound = NO;
NSData *imgData;
NSString *title = [song valueForProperty:MPMediaItemPropertyTitle];
NSString *albumTitle = [song valueForProperty:MPMediaItemPropertyAlbumTitle];
NSString *artist = [song valueForProperty:MPMediaItemPropertyArtist];
NSURL *songurl = [song valueForProperty:MPMediaItemPropertyAssetURL];
MPMediaItemArtwork *artImage = [song valueForProperty:MPMediaItemPropertyArtwork];
UIImage *artworkImage = [artImage imageWithSize:CGSizeMake(artImage.bounds.size.width, artImage.bounds.size.height)];
if(artworkImage != nil){
imgData = UIImagePNGRepresentation(artworkImage);
artImageFound = YES;
}
NSLog(@"title = %@",title);
NSLog(@"albumTitle = %@",albumTitle);
NSLog(@"artist = %@",artist);
NSLog(@"songurl = %@",songurl);
// some songs are protected by DRM
if(!songurl){
plresult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"This song is protected by Digital Rights Management (DRM) and cannot be accessed."];
[self.commandDelegate sendPluginResult:plresult callbackId:callbackID];
break;
}
NSNumber *duration = [song valueForProperty:MPMediaItemPropertyPlaybackDuration];
NSString *genre = [song valueForProperty:MPMediaItemPropertyGenre];
AVURLAsset *songURL = [AVURLAsset URLAssetWithURL:songurl options:nil];
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [path objectAtIndex:0];
//NSLog(@"Compatible Preset for selected Song = %@", [AVAssetExportSession exportPresetsCompatibleWithAsset:songURL]);
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:songURL presetName:AVAssetExportPresetAppleM4A];
exporter.outputFileType = @"com.apple.m4a-audio";
NSString *filename = [NSString stringWithFormat:@"%@.m4a",title];
NSString *outputfile = [documentDir stringByAppendingPathComponent:filename];
[self delSingleSong:outputfile];
NSURL *exportURL = [NSURL fileURLWithPath:outputfile];
exporter.outputURL = exportURL;
NSLog(@"1");
[exporter exportAsynchronouslyWithCompletionHandler:^{
NSLog(@"3");
int exportStatus = exporter.status;
NSLog(@"4");
completed++;
NSLog(@"5");
switch (exportStatus) {
case AVAssetExportSessionStatusFailed:{
NSError *exportError = exporter.error;
NSLog(@"AVAssetExportSessionStatusFailed = %@",exportError);
plresult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"The operation could not be completed"];
[self.commandDelegate sendPluginResult:plresult callbackId:callbackID];
break;
}
case AVAssetExportSessionStatusCompleted:{
NSURL *audioURL = exportURL;
NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
NSLog(@"AVAssetExportSessionStatusCompleted %@",audioURL);
if(title != nil) {
[songInfo setObject:title forKey:@"title"];
} else {
[songInfo setObject:@"No Title" forKey:@"title"];
}
if(albumTitle != nil) {
[songInfo setObject:albumTitle forKey:@"albumTitle"];
} else {
[songInfo setObject:@"No Album" forKey:@"albumTitle"];
}
if(artist !=nil) {
[songInfo setObject:artist forKey:@"artist"];
} else {
[songInfo setObject:@"No Artist" forKey:@"artist"];
}
[songInfo setObject:[songurl absoluteString] forKey:@"ipodurl"];
if (artImageFound) {
[songInfo setObject:[imgData base64EncodedStringWithOptions:0] forKey:@"image"];
} else {
[songInfo setObject:@"No Image" forKey:@"image"];
}
[songInfo setObject:duration forKey:@"duration"];
if (genre != nil){
[songInfo setObject:genre forKey:@"genre"];
} else {
[songInfo setObject:@"No Genre" forKey:@"genre"];
}
[songInfo setObject:[audioURL absoluteString] forKey:@"exportedurl"];
[songInfo setObject:filename forKey:@"filename"];
[songsList addObject:songInfo];
//NSLog(@"Audio Data = %@",songsList);
NSLog(@"Export Completed = %d out of Total Selected = %d",completed,selcount);
if (completed == selcount) {
plresult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:songsList];
[self.commandDelegate sendPluginResult:plresult callbackId:callbackID];
} else {
plresult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@""];
[self.commandDelegate sendPluginResult:plresult callbackId:callbackID];
}
break;
}
case AVAssetExportSessionStatusCancelled:{
NSLog(@"AVAssetExportSessionStatusCancelled");
plresult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Cancelled"];
[self.commandDelegate sendPluginResult:plresult callbackId:callbackID];
break;
}
case AVAssetExportSessionStatusUnknown:{
NSLog(@"AVAssetExportSessionStatusCancelled");
plresult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Unknown"];
[self.commandDelegate sendPluginResult:plresult callbackId:callbackID];
break;
}
case AVAssetExportSessionStatusWaiting:{
NSLog(@"AVAssetExportSessionStatusWaiting");
plresult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Waiting"];
[self.commandDelegate sendPluginResult:plresult callbackId:callbackID];
break;
}
case AVAssetExportSessionStatusExporting:{
NSLog(@"AVAssetExportSessionStatusExporting");
plresult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Exporting"];
[self.commandDelegate sendPluginResult:plresult callbackId:callbackID];
break;
}
default:{
NSLog(@"Didnt get any status");
plresult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Didnt get any status"];
[self.commandDelegate sendPluginResult:plresult callbackId:callbackID];
break;
}
}
}];
}
}
[self.viewController dismissViewControllerAnimated:YES completion:nil];
}
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker
{
plresult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"No Song Selected"];
[self.commandDelegate sendPluginResult:plresult callbackId:callbackID];
[self.viewController dismissViewControllerAnimated:YES completion:nil];
}
@end
添加了控制台日志,似乎延迟发生在这里:
NSLog(@"1");
[exporter exportAsynchronouslyWithCompletionHandler:^{
NSLog(@"3");