本地音乐播放器

本文详细介绍了如何使用Objective-C在iOS平台上开发一个具备基本音乐播放功能的应用,包括音频文件导入、播放、暂停、拖动滑竿前进或后退等操作。

需要导入



在AudioViewController.m中写代码

#import "AudioViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface AudioViewController ()<AVAudioPlayerDelegate, UIAlertViewDelegate>

@property(nonatomic, strong) AVAudioPlayer *player;
@property(nonatomic, assign) NSInteger count;
@property(nonatomic, strong) NSTimer *timer;
@property(nonatomic, assign) NSInteger count1;

@end

@implementation AudioViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

//快速拖动滑动条会回到左端
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //播放本地音频
    _player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"redRose" ofType:@"mp3"]] error:nil];
    //音量
    _player.volume = 1.0;
    _player.numberOfLoops = 1;
    //播放位置
    _player.currentTime = 0.0;
    //声道数
//    NSInteger channels = _player.numberOfChannels;//只读属性
    //持续时间
//    NSTimeInterval duration = _player.duration;//获取持续时间
    //仪表计数
    _player.meteringEnabled = YES;//开启仪表计数功能
    [_player updateMeters];
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 80, 50)];
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(handleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = 300;
    [self.view addSubview:button];
    
    UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(30, 200, 260, 10)];
    slider.minimumValue = 0.0;
    slider.maximumValue = _player.duration;
//    slider.value = 1.0f;
    [slider addTarget:self action:@selector(handleSliderAction:) forControlEvents:UIControlEventValueChanged];
    slider.tag = 200;
    [self.view addSubview:slider];
    
    
    //count代表button点击的次数,用来判断暂停、播放
    self.count = 0;
    //count1代表0代表未播放完成 2代表播放完成且要开始新一轮重播 其它代表播放完成暂停
    self.count1 = 0;
    
}

- (void)handleSliderAction:(UISlider *)sender{
    [_player pause];
        _player.currentTime = sender.value;
    self.count = 0;
    [self handleButtonAction:(UIButton *)[self.view viewWithTag:300]];
}

- (void)handleButtonAction:(UIButton *)sender{
    self.count++;
        if (self.count % 2 == 1) {
            //count1 = 2代表播放完成,且要开始新一轮播放
            if (self.count1 == 2) {
                _player.currentTime = 0.0;
                self.count1 = 0;
            }
            [_player prepareToPlay];//分配播放所需的资源,并将其加入内部播放队列
            [_player play];
            //创建定时器,每过0.1秒调用一次响应方法。repeats:是否重复调用目标方法。userinfo:发送的参数
            //程序开始后timer就一直开始运行,不会因音乐暂停而停止
            self.timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(handleTimerAction:) userInfo:nil repeats:YES];
        } else{
            [_player pause];
            [self.timer invalidate];
        }
}

- (void)handleTimerAction:(NSTimer *)sender{
    
    if ((_player.duration - _player.currentTime) < 0.1 && self.count1 == 0) {
        self.count1++;
        [_player pause];
        [self audioPlayerDidFinishPlaying:_player successfully:YES];
        //取消定时器
//        [self.timer invalidate];//定时器调用invallidate后,就会自动执行release方法,不需要在显示的时候调用release
//        self.timer = nil;
        //关闭定时器
        [self.timer setFireDate:[NSDate distantFuture]];
    }
        UISlider *slider = (UISlider *)[self.view viewWithTag:200];
        slider.value = _player.currentTime;
    
}

//代理方法,播放结束执行
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"通知" message:@"播放结束" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"再听一遍", nil];
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        self.count1 = 2;
        self.count = 0;
        [self handleButtonAction:(UIButton *)[self.view viewWithTag:300]];
    } else if (buttonIndex == 0){
        self.count = 0;
        self.count1 = 2;
    }
}

只有一首歌,不支持上一首、下一首,支持播放暂停,拖动滑竿可以前进或后退
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值