音頻與視頻的簡單用法:
使用音頻的時候需要導入#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];
}