……………………………………………………………………………
第1記 系統音頻、音、視訊播放器
……………………………………………………………………………
Login登陸;Register注冊。
一、圖檔
1、為圖檔視圖添加一組圖檔
NSMutableArray * imagesArr = [[NSMutableArray alloc]init];
for(int i = 0;i<25;i++)
{
擷取圖檔的路徑
NSString * path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"cat_angry00%.2d",i + 1] ofType:@"jpg"];
UIImage * image = [UIImage imageWithContentsOfFile:path];
[imagesArr addObject:image];
}
2、為imageView視圖添加動畫效果
[imageView setAnimationImages:imagesArr];
[imageView setAnimationDuration:3];
[imageView setAnimationRepeatCount:1];
[imageView startAnimating];
}
二、系統音頻
使用系統音頻的時候要導入這個系統庫:AudioToolbox.framework
系統音頻的特點:系統音頻小于30s,急速、不能暫停,不能循環播放、不能播放立體音、不能播放混音。
<1>擷取播放的系統音頻的路徑
NSString * path = [[NSBundle mainBundle] pathForResource:@"angry" ofType:@"m4a"];
<2>将字元串路徑轉化成NSURL
NSURL * url = [NSURL fileURLWithPath:path];
将本地路徑轉化成NSURL 使用的方法是fileURLWithPath:
将網絡路徑轉化成NSURL 使用的方法是URLWithString:
<3>播放本地音頻 需要向系統注冊一個系統音頻ID
SystemSoundID SID;
<4>建立系統音頻
系統音頻 調用的函數都是C語言函數 而且函數的函數名都是以AudioServices開頭
CFURLRef類型和NSURL類型原理相同 隻是表示的方式不同而已
AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &SID);
<5>播放系統音頻
AudioServicesPlaySystemSound(SID);
<6>系統音頻播放結束
AudioServicesAddSystemSoundCompletion(SID, nil, nil, finish, nil);
三、簡單的音樂播放器
使用長音頻(歌曲)的時候要導入這個系統庫:AVFoundation.framework
傳值方法:代理、通知、KVC、block文法
四、視訊播放器(完整)
視訊播放導入這個系統庫:MediaPlayer.framework
#import <MediaPlayer/MediaPlayer.h>
@interface XLViewController ()
{
//建立視訊播放器對象
MPMoviePlayerViewController * moviePlayer;
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100, 100);
[btn setTitle:@"播放" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)pressBtn:(id)sender
{
//<1>擷取視訊資源的路徑
NSString * path = [[NSBundle mainBundle] pathForResource:@"dzs" ofType:@"mp4"];
//<2>将字元串路徑轉化成NSURL
NSURL * url = [NSURL fileURLWithPath:path];
//<3>建立視訊播放器對象
moviePlayer = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
//<4>跳轉到視圖播放器的視圖
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
//<5>視訊播放器沒有協定 所有視訊播放器的所有方法必須借助通知完成
//通知的名稱是特殊的字元串 系統提供好的
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finishPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
-(void)finishPlaying:(NSNotification *)notif
{
[self dismissMoviePlayerViewControllerAnimated];
}