新建工程,添加音视频框架
编辑控制器的.h文件如下:
<span style="font-size:18px;">// // ViewController.h // 音视频预习 // // Created by apple on 15/9/4. // Copyright (c) 2015年 LiuXun. All rights reserved. // #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> #import <MediaPlayer/MediaPlayer.h> @interface ViewController : UIViewController @property (nonatomic, strong) AVAudioPlayer * player; @end </span>
<span style="font-size:18px;">// // ViewController.m // 音视频预习 // // Created by apple on 15/9/4. // Copyright (c) 2015年 LiuXun. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(20, 200, 213, 40); [btn setTitle:@"播放MV" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } -(void) play { // 播放本地文件视频 // NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"m4v"]; // NSURL *url = [[NSURL alloc] initFileURLWithPath:path]; // MPMoviePlayerViewController *playVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; // [self presentMoviePlayerViewControllerAnimated:playVC ]; // // playVC.view.frame = CGRectMake(100, 100, 200, 300); // [self.view addSubview:playVC.view]; // 播放网路视频文件 // NSURL *url= [NSURL URLWithString:@"http://10.50.129.30/mp4files/52070000013B61EC/www.dcjyxwzx.cn/data//news/video//2015/06/20150609134637_60491.mp4"]; // MPMoviePlayerViewController *playVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; // playVC.view.frame = CGRectMake(0, 100, 320, 200); // [playVC.moviePlayer setControlStyle:MPMovieControlStyleEmbedded]; // playVC.moviePlayer.scalingMode = MPMovieScalingModeFill; // [self.view addSubview:playVC.view]; // // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeFull) name:MPMoviePlayerWillEnterFullscreenNotification object:nil]; // [self presentMoviePlayerViewControllerAnimated:playVC]; //播放音频文件 NSString *path = [[NSBundle mainBundle] pathForResource:@"一江水" ofType:@"mp3"]; if (path) { NSLog(@"LLLL"); NSURL *url = [[NSURL alloc] initFileURLWithPath:path]; self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; [self.player prepareToPlay]; self.player.volume = 1.0f; self.player.numberOfLoops = 0; if (self.player) { if (![self.player isPlaying]) { [self.player play]; } } } } -(void) changeFull { NSLog(@"+++++++++"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end </span>
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playDidChangeNotification:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil]; } return self; } - (void)playDidChangeNotification:(NSNotification *)notification { MPMoviePlayerController *moviePlayer = notification.object; MPMoviePlaybackState playState = moviePlayer.playbackState; if (playState == MPMoviePlaybackStateStopped) { NSLog(@"停止"); } else if(playState == MPMoviePlaybackStatePlaying) { NSLog(@"播放"); } else if(playState == MPMoviePlaybackStatePaused) { NSLog(@"暂停"); } }
控制横竖屏的方法如下:
在控制器中添加如下方法
- (BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft; } //6.0之前用于控制横、竖 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) { return YES; } return NO; }