iOS 音乐播放器的实现

iOS 音乐播放器的实现


一:现在好多公司都要开始植入音频播放,所以我也做研究下;音乐播放其实很简单,ios已经帮我们实现了这样的一个播放器 AVAudioPlayer。请看代码

//
//  WFMusicViewController.m
//  WFUtils
//
//  Created by PC on 4/8/16.
//  Copyright © 2016 ibmlib. All rights reserved.
//

#import "WFMusicViewController.h"
#import 
    
    
     
     

@interface WFMusicViewController ()
     
     
      
      

@property (strong, nonatomic) AVAudioPlayer *musicPlayer;

@property (strong, nonatomic) UIButton *btnPlay;

@property (strong, nonatomic) UIProgressView *progressView;
/*** 计时器 ***/
@property (strong, nonatomic) NSTimer *timer;

@end

@implementation WFMusicViewController

- (NSTimer *)timer
{
    if(_timer == nil)
    {
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateProgress) userInfo:nil repeats:YES];
    }
    return _timer;
}

#pragma mark - 初始化相关
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"雪千寻-轻音乐";
    
    // *** 1. 初始化播放器
    [self initMusicPlayer];
    // *** 2. 初始化播放、暂停按钮
    [self createPlayButton];
    // *** 3. 初始化进度条
    [self createProgress];
}

- (void)initMusicPlayer
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"轻音乐 - 雪千寻.mp3" ofType:nil];
    self.musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath] error:nil];
    self.musicPlayer.delegate = self;
}

- (void)createPlayButton
{
    self.btnPlay = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnPlay.frame = CGRectMake(50, self.view.frame.size.height - 100, 100, 50);
    self.btnPlay.center = CGPointMake(self.view.center.x, self.btnPlay.center.y);
    
    self.btnPlay.backgroundColor = [UIColor greenColor];
    [self.btnPlay setTitle:@"播放" forState:UIControlStateNormal];
    [self.btnPlay setTitle:@"暂停" forState:UIControlStateSelected];
    [self.btnPlay addTarget:self action:@selector(actionToPlay:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btnPlay];
}

- (void)createProgress
{
    self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(10, 200, self.view.frame.size.width - 20, 20)];
    
    [self.view addSubview:self.progressView];
    
    
}

#pragma mark -
- (void)updateProgress
{
    float progress = self.musicPlayer.currentTime / self.musicPlayer.duration;
    [self.progressView setProgress:progress animated:YES];
}

- (void)actionToPlay:(UIButton *)sender
{
    if(!sender.selected)
    {
        if(![self.musicPlayer isPlaying])
        {
            [self.musicPlayer play];
            self.timer.fireDate = [NSDate distantPast];
        }
    }
    else
    {
        [self.musicPlayer pause];
        self.timer.fireDate = [NSDate distantFuture];
    }
    sender.selected = !sender.selected;
}

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

#pragma mark - delegate AVAudioPlayer
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    NSLog(@"音乐播放结束");
    self.btnPlay.selected = NO;
    [self.progressView setProgress:0];
    [self.timer invalidate];
    self.timer = nil;
}

/* if an error occurs while decoding it will be reported to the delegate. */
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError * __nullable)error
{
    
}

/* AVAudioPlayer INTERRUPTION NOTIFICATIONS ARE DEPRECATED - Use AVAudioSession instead. */

/* audioPlayerBeginInterruption: is called when the audio session has been interrupted while the player was playing. The player will have been paused. */
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player NS_DEPRECATED_IOS(2_2, 8_0)
{
    
}

/* audioPlayerEndInterruption:withOptions: is called when the audio session interruption has ended and this player had been interrupted while playing. */
/* Currently the only flag is AVAudioSessionInterruptionFlags_ShouldResume. */
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags NS_DEPRECATED_IOS(6_0, 8_0)
{
    
}

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags NS_DEPRECATED_IOS(4_0, 6_0)
{
    
}

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{
    
}

@end

     
     
    
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值