您好我正在申請其中,我必須上傳一個聲音文件到Facebook。我怎樣才能分享一個聲音文件,在Facebook或Twitter的iphone
請給我一個更好的解決方案,是否可以在Facebook上共享聲音文件。
在此先感謝
您好我正在申請其中,我必須上傳一個聲音文件到Facebook。我怎樣才能分享一個聲音文件,在Facebook或Twitter的iphone
請給我一個更好的解決方案,是否可以在Facebook上共享聲音文件。
在此先感謝
Facebook沒有聲音上傳。您可以隨時在其他地方上傳聲音文件,並使用Facebook分享鏈接。
如果您檢查webapps的twitter/facebook,他們不提供任何方式來上傳音頻文件。
Twittier只允許文本發佈,另一方面Facebook允許上傳圖片/視頻。
鑑於這些事實,我認爲沒有url分享是不可能的。
無法將音頻文件上傳到Facebook,只允許照片和視頻。但是,另一種解決方案是將音頻文件上傳到別處,然後使用Facebook API使用該引用發佈鏈接。您可能希望上傳音頻的一個地方是http://developers.soundcloud.com/
使用AVAssetExportSession,用聲音文件創建電影,然後將其上傳到Facebook。
這是可能的,但它有點痛苦。爲此,您必須將音頻文件轉換爲視頻文件,然後將其作爲視頻發佈到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的應用程序和然後允許用戶使用存儲在您應用中的視頻背景在牆上分享他們的音頻文件。
顯然,每個人的項目都不同,這意味着您可能無法將此代碼完全複製到您的項目中。我試圖分解這個過程,這意味着應該很容易推斷成功地上傳音頻消息。
看看http://developers.soundcloud。com/blog/ios-sharing – Mat 2012-02-28 16:56:54
@Mat,看起來很有希望,你應該做出答案而不是評論。 – picciano 2012-02-28 17:01:49