Iphone 在iOS中从gallery附加声音文件

Iphone 在iOS中从gallery附加声音文件,iphone,ios,xcode,Iphone,Ios,Xcode,是否可以像我的iOS应用程序中的图像库一样访问声音文件库,我尝试在网上搜索,但没有找到任何关于它的好内容。你可以参考苹果的示例应用程序,看看它是如何完成的 您可以参考苹果的示例应用程序,看看它是如何完成的 是的,您可以将其上载到服务器。使用以下代码: 当您从选取器中选择歌曲时 - (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaIte

是否可以像我的iOS应用程序中的图像库一样访问声音文件库,我尝试在网上搜索,但没有找到任何关于它的好内容。

你可以参考苹果的示例应用程序,看看它是如何完成的

您可以参考苹果的示例应用程序,看看它是如何完成的

是的,您可以将其上载到服务器。使用以下代码:

当您从选取器中选择歌曲时

- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection 
{
    MPMediaItem *song = nil ;
    [self dismissModalViewControllerAnimated:YES];

    if ([mediaItemCollection count] < 1) 
    {
        return;
    }
    [song release];
    song = [[[mediaItemCollection items] objectAtIndex:0] retain];

    //song.accessibilityHin

    [self uploadMusicFile:song];
}

- (void) uploadMusicFile:(MPMediaItem *)song
{
    // Init audio with playback capability
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

    NSURL *assetURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
    AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
 // to get uniqe time interval
    [[NSDate date] timeIntervalSince1970];
    // convert this ti string
    NSTimeInterval seconds = [[NSDate date] timeIntervalSince1970];
    NSString *intervalSeconds = [NSString stringWithFormat:@"%0.0f",seconds];

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset                                                                       presetName: AVAssetExportPresetPassthrough];

    exporter.outputFileType = @"public.mpeg-4";

    NSString *exportFile = [[NSString alloc] initWithString:[DOCUMENTS_FOLDER stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",intervalSeconds]]];


    NSURL *exportURL = [[NSURL fileURLWithPath:exportFile] retain];
    exporter.outputURL = exportURL; 

    [exporter exportAsynchronouslyWithCompletionHandler:
     ^{
         NSData *data = [NSData dataWithContentsOfURL:exportURL];

         // Here your upload code 
     }];
}
-(void)mediaPicker:(MPMediaPickerController*)mediaPicker didPickMediaItems:(MPMediaItemCollection*)mediaItemCollection
{
MPMediaItem*song=nil;
[自我解散Modalviewcontrolleranimated:是];
如果([mediaItemCollection计数]<1)
{
返回;
}
[歌曲发布];
song=[[mediaItemCollection项目]对象索引:0]保留];
//易接近性
[自上传音乐文件:歌曲];
}
-(无效)上传音乐文件:(MPMediaItem*)歌曲
{
//具有播放功能的初始化音频
AVAudioSession*audioSession=[AVAudioSession sharedInstance];
[audioSession setCategory:AvaudioSessionCategory回放错误:无];
NSURL*assetURL=[song valueForProperty:MPMediaItemPropertyAsetUrl];
AVURLAsset*songAsset=[AvurlAssetUrlAssetTwithUrl:assetURL选项:无];
//获取uniqe时间间隔的步骤
[[NSDate date]时间间隔自1970年起];
//转换此ti字符串
NSTimeInterval秒数=[[NSDate date]TIMEINTERVALSCENCES1970];
NSString*intervalSeconds=[NSString stringWithFormat:@“%0.0f”,秒];
AVAssetExportSession*exporter=[[AVAssetExportSession alloc]initWithAsset:songAsset预设名称:AVAssetExportPresetPassthrough];
exporter.outputFileType=@“public.mpeg-4”;
NSString*exportFile=[[NSString alloc]initWithString:[DOCUMENTS\u文件夹stringByAppendingPathComponent:[NSString stringWithFormat:@“%@.mp4”,intervalSeconds]];
NSURL*exportURL=[[NSURL fileURLWithPath:exportFile]retain];
exporter.outputURL=导出URL;
[exporter exportAsynchronouslyWithCompletionHandler:
^{
NSData*data=[NSData dataWithContentsOfURL:exportURL];
//这是你的上传代码
}];
}

是,您可以将其上载到服务器。使用以下代码:

当您从选取器中选择歌曲时

- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection 
{
    MPMediaItem *song = nil ;
    [self dismissModalViewControllerAnimated:YES];

    if ([mediaItemCollection count] < 1) 
    {
        return;
    }
    [song release];
    song = [[[mediaItemCollection items] objectAtIndex:0] retain];

    //song.accessibilityHin

    [self uploadMusicFile:song];
}

- (void) uploadMusicFile:(MPMediaItem *)song
{
    // Init audio with playback capability
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

    NSURL *assetURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
    AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
 // to get uniqe time interval
    [[NSDate date] timeIntervalSince1970];
    // convert this ti string
    NSTimeInterval seconds = [[NSDate date] timeIntervalSince1970];
    NSString *intervalSeconds = [NSString stringWithFormat:@"%0.0f",seconds];

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset                                                                       presetName: AVAssetExportPresetPassthrough];

    exporter.outputFileType = @"public.mpeg-4";

    NSString *exportFile = [[NSString alloc] initWithString:[DOCUMENTS_FOLDER stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",intervalSeconds]]];


    NSURL *exportURL = [[NSURL fileURLWithPath:exportFile] retain];
    exporter.outputURL = exportURL; 

    [exporter exportAsynchronouslyWithCompletionHandler:
     ^{
         NSData *data = [NSData dataWithContentsOfURL:exportURL];

         // Here your upload code 
     }];
}
-(void)mediaPicker:(MPMediaPickerController*)mediaPicker didPickMediaItems:(MPMediaItemCollection*)mediaItemCollection
{
MPMediaItem*song=nil;
[自我解散Modalviewcontrolleranimated:是];
如果([mediaItemCollection计数]<1)
{
返回;
}
[歌曲发布];
song=[[mediaItemCollection项目]对象索引:0]保留];
//易接近性
[自上传音乐文件:歌曲];
}
-(无效)上传音乐文件:(MPMediaItem*)歌曲
{
//具有播放功能的初始化音频
AVAudioSession*audioSession=[AVAudioSession sharedInstance];
[audioSession setCategory:AvaudioSessionCategory回放错误:无];
NSURL*assetURL=[song valueForProperty:MPMediaItemPropertyAsetUrl];
AVURLAsset*songAsset=[AvurlAssetUrlAssetTwithUrl:assetURL选项:无];
//获取uniqe时间间隔的步骤
[[NSDate date]时间间隔自1970年起];
//转换此ti字符串
NSTimeInterval秒数=[[NSDate date]TIMEINTERVALSCENCES1970];
NSString*intervalSeconds=[NSString stringWithFormat:@“%0.0f”,秒];
AVAssetExportSession*exporter=[[AVAssetExportSession alloc]initWithAsset:songAsset预设名称:AVAssetExportPresetPassthrough];
exporter.outputFileType=@“public.mpeg-4”;
NSString*exportFile=[[NSString alloc]initWithString:[DOCUMENTS\u文件夹stringByAppendingPathComponent:[NSString stringWithFormat:@“%@.mp4”,intervalSeconds]];
NSURL*exportURL=[[NSURL fileURLWithPath:exportFile]retain];
exporter.outputURL=导出URL;
[exporter exportAsynchronouslyWithCompletionHandler:
^{
NSData*data=[NSData dataWithContentsOfURL:exportURL];
//这是你的上传代码
}];
}

也可以将这些文件发送到服务器吗?也可以将这些文件发送到服务器吗?下面给出的解决方案对您有效吗?因为问题从现在起已经问了3年了,它今天是否有效?下面给出的解决方案对您有效吗?因为问题从现在起已经问了3年了,它今天是否有效?