AVAudioPlayer播放音乐

1:首先创建一个新的项目,继承自UIViewController

2:导入框架AVFoundation.framework

右键工程名,在Build Phases的Link Binary With Libraries中的+号,找到AVFoundation.framework添加即可

3,导入音乐

4:添加代理AVAudioPlayerDelegate

5代码如下

//

//  ViewController.m

//  PlayMusic

//

//  Created by summer on 16/4/4.

//  Copyright © 2016年 summer. All rights reserved.

//

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVAudioPlayerDelegate>

@property(nonatomic,strong)AVAudioPlayer *player;

@property(nonatomic,strong)NSTimer *timer;

@property(nonatomic,strong) UISlider *slider;

@property(nonatomic,strong)UIProgressView *progress;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    //添加音乐

    NSURL *url=[[NSBundle mainBundle]URLForResource:@"Need You Now.wav" withExtension:Nil];

    NSLog(@"%@",url);

    self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil ];

    //设置音量大小

    self.player.volume=5;

    //设置循环次数-1是无限循环,1就是1次

    self.player.numberOfLoops=1;

    //设置代理

    self.player.delegate=self;

    //准备播放

    [self.player prepareToPlay];

    //添加3个button,和响应事件

    UIButton *btnPlay=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 120, 50)];

    btnPlay.backgroundColor=[UIColor cyanColor];

    [btnPlay.layer setMasksToBounds:YES];

    [btnPlay.layer  setCornerRadius:5];

    [btnPlay setTitle:@"点击播放音乐" forState:UIControlStateNormal];

    [btnPlay addTarget:self action:@selector(PlayMusic) forControlEvents:UIControlEventTouchUpInside];

    

    UIButton *btnSuspend=[[UIButton alloc]initWithFrame:CGRectMake(100, 200, 120, 50)];

    btnSuspend.backgroundColor=[UIColor cyanColor];

    [btnSuspend.layer setMasksToBounds:YES];

    [btnSuspend.layer  setCornerRadius:5];

    [btnSuspend setTitle:@"点击暂停音乐" forState:UIControlStateNormal];

    [btnSuspend addTarget:self action:@selector(SuspendMusic) forControlEvents:UIControlEventTouchUpInside];

    

    UIButton *btnStop=[[UIButton alloc]initWithFrame:CGRectMake(100, 300, 120, 50)];

    btnStop.backgroundColor=[UIColor cyanColor];

   [btnStop.layer setMasksToBounds:YES];

    [btnStop.layer setCornerRadius:5];

    [btnStop setTitle:@"点击停止音乐" forState:UIControlStateNormal];

    [btnStop addTarget:self action:@selector(StopMusic) forControlEvents:UIControlEventTouchUpInside];

    //设置一个switch,添加事件

    UISwitch *swi=[[UISwitch alloc]initWithFrame:CGRectMake(100, 380, 100, 40)];

    swi.on=YES;

    [swi addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];

    //设置一个进度条,用 NStimer来更新进度条

    self.progress=[[UIProgressView alloc]initWithFrame:CGRectMake(100, 430, 200, 30)];

    self.timer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(playProgress) userInfo:nil repeats:YES];

    //设置一个音量空的slider,并添加响应事件

    self.slider=[[UISlider alloc]initWithFrame:CGRectMake(100, 480, 200, 20)];

    [self.slider  addTarget:self action:@selector(volumeChange) forControlEvents:UIControlEventValueChanged];

    //设置声音的最小值

    self.slider.minimumValue=0.0f;

    //设置声音的最大值

    self.slider.maximumValue=10.0f;

    //设置声音的当前值

    self.slider.value=5.0f;

    //加入视图中显示

    [self.view addSubview:self.slider];

    [self.view addSubview:self.progress];

    [self.view addSubview:swi];

    [self.view addSubview:btnPlay];

    [self.view addSubview:btnSuspend];

    [self.view addSubview:btnStop];

    

  

    

    //AudioServicesPlaySystemSound(sound);

    

    // Do any additional setup after loading the view, typically from a nib.

}

-(void) PlayMusic{

    //开始播放

    [self.player play];

}

-(void)SuspendMusic{

    //暂停音乐

    [self.player pause];

}

-(void)StopMusic{

    //停止播放,音乐播放时间置为0

    self.player.currentTime=0;

    [self.player stop];

}

-(void)playProgress{

    //进度条的时间是当前时间/音乐的总时间

    self.progress.progress=self.player.currentTime/self.player.duration;

}

-(void)switchChange:(UISwitch *)sender{

    self.player.volume=sender.on;

}

-(void)volumeChange{

    self.player.volume=self.slider.value;

}

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

    //音乐播放完时调用的事件,都清空

    [self.timer invalidate];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

原文地址:https://www.cnblogs.com/summerxx/p/5356631.html