2012-02-28 77 views

回答

0

Facebook没有声音上传。您可以随时在其他地方上传声音文件,并使用Facebook分享链接。

0

如果您检查webapps的twitter/facebook,他们不提供任何方式来上传音频文件。

Twittier只允许文本发布,另一方面Facebook允许上传图片/视频。

鉴于这些事实,我认为没有url分享是不可能的。

0

无法将音频文件上传到Facebook,只允许照片和视频。但是,另一种解决方案是将音频文件上传到别处,然后使用Facebook API使用该引用发布链接。您可能希望上传音频的一个地方是http://developers.soundcloud.com/

0

使用AVAssetExportSession,用声音文件创建电影,然后将其上传到Facebook。

0

这是可能的,但它有点痛苦。为此,您必须将音频文件转换为视频文件,然后将其作为视频发布到Facebook。

首先,我们需要访问我们的audioFile,你应该已经有了这个,如果没有的话,这里有很多关于这个问题的Stackoverflow问题,我不会因为脱轨而使问题复杂化。然后,我们在文档中为视频创建一个NSURL。在这种情况下,我们有一个名为video_base.mp4的视频,该视频被设计为我们音轨的不错背景。最后,我们在将返回的文件分享到Facebook之前合并这些文件。

- (IBAction)shareToFacebook:(id)sender { 

    // You should already have your audio file saved 
    NSString * songFileName = [self getSongFileName]; 

    NSArray * searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString * documentPath = [searchPaths objectAtIndex:0]; 

    NSString * file = [documentPath stringByAppendingPathComponent:songFileName]; 
    NSURL * audioFileURL = [NSURL fileURLWithPath: audioFile]; 
    NSURL * videoFileURL = [NSURL fileURLWithPath:[NSFileManager getFilePath:@"video_base.mp4" withFolder:@""]]; 

    [self mergeAudio:audioFileURL andVideo:videoFileURL withSuccess:^(NSURL * url) { 

     // Now we have the URL of the video file 
     [self shareVideoToFacebook:url]; 
    }]; 
} 

感谢@dineshprasanna为可发现here的这部分代码。我们想要合并我们的音频和视频,然后将它们保存到路径中。然后我们返回完成块中的exportURL。

- (void)mergeAudio: (NSURL *)audioURL andVideo: (NSURL *)videoURL withSuccess:(void (^)(NSURL * url))successBlock { 

    AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audioURL options:nil]; 
    AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoURL options:nil]; 

    AVMutableComposition * mixComposition = [AVMutableComposition composition]; 

    AVMutableCompositionTrack * compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio 
                        preferredTrackID:kCMPersistentTrackID_Invalid]; 
    [compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) 
            ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] 
            atTime:kCMTimeZero error:nil]; 

    AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo 
                       preferredTrackID:kCMPersistentTrackID_Invalid]; 
    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) 
           ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] 
           atTime:kCMTimeZero error:nil]; 

    AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition 
                     presetName:AVAssetExportPresetHighestQuality]; 

    NSString * videoName = @"export.mov"; 

    NSString * exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName]; 
    NSURL * exportUrl = [NSURL fileURLWithPath:exportPath]; 

    if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) { 
     [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil]; 
    } 

    _assetExport.outputFileType = @"com.apple.quicktime-movie"; 
    _assetExport.outputURL = exportUrl; 
    _assetExport.shouldOptimizeForNetworkUse = YES; 

    [_assetExport exportAsynchronouslyWithCompletionHandler: ^(void) { 
     if(successBlock) successBlock(exportUrl); 
    }]; 
} 

最后,我们希望将我们的返回videoURL保存到Facebook。值得一提的是,我们需要添加一些库此功能工作:

#import <AssetsLibrary/AssetsLibrary.h> 
#import <FBSDKCoreKit/FBSDKCoreKit.h> 
#import <FBSDKShareKit/FBSDKShareKit.h> 

然后我们分享合并后的文件给Facebook:

- (void)shareVideoToFacebook: (NSURL *)videoURL { 

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
    ALAssetsLibraryWriteVideoCompletionBlock videoWriteCompletionBlock = ^(NSURL *newURL, NSError *error) { 
     if(error) { 
      NSLog(@"Error writing image with metadata to Photo Library: %@", error); 
     } else { 
      NSLog(@"Wrote image with metadata to Photo Library %@", newURL.absoluteString); 

      FBSDKShareDialog *shareDialog = [[FBSDKShareDialog alloc]init]; 
      NSURL *videoURL = newURL; 

      FBSDKShareVideo *video = [[FBSDKShareVideo alloc] init]; 
      video.videoURL = videoURL; 

      FBSDKShareVideoContent *content = [[FBSDKShareVideoContent alloc] init]; 
      content.video = video; 

      [FBSDKShareDialog showFromViewController:self 
            withContent:content 
             delegate:nil]; 
     } 
    }; 

    if([library videoAtPathIsCompatibleWithSavedPhotosAlbum:videoURL]) { 
     [library writeVideoAtPathToSavedPhotosAlbum:videoURL 
           completionBlock:videoWriteCompletionBlock]; 
    } 
} 

这应该打开Facebook的应用程序和然后允许用户使用存储在您应用中的视频背景在墙上分享他们的音频文件。

显然,每个人的项目都不同,这意味着您可能无法将此代码完全复制到您的项目中。我试图分解这个过程,这意味着应该很容易推断成功地上传音频消息。

相关问题