播放器 9.0之前的視屏播放 AVFoundation音樂播放 AudioToolbox音效播放

//
//  ViewController.m
//  AVFoundation音樂播放
//
//  Created by DC020 on 15/12/28.
//  Copyright (c) 2015年 Bill. All rights reserved.
//

#import "ViewController.h"
//引入音樂播放框架
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVAudioPlayerDelegate>{
    NSDictionary *dict;
    NSArray *arrName;
    NSArray *arrType;
    NSTimer *timer;
    BOOL choose;
    int i;
    int SecEnd;
    int SecBe;
    int minEnd;
    int minBe;
}



@property(nonatomic,strong)AVAudioPlayer *audioPlayer;//音樂播放器

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    i = 0;
    choose = YES;
    _slider.transform = CGAffineTransformMakeRotation(1.57079633);
    [_buttonPause addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside];
    [_buttonLeft addTarget:self action:@selector(left) forControlEvents:UIControlEventTouchUpInside];
    [_buttonRight addTarget:self action:@selector(right) forControlEvents:UIControlEventTouchUpInside];
    arrName = @[@"葫蘆娃",@"鄧紫棋 - 單行的軌道",@"demo3",@"demo6",@"jazz",@"demo2"];
    arrType = @[@"mp3",@"m4a",@"mp3",@"mp3",@"mp3",@"mp3"];
    _label.text = arrName[i];
    _imageView.image = [UIImage imageNamed:@"1"];
    _imageView.layer.cornerRadius = 300/2;
    _imageView.layer.masksToBounds = YES;
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(progress)  userInfo:nil repeats:YES];
    [_slider addTarget:self action:@selector(value) forControlEvents:UIControlEventValueChanged];
    [self.audioPlayer play];//若是等於_audioPlayer就是set,這是get
}
-(void)value{
    _audioPlayer.volume = _slider.value;
}
#pragma mark audioPlayer屬性的get方法
-(AVAudioPlayer *)audioPlayer
{
    if (!_audioPlayer) {
        NSLog(@"播放器準備啓動,開始實例化");
        //1.獲取音樂文件路徑
        NSString *urlStr = [[NSBundle mainBundle]pathForResource:arrName[0] ofType:arrType[0]];
        NSURL *url = [NSURL fileURLWithPath:urlStr];//獲取本地音樂url,只能播放本地
        //2.初始化播放器
        NSError *error;
        _audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
        //設置播放器屬性
        _audioPlayer.numberOfLoops = 0;//0爲不循環,負數爲無限循環
        _audioPlayer.volume = 0.1;//音量範圍0-1
        NSLog(@"%f",_audioPlayer.duration);
        
        [_audioPlayer prepareToPlay];//加載音頻文件到緩存
        _audioPlayer.delegate = self;
        
        NSLog(@"%f",_audioPlayer.currentTime);
        ;
        SecBe =(int)_audioPlayer.currentTime%60;
       
        SecEnd = (int)_audioPlayer.duration % 60;
        _labelBegin.text = [NSString stringWithFormat:@"%.2f:%.2d",_audioPlayer.currentTime/60,SecBe];
        _labelEnd.text = [NSString stringWithFormat:@"%.2f:%.2d", _audioPlayer.duration/60,SecEnd];
    }
    
    return _audioPlayer;
}



-(void)initData:(int)num{
    
    NSString *str = [[NSBundle mainBundle]pathForResource:arrName[num] ofType:arrType[num]];
    NSURL *url = [NSURL fileURLWithPath:str];
    NSError *error;
    _audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
    
     _audioPlayer.numberOfLoops = 0;
    _audioPlayer.volume = 0.1;
    [_audioPlayer play];
    
    
    _label.text = arrName[num];
    _imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",num+1]];
    _imageView.layer.cornerRadius = 300/2;
    _imageView.layer.masksToBounds = YES;
    _labelBegin.text = [NSString stringWithFormat:@"%.2f",_audioPlayer.currentTime];
    _labelEnd.text = [NSString stringWithFormat:@"%.2f",_audioPlayer.duration];
    
    SecBe =(int)_audioPlayer.currentTime%60;
    
    SecEnd = (int)_audioPlayer.duration % 60;
    _labelBegin.text = [NSString stringWithFormat:@"%.2f:%.2d",_audioPlayer.currentTime/60,SecBe];
    _labelEnd.text = [NSString stringWithFormat:@"%.2f:%.2d", _audioPlayer.duration/60,SecEnd];
}


-(void)progress{
    _progressView.progress = _audioPlayer.currentTime/_audioPlayer.duration;
    _labelBegin.text = [NSString stringWithFormat:@"%.2f",_audioPlayer.currentTime];
    [self turnAround];
}


-(void)turnAround{
//    CGAffineTransformMakeRotation這個方法是根據原始圖形的transform來轉變的
//    CGAffineTransformRotate是根據前一次的狀態來改變
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0f];
    _imageView.transform =CGAffineTransformRotate(_imageView.transform, M_PI_4);
    [UIView commitAnimations];
}


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



#pragma mark 播放器代理方法-播放結束時
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    if (i <arrName.count-1) {
        i++;
    }
    else{
        i = 0;
    }
    _audioPlayer = nil;
    [self initData:i];
    
    
    
}

-(void)pause{
    if (choose) {
         [self.audioPlayer pause];
        choose = NO;
        timer.fireDate = [NSDate distantFuture];//定時器暫停
    }
    else{
        [self.audioPlayer play];
        choose = YES;
        timer.fireDate = [NSDate distantPast];//開始
    }
   
}

-(void)left{
    if (i >0) {
        i--;
    }
    else{
        i = (int)arrName.count-1;
    }
     NSLog(@"%d",i);
    [self initData:i];
}

-(void)right{
    if (i <arrName.count-1) {
        i++;
    }
    else{
        i = 0;
    }
    NSLog(@"%d",i);
    [self initData:i];
}
@end

緩存



//
//  ViewController.m
//  9.0之前的視屏播放
//
//  Created by DC020 on 15/12/28.
//  Copyright (c) 2015年 Bill. All rights reserved.
//

#import "ViewController.h"
//引入頭文件
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
@property(nonatomic,strong)MPMoviePlayerController *moviePlayer;//視頻播放控制器
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.moviePlayer play];
    
    //添加通知
    [self addNotatification];
}
-(void)addNotatification{
    //建立通知中心
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    //播放狀態改變的通知
    [notificationCenter addObserver:self selector:@selector(stateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];
    //播放完成的通知
    [notificationCenter addObserver:self selector:@selector(finishPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
}
-(void)dealloc{
    //移除全部self裏的通知監控
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(void)finishPlaying:(NSNotification *)notification{
    NSLog(@"播放結束!!!!");
}

-(void)stateChange:(NSNotification*)notification{
    //判斷播放狀態
    switch (self.moviePlayer.playbackState) {
        case MPMoviePlaybackStatePlaying:
            NSLog(@"正在播放...");
            break;
        case MPMoviePlaybackStatePaused:
            NSLog(@"暫停播放...");
            break;
        case MPMoviePlaybackStateStopped:
            NSLog(@"中止播放...");
            break;
        default:
            NSLog(@"播放狀態爲:%li",self.moviePlayer.playbackState);
            break;
    }
}

#pragma mark 建立視頻播放控制器
-(MPMoviePlayerController *)moviePlayer{
    if (!_moviePlayer) {
        //1.獲取視頻地址(能夠本地,也能夠網絡)
        NSString *urlStr = [[NSBundle mainBundle]pathForResource:@"0" ofType:@"mp4"];
        NSURL *url = [NSURL fileURLWithPath:urlStr];
        //2.初始化播放控制器
        _moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
        _moviePlayer.view.frame = self.view.frame;
        //播放器視圖->自適應屏幕寬高
        _moviePlayer.view.autoresizingMask =UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:_moviePlayer.view];
    }
    return _moviePlayer;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

網絡




//
//  ViewController.m
//  AVFoundation音樂播放
//
//  Created by DC020 on 15/12/28.
//  Copyright (c) 2015年 Bill. All rights reserved.
//

#import "ViewController.h"
//引入音樂播放框架
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVAudioPlayerDelegate>



@property(nonatomic,strong)AVAudioPlayer *audioPlayer;//音樂播放器

@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.audioPlayer play];//若是等於_audioPlayer就是set,這是get
}
#pragma mark audioPlayer屬性的get方法
-(AVAudioPlayer *)audioPlayer
{
    if (!_audioPlayer) {
        NSLog(@"播放器準備啓動,開始實例化");
        //1.獲取音樂文件路徑
        NSString *urlStr = [[NSBundle mainBundle]pathForResource:@"鄧紫棋 - 單行的軌道" ofType:@"m4a"];
        NSURL *url = [NSURL fileURLWithPath:urlStr];//獲取本地音樂url,只能播放本地
        //2.初始化播放器
        NSError *error;
        _audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
        //設置播放器屬性
        _audioPlayer.numberOfLoops = 0;//0爲不循環,負數爲無限循環
        _audioPlayer.volume = 0.1;//音量範圍0-1
        NSLog(@"%f",_audioPlayer.duration);
        [_audioPlayer prepareToPlay];//加載音頻文件到緩存
        _audioPlayer.delegate = self;
    }

    return _audioPlayer;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark 播放器代理方法-播放結束時
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    NSLog(@"音樂播放完成");
}
@end
框架


//
//  ViewController.m
//  AudioToolbox音效播放
//
//  Created by DC020 on 15/12/28.
//  Copyright (c) 2015年 Bill. All rights reserved.
//

#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //播放系統自帶音效
//    AudioServicesPlaySystemSound(1000);;;;;;;;;;
    //1.要獲取音效文件路徑->文件url
    NSString *audioFile = [[NSBundle mainBundle]pathForResource:@"videoRing" ofType:@"caf"];
    NSURL *fileUrl = [NSURL fileURLWithPath:audioFile];
    //2.獲取聲音ID
    //參數:音頻文件url,聲音id
    SystemSoundID soundID = 0;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileUrl, &soundID);
    NSLog(@"%@",audioFile);
    //若是須要在播放完成以後執行某些操做,能夠調用一下方法註冊一個回調函數
    AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundComplete, NULL);
    //3.播放
    AudioServicesPlaySystemSound(soundID);
    
}
void soundComplete(){
    NSLog(@"播放完成");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

ide

相關文章
相關標籤/搜索