0

Upload và play file mp3 với Dropbox SDK

Trong bài hướng dẫn này, các bạn cần chuẩn bị 1 project đã cài đặt Dropbox SDK và đã implement chức năng connect đến tài khoản Dropbox.

1. Get danh sách nhạc

Đầu tiên, bạn cần kéo các file nhạc mp3 vào project

Screen Shot 2016-11-09 at 9.27.35 PM.png

Khi đã thêm các bài nhạc vào project, khi project được build, các bài nhạc này sẽ có trong app của bạn trên device. Bạn cần gọi hàm để lấy chúng ra từ device local.

musicPaths = [NSMutableArray new];

    NSString * resourcePath = [[NSBundle mainBundle] resourcePath];
    NSError * error;
    NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:resourcePath error:&error];

    for (NSString *item in directoryContents){
        if ([[item pathExtension] isEqualToString:@"mp3"]) {

            SongUploadObj *obj = [SongUploadObj new];
            obj.filename = item;
            obj.path = [NSString stringWithFormat:@"%@/%@",resourcePath,item];
            [musicPaths addObject:obj];
        }
    }

Mỗi object được thêm vào list sẽ chứa tênđường dẫn của mp3 file.

2. Upload mp3 file

DBRestClient là cổng kết nối đến Dropbox khi bạn đã link đến tài khoản Dropbox

Your View Controller

#import <DropboxSDK/DropboxSDK.h>

@interface YourViewController () <DBRestClientDelegate>
@property (nonatomic, strong) DBRestClient *restClient;
@end
...

- (void)viewDidLoad {
    [super viewDidLoad];

    self.restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
    self.restClient.delegate = self;
}

Sau khi tạo xong DBRestClient object, bạn đã có thể tạo request. Ở đây chúng ta sẽ tạo 1 request upload:

[DBRestClient uploadFile:filename toPath:destDir withParentRev:nil fromPath:localPath]
  • filename là tên file của bạn sẽ hiển thị trên Dropbox.

  • toPath là đường dẫn folder để đặt file trên Dropbox. (VD: "/Public" là folder Public trên Dropbox)

  • parentRev dùng khi cần overwrite file có sẵn. Nếu bạn upload 1 file mới thì set là nil.

  • fromPath đường dẫn của file trong device của bạn.

Tất cả hàm DBRestClientasynchronous. Nghĩa là nó sẽ không trả về data ngay lập tức mà phải có thời gian để thực hiện. Mỗi hàm có thể có 2-3 hàm DBRestClientDelegate liên quan sẽ được gọi.

Dưới đây là những delegate được gọi khi đang upload, success hoặc fail. Success sẽ trả về data trên Dropbox, progress là hiển thị tiến độ upload, còn fail sẽ trả về NSError chứa chi tiết vì sao request fail.

- (void)restClient:(DBRestClient *)client uploadedFile:(NSString *)destPath
    from:(NSString *)srcPath metadata:(DBMetadata *)metadata {
    NSLog(@"File uploaded successfully to path: %@", metadata.path);
}

- (void)restClient:(DBRestClient*)client uploadProgress:(CGFloat)progress
           forFile:(NSString*)destPath from:(NSString*)srcPath {
    NSLog(@"Progress: %0.2f", progress);
}

- (void)restClient:(DBRestClient *)client uploadFileFailedWithError:(NSError *)error {
    NSLog(@"File upload failed with error: %@", error);
}

Chú ý:

  • Bạn phải gọi hàm của DBRestClient từ Main thread. Nếu không delegate sẽ không được gọi.
  • Thư mục root trên Dropbox là "/". Nếu toPath bạn thêm vào là "/a/b/", Dropbox sẽ tự động tạo thêm các thư mục a rồi đến thư mục b. Do đó file được upload sẽ có đường dẫn là "/a/b/file.mp3"
  • Nếu file upload bị trùng tên, Dropbox sẽ tự động thêm số vào tên các file upload sau đó.

Screen Shot 2016-11-24 at 10.02.14 AM.png

3. Play mp3 file được upload

Trước hết, chúng ta cần lấy URL streaming của file vừa được upload. DBRestClient có hỗ trợ hàm sau:

- (void)loadStreamableURLForFile:(NSString *)path;

Ở phần 2, khi upload mp3 file success, delegate success trả ra có object DBMetadata. Nó chứa các thông tin của files và folders trên Dropbox. Khi gọi hàm lấy url stream, bạn truyền đường dẫn file trên Dropbox chứa trong DBMetadata.

- (void)restClient:(DBRestClient *)client uploadedFile:(NSString *)destPath
    from:(NSString *)srcPath metadata:(DBMetadata *)metadata {
    [self.restClient loadStreamableURLForFile:metadata.path];
}

Tương tự như khi upload, khi get URL streaming cũng sẽ có các delegate tương ứng của nó cho success và fail.

- (void)restClient:(DBRestClient*)restClient loadedStreamableURL:(NSURL*)url forFile:(NSString*)path {

}
- (void)restClient:(DBRestClient*)restClient loadStreamableURLFailedWithError:(NSError*)error {

}

Delegate success sẽ trả về URL stream của file mp3 trên Dropbox, bạn chỉ việc tạo ra Player và truyền URL lấy được vào. Player của hệ thống sẽ tự động play bài nhạc bạn vừa upload.

- (void)restClient:(DBRestClient*)restClient loadedStreamableURL:(NSURL*)url forFile:(NSString*)path {

    MPMoviePlayerViewController* vc = [[MPMoviePlayerViewController alloc] init];
    vc.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
    vc.moviePlayer.shouldAutoplay = YES;
    vc.moviePlayer.contentURL = url;
    [self presentMoviePlayerViewControllerAnimated:vc];
    [vc.moviePlayer play];
}

All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí