我如何从电话中获取音频并通过电子邮件共享?
可以使用吗?
我们能否在应用程序中像音乐一样简单地共享它?

最佳答案

您的问题可能会更好,应该显示您尝试过的内容,特定的问题,实际结果和预期结果。

话虽如此,这听起来像是一个不错的挑战。

我不确定您是否要从iPod或应用程序包中获取音乐,因此我将iPod版本实现为(我认为)更为复杂。

SSCCE:

主目录

#import "AppDelegate.h"

int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

AppDelegate.h
@import UIKit;
@import MessageUI;
@import MediaPlayer;

@interface AppDelegate : UIResponder <UIApplicationDelegate, MFMailComposeViewControllerDelegate, MPMediaPickerControllerDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m
@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 = @[@"[email protected]"];

        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

关于ios - 通过电子邮件发送音频,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20629436/

10-09 14:13