.h
#import <UIKit/UIKit.h> #import <MediaPlayer/MediaPlayer.h> @interface ViewController : UIViewController @property (nonatomic,strong) MPMoviePlayerController *player; - (IBAction)SelfVideo; - (IBAction)RecordVideo; - (IBAction)PlayVideo; @end
.m
// // ViewController.m // SysAssetVideourlToTempFile // // Created by jstv.com jstv on 12-6-19. // Copyright (c) 2012年 jstv.com. All rights reserved. // #import "ViewController.h" #import <AssetsLibrary/AssetsLibrary.h> @interface ViewController () @end @implementation ViewController @synthesize player; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } +(NSString*) videoAssetURLToTempFile:(NSURL*)url { NSString * surl = [url absoluteString]; NSString * ext = [surl substringFromIndex:[surl rangeOfString:@"ext="].location + 4]; NSTimeInterval ti = [[NSDate date]timeIntervalSinceReferenceDate]; NSString * filename = [NSString stringWithFormat: @"%f.%@",ti,ext]; NSString * tmpfile = [NSTemporaryDirectory() stringByAppendingPathComponent:filename]; ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) { ALAssetRepresentation * rep = [myasset defaultRepresentation]; NSUInteger size = [rep size]; const int bufferSize = 8192; NSLog(@"Writing to %@",tmpfile); FILE* f = fopen([tmpfile cStringUsingEncoding:1], "wb+"); if (f == NULL) { NSLog(@"Can not create tmp file."); return; } Byte * buffer = (Byte*)malloc(bufferSize); int read = 0, offset = 0, written = 0; NSError* err; if (size != 0) { do { read = [rep getBytes:buffer fromOffset:offset length:bufferSize error:&err]; written = fwrite(buffer, sizeof(char), read, f); offset += read; } while (read != 0); } fclose(f); }; ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror) { NSLog(@"Can not get asset - %@",[myerror localizedDescription]); }; if(url) { ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease]; [assetslibrary assetForURL:url resultBlock:resultblock failureBlock:failureblock]; } return tmpfile; } - (IBAction)SelfVideo { //获取系统视频 } - (IBAction)RecordVideo { //调用录制视频接口录制视频并保存 } -(void)finishedPlaying:(NSNotification *)paramNotification { NSNumber *reason = [paramNotification.userInfo valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]; if(reason !=nil) { NSInteger reasonAsInteger = [reason integerValue]; switch (reasonAsInteger) { case MPMovieFinishReasonPlaybackEnded: { NSLog(@"正常播放完成"); break; } case MPMovieFinishReasonPlaybackError: { NSLog(@"错误引起播放结束"); break; } case MPMovieFinishReasonUserExited: { NSLog(@"用户退出"); } default: break; } NSLog(@"finished reason=%ld",(long)reasonAsInteger); } } - (IBAction)PlayVideo { //播放视频 NSLog(@"播放"); NSURL *url = [NSURL URLWithString:@"http://202.102.79.115/webmedia//2012/05/29/mingshigt/mingshigt_20120529_937346_3.mp4"]; self.player = [[MPMoviePlayerController alloc] initWithContentURL:url]; if(self.player !=nil) { [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(finishedPlaying) name:MPMoviePlayerPlaybackDidFinishNotification object:self.player]; self.player.scalingMode = MPMovieScalingModeAspectFit; [self.player play]; [self.view addSubview:self.player.view]; [self.player setFullscreen:YES animated:YES]; } } @end