AVFoundation

1、AVAudioPlayer
2、AVPlayer
3、系統(tǒng)音頻使用
4、錄音
5、視頻播放
6、圖層

  • 音樂(lè)播放,都可以實(shí)現(xiàn)本地和網(wǎng)上播放
    • AVAudioPlayer
    • AVPlayer

AVAudioPlayer

// 注意這里的player一定要有強(qiáng)引用不然會(huì)被銷毀
    
    // AVAudioPlayer播放本地音樂(lè)
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"咱們屯里的人" ofType:@"mp3"];
    
    NSURL *url = [NSURL fileURLWithPath:filePath];
    
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];

    // AVAudioPlayer播放網(wǎng)絡(luò)音樂(lè)
    NSURL *url = [NSURL URLWithString:@"http://mp3.qqmusic.cc/yq/107771414.mp3"];
    
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    self.player = [[AVAudioPlayer alloc] initWithData:data error:nil];
    
    [self.player play];

AVPlayer

// AVlayer播放本地音樂(lè)
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"咱們屯里的人" ofType:@"mp3"];
    
    NSURL *url = [NSURL fileURLWithPath:filePath];
    
    self.player = [AVPlayer playerWithURL:url];
    
    NSURL *url = [NSURL URLWithString:@"http://mp3.qqmusic.cc/yq/107771414.mp3"];

    self.player = [AVPlayer playerWithURL:url];
    
    [self.player play];

系統(tǒng)音頻使用

    //本地url
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"44th Street Medium" ofType:@"caf"];
    //將該路徑下的文件轉(zhuǎn)成url格式
    NSURL *fileUrl = [NSURL fileURLWithPath:filePath];

    
    /*
     CFURLRef: Core Foundation
     __bridge:橋接  Foundation  <---> Core Foundation 
     解決兩個(gè)框架中對(duì)象轉(zhuǎn)換時(shí) 內(nèi)存管理的問(wèn)題
     //橋接:讓ARC管理內(nèi)存
     
     //手動(dòng)管理內(nèi)存: 
     CFRelease(fileUrl)
     CFRetain(<#CFTypeRef cf#>)

     
     */
    
    //(1) 注冊(cè)系統(tǒng)聲音 (C函數(shù)的調(diào)用)
    //soundID:標(biāo)示音頻
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileUrl, &soundID);
    
    //(2)播放
    AudioServicesPlaySystemSound(soundID);
    
    //(3)振動(dòng)
//    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    
    //(4)銷毀 (在dealloc中)

-(void)dealloc{

    /*
     ARC中C語(yǔ)言的函數(shù)需要自己管理內(nèi)存,所以在這里,要移除或者銷毀注冊(cè)的系統(tǒng)聲音.
     
     */
    //移除注冊(cè)的系統(tǒng)聲音
//    AudioServicesRemoveSystemSoundCompletion(soundID);
    
    //銷毀注冊(cè)的系統(tǒng)聲音(確定不再使用該id的音頻)
    AudioServicesDisposeSystemSoundID(soundID);
    
}
  • 錄音
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
{

    AVAudioRecorder *recorder;//錄音對(duì)象
    AVAudioPlayer *player;//播放對(duì)象
    NSURL *fileUrl;//文件路徑
    
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
//按下錄音
- (IBAction)recoder:(id)sender {
    //(1)url
    NSString *urlStr = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/recoder.aac"];
    
    //刪除之前的錄音
    [[NSFileManager defaultManager]removeItemAtPath:urlStr error:nil];
    
    
    fileUrl = [NSURL fileURLWithPath:urlStr];
    
    
    
    //(2)設(shè)置錄音的音頻參數(shù)
    /*
     1 ID號(hào):acc
     2 采樣率(HZ):每秒從連續(xù)的信號(hào)中提取并組成離散信號(hào)的采樣個(gè)數(shù)
     3 通道的個(gè)數(shù):(1 單聲道 2 立體聲)
     4 采樣位數(shù)(8 16 24 32) 衡量聲音波動(dòng)變化的參數(shù)
     5 大端或者小端 (內(nèi)存的組織方式)
     6 采集信號(hào)是整數(shù)還是浮點(diǎn)數(shù)
     7 音頻編碼質(zhì)量
     
     
     */
    NSDictionary *info = @{
                           AVFormatIDKey:[NSNumber numberWithInt:kAudioFormatMPEG4AAC],//音頻格式
                           AVSampleRateKey:@1000,//采樣率
                           AVNumberOfChannelsKey:@2,//聲道數(shù)
                           AVLinearPCMBitDepthKey:@8,//采樣位數(shù)
                           AVLinearPCMIsBigEndianKey:@NO,
                           AVLinearPCMIsFloatKey:@NO,
                           AVEncoderAudioQualityKey:[NSNumber numberWithInt:AVAudioQualityMedium],
            
                           };
    
    
    /*
     url:錄音文件保存的路徑
     settings: 錄音的設(shè)置
     error:錯(cuò)誤
     */
    recorder = [[AVAudioRecorder alloc]initWithURL:fileUrl settings:info error:nil];
    
    [recorder prepareToRecord];
    [recorder record];
    
    
    
}
//抬手結(jié)束錄音
- (IBAction)stopRecoder:(id)sender {
    [recorder stop];
    
    //手動(dòng)釋放
    recorder =nil;
    
    
}
//播放錄音
- (IBAction)playRecoder:(id)sender {
    
    //多次播放的時(shí)候
    player = nil;
    
    player = [[AVAudioPlayer alloc]initWithContentsOfURL:fileUrl error:nil];
    
    if (player) {
        
        [player prepareToPlay];
        [player play];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

  • 視頻播放
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
{

    MPMoviePlayerController *moviePlayerC; //NSobject
    
    MPMoviePlayerViewController *MPVC; //UIViewController
    
    
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)MPMoviePlayerController:(id)sender {
    
    //通知的監(jiān)聽
    //視頻播放狀態(tài)改變的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playStateDidChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
    
    //視頻播放完成的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playStateDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    
    
    
    //本地視頻
    //ios中視頻格式是MP4. 電腦的流媒體格式:flv,在手機(jī)上自動(dòng)轉(zhuǎn)成MP4
    NSString *path = [[NSBundle mainBundle]pathForResource:@"video" ofType:@"mp4"];
    NSURL *url1 = [NSURL fileURLWithPath:path];
    
    //網(wǎng)絡(luò)視頻
//    NSURL *url2 = [NSURL URLWithString:@"http://vf1.mtime.cn/Video/2012/04/23/mp4/120423212602431929.mp4"];
    
    //
    moviePlayerC = [[MPMoviePlayerController alloc]initWithContentURL:url1];
    moviePlayerC.view.frame = CGRectMake(0, 300, 300, 150);
    
    [self.view addSubview:moviePlayerC.view];
    
    //設(shè)置播放器控制按鈕的樣式
    moviePlayerC.controlStyle = MPMovieControlStyleEmbedded;//默認(rèn):嵌入式
    
    
    [moviePlayerC prepareToPlay];
    //手動(dòng)開啟
    [moviePlayerC play];
    
    
    
    
}
- (IBAction)MPMoviePlayerVIewController:(id)sender {
    NSString *path = [[NSBundle mainBundle]pathForResource:@"video" ofType:@"mp4"];
    NSURL *url1 = [NSURL fileURLWithPath:path];
    
    MPVC = [[MPMoviePlayerViewController alloc]initWithContentURL:url1];
    
    //自動(dòng)播放
    [self presentViewController:MPVC animated:YES completion:NULL];
    
    
}
- (IBAction)AVPlayerVIewController:(id)sender {
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"video" ofType:@"mp4"];
    NSURL *url1 = [NSURL fileURLWithPath:path];
    
    //播放視頻 (依賴于 AVKit)
    AVPlayerViewController *avPlayerVC = [[AVPlayerViewController alloc]init];
    
    //視頻播放器要播放視頻 AVPlayer (依賴 AVFoundation)
    //AVPlayer
    avPlayerVC.player = [AVPlayer playerWithURL:url1];
    
    //手動(dòng)開始播放
    [avPlayerVC.player play];
    
    [self presentViewController:avPlayerVC animated:YES completion:NULL];
}

-(void)playStateDidChange:(NSNotification*)noti{

    NSLog(@"播放狀態(tài)改變");
    
    MPMoviePlayerController *playerC = noti.object;
    
    //播放狀態(tài)
    MPMoviePlaybackState state = playerC.playbackState;
    
    if (state == MPMoviePlaybackStatePaused) {
        NSLog(@"暫停");
    }else if (state ==MPMoviePlaybackStateSeekingForward){
    
        NSLog(@"快進(jìn)");
    }
    
    
    
}
//視頻播放完成調(diào)用
-(void)playStateDidFinish:(NSNotification*)noti{

    NSLog(@"播放完成");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

  • AVPlayer 圖層

/** 播放器 */
@property (nonatomic, strong) AVPlayer *player;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // url
    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"];

    // 根據(jù)URL創(chuàng)建播放曲目
    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
    
    self.player = [AVPlayer playerWithPlayerItem:item];
    
    // 創(chuàng)建一個(gè)視頻播放圖層
    AVPlayerLayer *playLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    
    // 設(shè)置視頻播放frame
    playLayer.frame = CGRectMake(0, 200, self.view.frame.size.width, 300);
    
    // 將播放圖層添加到父控件
    [self.view.layer addSublayer:playLayer];
    
    [self.player play];
    
}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容