簡體   English   中英

通過電子郵件發送音頻

[英]Send Audio by Email

我如何從手機獲取音頻並通過電子郵件分享? 它可用嗎? 我們可以像申請時的音樂一樣分享嗎?

您的問題可能會更好,您應該展示您嘗試過的內容,您的具體問題,實際結果和預期結果。

話雖如此,這聽起來像是一個很好的挑戰..所以答案如下。

我不確定你是想從iPod或應用程序包中獲取音樂所以我實現了iPod版本(在我看來),它更復雜。

SSCCE:

的main.m

#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 = @[@"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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM