2

サーバーからオーディオ ファイル (mp3) を再生する iPhone アプリケーションがあります。ダウンロード ボタンを追加して、ユーザーがこれらのファイルをアプリにダウンロードできるようにしたいと考えています。そのため、これらのファイルを聞くたびにインターネットに接続する必要はありません。そのためのサンプルコードはありますか?

私はウェブ上で多くのことを見ましたが、私はまだ混乱しています. ここに私が使用しているコードがあります:

.h ファイル内:

#import 
#import "MBProgressHUD.h"

#import 
#import 

@interface myViewController : UIViewController {
MBProgressHUD *HUD;

long long expectedLength;
long long currentLength;

AVAudioPlayer *theAudio;

}

-(NSString *)pathOfFile;
-(void)applicationWillTerminate:(NSNotification*)notification;

-(IBAction)play:(id)sender;
-(IBAction)download(id)sender;
@property (nonatomic,retain) AVAudioPlayer *theAudio;

.m ファイル内:

- (IBAction)download:(id)sender {
NSURL *URL = [NSURL URLWithString:@"http://www.server.com/myfile.mp3"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request      delegate:self];
[connection start];
[connection release];

HUD = [[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES] retain];

HUD.labelText = @"Loading...";
}

-(IBAction)play:(id)sender { 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *file = [NSString stringWithFormat:@"%@/song.mp3", documentsDirectory];

theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:file]   error:nil];

[theAudio play];

}

-(NSString *)pathOfFile {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

return [documentsDirectory stringByAppendingFormat:@"myfile.plist"];

}

-(void)applicationWillTerminate:(NSNotification*)notification {

NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:theAudio];
[array writeToFile:[self pathOfFile] atomically:YES];

[array release];

}

何が問題なのか、何が欠けているのかわかりません。詳細を教えてください。前もって感謝します。

4

2 に答える 2

2

ダウンロードには ASIHTTP を使用します。非同期で信頼性が高くなります。http://allseeing-i.com/ASIHTTPRequest/ 音声・動画をダウンロードする方法です。パラメータを渡してダウンロードを開始しますか?

パラメータ: (1) File Path 、保存するドキュメント ディレクトリ パス (2) Trimmed String 、講義の URL

+ (void)downloadingLecture:(NSString *)filePath withLectureUrl:(NSString *)trimmedString 
 {
    AppDelegate *_appDel=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    [_appDel.queue setDelegate:self];
    [_appDel.queue setMaxConcurrentOperationCount:10];
    [_appDel.queue setShouldCancelAllRequestsOnFailure:NO];
    [_appDel.queue setShowAccurateProgress:YES];

    ASIHTTPRequest *request;
    request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:trimmedString]];
    [request setDelegate:self];
    [request setDownloadDestinationPath:filePath];
    [request setShowAccurateProgress:YES];
    [request setShouldContinueWhenAppEntersBackground:YES];
    [request setUsername:downloadedSuccessfullString];
    [request setDidFinishSelector:@selector(downloadedSuccessfully:)];
    [request setDidFailSelector:@selector(downloadedFailure:)];

    [_appDel.queue addOperation:request];
    [_appDel.queue go];
}

これがあなたを助けることを願っています。それでも問題が解決しない場合は、私にメールしてください。私は 2 つのアプリケーションに実装したので、必ず解決します。

于 2012-11-10T11:58:41.770 に答える
0

以下のコードを試してください:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
[request setDownloadDestinationPath:destinationPath];   
[request setTemporaryFileDownloadPath:[NSString stringWithFormat:@"%@-part",destinationPath]];
[request setDelegate:self];
[request setAllowResumeForFileDownloads:YES];
[request startAsynchronous];

ダウンロードを確認するには:

- (void)requestStarted:(ASIHTTPRequest *)request;
- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders;
- (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL;
- (void)requestFinished:(ASIHTTPRequest *)request;
- (void)requestFailed:(ASIHTTPRequest *)request; 

上記のコードは、ダウンロードの進行中に宛先フォルダーにファイル「your_file.mp3-part」を作成し、ダウンロードが完了すると「your_file.mp3」を作成します。

それが役に立てば幸い。不明な点があればお知らせください。

于 2012-11-19T12:12:42.247 に答える