播放器 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

转载于:https://my.oschina.net/u/2501648/blog/552478

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值