この方法は、AppDelegateの.mファイルにコードを挿入後AppDelegate.hファイル
@property (nonatomic , retain) MPMoviePlayerViewController *mPlayer;
-(void)initAndPlayMovie:(NSURL *)movieURL andViewController:(UIViewController*)vCtr;
に挿入
プラグママーク - メディアプレーヤー
-(void)initAndPlayMovie:(NSURL *)movieURL andViewController:(UIViewController*)vCtr
{
self.mPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
// set source type streaming
[self.mPlayer.moviePlayer setMovieSourceType:MPMovieSourceTypeUnknown];
[self.mPlayer.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
// fit to screen mode
[self.mPlayer.moviePlayer setScalingMode:MPMovieScalingModeAspectFit];
// full screen mode
[self.mPlayer.moviePlayer setFullscreen:YES animated:YES];
[vCtr presentMoviePlayerViewControllerAnimated:self.mPlayer];
}
-(void)stopPlaying_dismissMoviePlayer
{
[self.mPlayer.moviePlayer stop];
[self.mPlayer dismissMoviePlayerViewControllerAnimated];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[self stopPlaying_dismissMoviePlayer];
}
.hファイルにコードを挿入後
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#import "ASIHTTPRequest.h"
#import "ASIDownloadCache.h"
@interface locallyCacheMP3FileViewController : UIViewController
- (IBAction)playAudio:(UIButton*)sender;
@property (nonatomic, retain) ASIHTTPRequest *rqstForAudio;
@end
は、ボタンのプレスイベントに接続playAudioとして変数
@synthesize rqstForAudio=_rqstForAudio;
Createメソッド名を合成。
#pragma mark - View lifecycle
- (IBAction)playAudio:(UIButton*)sender
{
NSString *[email protected]"Your Video link";
// check first locally exists or not
NSString *strPathToAudioCache=[NSString stringWithFormat:@"%@/%@",
[(NSArray*)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0],
AudioFolder];
NSDictionary *dOfAudios=[NSDictionary dictionaryWithContentsOfFile:strPathToAudioCache];
if([dOfAudios valueForKey:strAudioURL]) {
[APP_DEL initAndPlayMovie:[NSURL fileURLWithPath:[dOfAudios valueForKey:strAudioURL]] andViewController:self];
} else {
NSURL *audioURL = [NSURL URLWithString:strAudioURL];
NSString *strPathToDownload=[NSString stringWithFormat:@"%@/%@",[(NSArray*)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0],
[strAudioURL lastPathComponent]];
if(!self.rqstForAudio || [self.rqstForAudio isFinished]) {
self.rqstForAudio=[ASIHTTPRequest requestWithURL:audioURL];
[self.rqstForAudio setDelegate:self];
[self.rqstForAudio setAllowResumeForFileDownloads:YES];
[self.rqstForAudio setCachePolicy:ASIUseDefaultCachePolicy];
[self.rqstForAudio setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[self.rqstForAudio setDidFailSelector:@selector(failedToLoad:)];
[self.rqstForAudio setDidFinishSelector:@selector(finishedLoading:)];
[self.rqstForAudio setDownloadCache:[ASIDownloadCache sharedCache]];
[self.rqstForAudio setDownloadDestinationPath:strPathToDownload];
[self.rqstForAudio startAsynchronous];
}
}
}
You can download the Source code and tutorial here