音频与视频的简单用法:
使用音频的时候需要导入#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
{
AVPlayer *player;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
注意:AVPlayer需要定义全局变量,如果不定义为全局变量的话,出了括号,它就被释放掉了
NSURL *url = [NSURL URLWithString:@"http://bcs.duapp.com/chenwei520/media/music.mp3"];
player = [[AVPlayer alloc] initWithURL:url];
[player play]; 开始播放
[player pause]; 暂停播放
}
注意:MPMoviePlayerController也需要定义全局变量,如果不定义为全局变量的话,出了括号,它就被释放掉了
@interface ViewController ()
{
MPMoviePlayerController *moviePlayer;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *mp4 = @"http://bcs.duapp.com/chenwei520/media/mobile_vedio.mp4";
NSURL *url = [NSURL URLWithString:mp4];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.view.frame = self.view.bounds;
moviePlayer.view.backgroundColor = [UIColor grayColor];
[moviePlayer play];
[self.view addSubview:moviePlayer.view];
}