Skip to content

Commit

Permalink
When playing audio from remote URL, stop as soon as download fails an…
Browse files Browse the repository at this point in the history
…d make loading cacheable.

Load remote sound files via NSURLConnection's
sendSynchronousRequest:returningResponse:error:
class method so that they get cached by
NSURLCache.
  • Loading branch information
buddydvd committed Nov 7, 2011
1 parent d5962f1 commit f405f8c
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions PhoneGapLib/Classes/Sound.m
Expand Up @@ -228,8 +228,14 @@ - (BOOL) prepareToPlay: (PGAudioFile*) audioFile withId: (NSString*) mediaId
if ([resourceURL isFileURL]) {
audioFile.player = [[[ AudioPlayer alloc ] initWithContentsOfURL:resourceURL error:&playerError] autorelease];
} else {
NSData* data = [NSData dataWithContentsOfURL:resourceURL];
audioFile.player = [[[ AudioPlayer alloc ] initWithData:data error:&playerError] autorelease];
NSURLRequest *request = [NSURLRequest requestWithURL:resourceURL];
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&playerError];
if (playerError) {
NSLog(@"Unable to download audio from: %@", [resourceURL absoluteString]);
} else {
audioFile.player = [[[ AudioPlayer alloc ] initWithData:data error:&playerError] autorelease];
}
}
}
if (playerError != nil) {
Expand Down

0 comments on commit f405f8c

Please sign in to comment.