多媒体之音乐播放

音效是比较简短的,音乐比较长,格式比较多,那就需要使用<AVFoundation/AVFoundation.h>框架,AVAudioPlayer只支持本地的

//

//  ViewController.m

//  AVFoundation

//

//  Created by City--Online on 15/5/5.

//  Copyright (c) 2015年 CYW. All rights reserved.

//



#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>



@interface ViewController ()<AVAudioPlayerDelegate>

@property(nonatomic,strong)AVAudioPlayer *audioPlayer;

@property(nonatomic,strong)UIButton *btnStop;

@property(nonatomic,strong)UIButton *btnPause;

@property(nonatomic,strong) UIButton *btnPlayer;

@property(nonatomic,strong)UIProgressView *progress;

@property(nonatomic,strong)UIProgressView *averagePowerProgress;

@property(nonatomic,weak)NSTimer *timer;



@end



@implementation ViewController



- (void)viewDidLoad {

    [super viewDidLoad];

    [self stepUI];

    

    NSString *path=[[NSBundle mainBundle]pathForResource:@"1" ofType:@".mp3"];

    NSURL *url=[[NSURL alloc]initFileURLWithPath:path];

    _audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];

    //立体声平衡 -1 左声道  0 左右声道平衡  1右声道

    _audioPlayer.pan=1.0;

    //音量(0-1)

    _audioPlayer.volume=0.8;

    //是否允许改变速率

    _audioPlayer.enableRate=YES;

    //播放速率 (0.5-2) 1.0正常播放

    _audioPlayer.rate=0.8;

    //是否启用音频测量 默认NO

    _audioPlayer.meteringEnabled=true;

    //循环次数

    _audioPlayer.numberOfLoops=3;

    //设置代理

    _audioPlayer.delegate=self;

    

    

}

-(void)stepUI

{

    // 进度条

   _progress=[[UIProgressView alloc]initWithFrame:CGRectMake(100, 100, 200, 10)];

    [self.view addSubview:_progress];

    

    _averagePowerProgress=[[UIProgressView alloc]initWithFrame:CGRectMake(100,  200, 100, 10)];

    [self.view addSubview:_averagePowerProgress];

    

    //停止播放

    _btnStop=[UIButton buttonWithType:UIButtonTypeSystem];

    [_btnStop setTitle:@"停止" forState:UIControlStateNormal];

    _btnStop.frame=CGRectMake(40, 150, 40, 40);

    _btnStop.tag=1001;

    [_btnStop addTarget:self action:@selector(operation:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_btnStop];

    

    //播放

    _btnPlayer=[UIButton buttonWithType:UIButtonTypeSystem];

    [_btnPlayer setTitle:@"播放" forState:UIControlStateNormal];

    _btnPlayer.frame=CGRectMake(90, 150, 40, 40);

    _btnPlayer.tag=1002;

    [_btnPlayer addTarget:self action:@selector(operation:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_btnPlayer];

    

    _btnPause=[UIButton buttonWithType:UIButtonTypeSystem];

    [_btnPause setTitle:@"暂停" forState:UIControlStateNormal];

    _btnPause.frame=CGRectMake(140, 150, 40, 40);

    _btnPause.tag=1003;

    [_btnPause addTarget:self action:@selector(operation:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_btnPause];

    

    _timer=[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(updateProgress) userInfo:nil repeats:YES];

  

}

-(void)operation:(id)sender

{

    UIButton *btn=(UIButton *)sender;

    switch (btn.tag) {

        case 1001:

            if ([_audioPlayer isPlaying]) {

                [_audioPlayer stop];

                [_progress setProgress:0];

                //

                 _timer.fireDate=[NSDate distantFuture];

                _audioPlayer.currentTime=0;

                

                NSLog(@"%ld",_audioPlayer.numberOfChannels);

            }

            break;

        case 1002:

            if (![_audioPlayer isPlaying]) {

                [_audioPlayer play];

                //恢复定时

                _timer.fireDate=[NSDate distantPast];

            }

            break;

        case 1003:

            if ([_audioPlayer isPlaying]) {

                [_audioPlayer pause];

                //暂停定时器,注意不能调用invalidate方法,此方法会取消,之后无法恢复

                _timer.fireDate=[NSDate distantFuture];

            }

            break;



        default:

            break;

    }

    



}

-(void)updateProgress

{

    float progress=_audioPlayer.currentTime/_audioPlayer.duration;

    [_progress setProgress:progress animated:YES];

    //更新音频测量值,注意如果要更新音频测量值必须设置meteringEnabled为YES,通过音频测量值可以即时获得音频分贝等信息

    [_audioPlayer updateMeters];

    //获得指定声道的分贝峰值,注意如果要获得分贝峰值必须在此之前调用updateMeters方法

//   float  peakPower=[_audioPlayer peakPowerForChannel:2];

//    NSLog(@"%lf",peakPower);

    //获得指定声道的分贝平均值,注意如果要获得分贝平均值必须在此之前调用updateMeters方法

   float averagePower= [_audioPlayer averagePowerForChannel:1];

    //类似上下波动

    [_averagePowerProgress setProgress:(-averagePower)/50.0 animated:YES];

    NSLog(@"%lf",averagePower);

    

}





-(void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:AVAudioSessionRouteChangeNotification object:nil];

}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;

{

    NSLog(@"播放完毕");

}





- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error

{

     NSLog(@"%@",error);

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end

 多媒体之音乐播放

上面的是进度条,下面的是分贝平均值,类似有的音乐播放器波浪效果

你可能感兴趣的:(多媒体)