我如何从手机获取音频并通过电子邮件分享? 它可用吗? 我们可以像应用中的音乐一样分享它吗?
答案 0 :(得分:2)
您的问题可能会更好,您应该展示您尝试过的内容,您的具体问题,实际结果和预期结果。
话虽如此,这听起来像是一个很好的挑战..所以答案如下。
我不确定你是想从iPod或应用程序包中获取音乐,所以我实现了iPod版本(在我看来),它更复杂。
#import "AppDelegate.h"
int main(int argc, char * argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
@import UIKit;
@import MessageUI;
@import MediaPlayer;
@interface AppDelegate : UIResponder <UIApplicationDelegate, MFMailComposeViewControllerDelegate, MPMediaPickerControllerDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
UIViewController *controller = [[UIViewController alloc] init];
_window.rootViewController = controller;
MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAny];
mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = YES;
mediaPicker.prompt = @"Select songs to play";
[_window.rootViewController presentViewController:mediaPicker animated:YES completion:^{
}];
return YES;
}
-(void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
NSLog(@"Result:%d", result);
[controller dismissViewControllerAnimated:YES completion:nil];
}
-(void) mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
[mediaPicker dismissViewControllerAnimated:YES completion:^{
NSArray *recipents = @[@"a@b.com"];
MFMailComposeViewController *messageController = [[MFMailComposeViewController alloc] init];
messageController.mailComposeDelegate = self;
[messageController setToRecipients:recipents];
[messageController setMessageBody:@"Here is a music track" isHTML:NO];
[messageController setSubject:@"Music"];
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
for (MPMediaItem *item in mediaItemCollection.items)
{
NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
AVAsset *asset = [AVAsset assetWithURL:url];
NSArray *presets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset];
AVAssetExportSession *session = [AVAssetExportSession exportSessionWithAsset:asset presetName:presets[0]];
session.outputURL = [[tmpDirURL URLByAppendingPathComponent:@"item"] URLByAppendingPathExtension:@"m4a"];
session.outputFileType = [session supportedFileTypes][0];
[session exportAsynchronouslyWithCompletionHandler:^{
NSData *data = [NSData dataWithContentsOfURL:session.outputURL];
[messageController addAttachmentData:data mimeType:@"audio/mp4" fileName:@"musicAttachment.m4a"];
}];
}
[_window.rootViewController presentViewController:messageController animated:YES completion:^{
}];
}];
}
-(void) mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker
{
}
@end