我为动态视频编写代码,即存储在文档中,并在我们的程序中使用该视频
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ipc.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:ipc.sourceType];
ipc.delegate = self;
ipc.editing = NO;
[self presentModalViewController:ipc animated:YES];
答案 0 :(得分:-1)
以下代码可以帮助您,
#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)imagePickerControl didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *videoURL;
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
if ([type isEqualToString:(NSString *)kUTTypeVideo] ||
[type isEqualToString:(NSString *)kUTTypeMovie])
{
videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
}
[self saveVideoToFileFromURL:videoURL];
[self dismissModalViewControllerAnimated:YES];
}
-(void)saveVideoToFileFromURL:(NSURL*)videoStorageURL
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSDate *todayDateObj = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"ddMMyyyyHHmmss"];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"movie%@.mov",[dateFormat stringFromDate:todayDateObj]] ];
[dateFormat release];
NSData *data = [NSData dataWithContentsOfURL:videoStorageURL];
[data writeToFile:filePath atomically:YES];
}
答案 1 :(得分:-1)
以下代码方法插入AppDelegate.h文件
@property (nonatomic , retain) MPMoviePlayerViewController *mPlayer;
-(void)initAndPlayMovie:(NSURL *)movieURL andViewController:(UIViewController*)vCtr;
将代码插入AppDelegate .m文件
-(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
合成变量
@synthesize rqstForAudio=_rqstForAudio;
创建方法名称为playAudio,连接按钮按下事件。
#pragma mark - View lifecycle
- (IBAction)playAudio:(UIButton*)sender
{
NSString *strAudioURL=@"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];
}
}
}