iOS音乐播放器详解(MusicPlayer1.0)

  1. 先放效果图:

效果图
2. 功能如下:

/播放
/ 暂停
/ 停止(计时归零)
/ 左扫切歌(歌曲的名字和总计时会根据歌曲的不同而辩变化)
/ 控制音量
/ 播放过程中进度条在走,并且颜色随机变化
/ 计时功能
/ 图片会随着歌曲的播放和暂停会旋转和停止旋转
3. 素材准备:

凡是效果图上展示出来的图片都需要寻找(背景/按钮/歌手的图片/最主要的是音乐素材(如果爱-张学友.mp3/煎熬-李佳薇.mp3/王菲-红豆.mp3))
4. 首先音乐播放功能需要导入系统框架 遵守协议

#import <AVFoundation/AVFoundation.h>
@interface RootViewController : UIViewController<AVAudioPlayerDelegate>

5.其次声明几个属性

@property (nonatomic,retain)NSURL *url;  // url路径
@property (nonatomic,retain)UIProgressView *progressV; //播放进度条
@property (nonatomic, retain)NSTimer *timer; //监控音频播放进度/ 图片旋转 
@property (nonatomic,retain)UISlider *volumeSlider; //声音控制Slider
@property (nonatomic,retain)UIImageView *photo;  // 照片
@property (nonatomic,retain)AVAudioPlayer *avAudioPlayer; //播放器player
@property (nonatomic,retain)UILabel *label;   // 显示播放时间

6.主要的功能实现是需要依靠AVAudioPlayer类来实现的.其中包括音乐的播放属性(比如:音乐总时长,播放循环次数,开始,停止等).下面是代码实现部分:

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController
- (void)dealloc
{
    [_url release];
    [_progressV release];
    [_timer release];
    [_volumeSlider release];
    [_photo release];
    [_avAudioPlayer release];
    [_label release];
    [super dealloc];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self addSubViews];

    // 当前的标题
    self.navigationItem.title = @"如果爱-张学友";

#warning  清扫换歌

    // 添加清扫手势   切歌
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(actionSwipe:)];
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipe];
    [swipe release];
}

#warning  界面

- (void)addSubViews
{
    // 背景图片
    UIImageView *bgImage = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    bgImage.image = [UIImage imageNamed:@"bg"];
    [self.view addSubview:bgImage];
    [bgImage release];

    // 歌手大头贴
    UIImageView *zhangImage = [[UIImageView alloc]initWithFrame:CGRectMake(75 / 2, 58, 310, 300)];
    zhangImage.image = [UIImage imageNamed:@"张学友"];
    self.photo = zhangImage;
    [self.view addSubview:self.photo];
    [zhangImage release];


    // 底部图
    UIImageView *bottomImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 280, 375, 500)];
    bottomImage.image = [UIImage imageNamed:@"bottom"];
    [self.view addSubview:bottomImage];
    [bottomImage release];

    // 修饰的圆弧
    UIImageView *xiuImage = [[UIImageView alloc]initWithFrame:CGRectMake(65, 550, 250, 250)];
    xiuImage.image = [UIImage imageNamed:@"123"];
    [self.view addSubview:xiuImage];
    [xiuImage release];


    // 播放按钮
    UIButton *playButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    playButton.frame = CGRectMake(295 / 2, 570, 80, 80);
    [playButton setImage:[UIImage imageNamed:@"播放1"] forState:(UIControlStateNormal)];
    [playButton setImage:[UIImage imageNamed:@"播放"] forState:(UIControlStateHighlighted)];
    [playButton addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:playButton];


    // 停止按钮
    UIButton *stopButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    stopButton.frame = CGRectMake(50, 500, 70, 70);
    [stopButton setImage:[UIImage imageNamed:@"停止1"] forState:(UIControlStateNormal)];
    [stopButton setImage:[UIImage imageNamed:@"停止"] forState:(UIControlStateHighlighted)];
    [stopButton addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:stopButton];


    // 暂停按钮
    UIButton *pauseButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    pauseButton.frame = CGRectMake(250, 500, 70, 70);
    [pauseButton setImage:[UIImage imageNamed:@"暂停1"] forState:(UIControlStateNormal)];
    [pauseButton setImage:[UIImage imageNamed:@"暂停"] forState:(UIControlStateHighlighted)];
    [pauseButton addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:pauseButton];


    // 显示播放时间的label
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(140, 450, 200, 30)];
    [self.view addSubview:self.label];
    [_label release];


    //初始化音量控制
    self.volumeSlider = [[UISlider alloc] initWithFrame:CGRectMake(250, 550, 200, 5)];


    // 将音量的slider 竖起来
    self.volumeSlider.transform = CGAffineTransformRotate(self.volumeSlider.transform,4.71);
    self.volumeSlider.tintColor = [UIColor colorWithRed:0.384 green:0.887 blue:1.000 alpha:1.000];
    [self.volumeSlider addTarget:self action:@selector(volumeChange)
                forControlEvents:UIControlEventValueChanged];
    [self.volumeSlider setThumbImage:[UIImage imageNamed:@"valumButton"] forState:(UIControlStateNormal)];
    [self.volumeSlider setThumbImage:[UIImage imageNamed:@"valumButton"] forState:(UIControlStateHighlighted)];
    self.volumeSlider.maximumValueImage = [UIImage imageNamed:@"laba"];



    //设置最小音量
    self.volumeSlider.minimumValue = 0.0f;

    //设置最大音量
    self.volumeSlider.maximumValue = 10.0f;


    //初始化音量为多少
    self.volumeSlider.value = 10.0f;


    [self.view addSubview:self.volumeSlider];
    [self.volumeSlider release];

    //初始化一个播放进度条
    self.progressV = [[UIProgressView alloc] initWithFrame:CGRectMake(60, 450, 250, 20)];
    [self.view addSubview:self.progressV];
    [self.progressV release];



#warning 计时器

    //用NSTimer来监控音频播放进度
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(playProgress) userInfo:nil repeats:YES];

    [self.timer fire];



#warning AvAudioPlayer

    //从budle路径下读取音频文件  这个文件名是你的歌曲名字,mp3是你的音频格式
    NSString *string = [[NSBundle mainBundle] pathForResource:@"如果.爱 - 张学友" ofType:@"mp3"];

    //把音频文件转换成url格式
    self.url = [NSURL fileURLWithPath:string];

    /*
     file:///Users/lanou3g/Library/Developer/CoreSimulator/Devices/F4C0035C-BFC9-4B9A-95D1-59CE94B63E35/data/Containers/Bundle/Application/CB7A7074-D5CF-47F0-866D-29C9D0C86A2D/MusicPlayer1.0.app/%E5%A6%82%E6%9E%9C.%E7%88%B1%20-%20%E5%BC%A0%E5%AD%A6%E5%8F%8B.mp3
     */

    //初始化音频类 并且添加播放文件
    _avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.url error:nil];

    //设置代理
    _avAudioPlayer.delegate = self;

    //设置初始音量大小
     _avAudioPlayer.volume = 10;

    //设置音乐播放次数  -1为一直循环
    _avAudioPlayer.numberOfLoops = -1;

    //预播放
   // [_avAudioPlayer prepareToPlay];

}

// 这个i 是设置图片旋转角度的.
/**
 *  初始化为 0,通过计时器每次让这个值加0.000000001  ()
 */
float i = 0.0;
#warning  Button 的方法
//播放
- (void)play
{
    [_avAudioPlayer play];
    i = 0.02;
}

//暂停
- (void)pause
{
    [_avAudioPlayer pause];
    i = 0;
}

//停止
- (void)stop
{
    _avAudioPlayer.currentTime = 0;  //当前播放时间设置为0
    [_avAudioPlayer stop];
    i = 0;
}

#warning   进度条

//播放进度条
- (void)playProgress
{

    //通过音频播放时长的百分比,给progressview进行赋值
    self.progressV.progress = _avAudioPlayer.currentTime/_avAudioPlayer.duration;


    // 让进度条的颜色随机  不灵不灵的闪
    self.progressV.tintColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256 /255.0 alpha:1.000];


    // 播放时间
    self.label.text = [NSString stringWithFormat:@"%.2d:%.2d / %.2d:%.2d", (int)_avAudioPlayer.currentTime/60, (int)_avAudioPlayer.currentTime%60,  (int)_avAudioPlayer.duration/60, (int)_avAudioPlayer.duration%60];

    // 让图片旋转起来
    i += 0.000000001;
    self.photo.transform = CGAffineTransformRotate(self.photo.transform,  i);

}
#warning   控制音量

//播放音量控制
- (void)volumeChange
{
    _avAudioPlayer.volume = self.volumeSlider.value;
}

#warning  代理方法
//播放完成时调用的方法  (代理里的方法),需要设置代理才可以调用

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    [self.timer invalidate]; //NSTimer暂停
}


/*
 AVAudioPlayerDelegate   代理方法如下:

// 播放结束
 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
// 解码错误
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error;
// 开始中断
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player NS_DEPRECATED_IOS(2_2, 8_0);
// 已经中断  适用的系统不一样

- (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 NS_DEPRECATED_IOS(2_2, 6_0);
 */

#warning  切歌的清扫手势方法

// 清扫的手势触发的方法  切歌
- (void)actionSwipe:(UISwipeGestureRecognizer *)swipe
{
    static int flag = 0;
    if (flag == 0) {
        // 停止播放当前的音乐文件
        [self stop];

        //从budle路径下读取音频文件  这个文件名是你的歌曲名字,mp3是你的音频格式
        NSString *string1 = [[NSBundle mainBundle] pathForResource:@"煎熬 - 李佳薇" ofType:@"mp3"];
        // 根据字符串解析成url
        self.url = [NSURL fileURLWithPath:string1];
        // 根据url变成我们的播放文件
        self.avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.url error:nil];
        // 更改图片
        self.photo.image = [UIImage imageNamed:@"李佳薇"];
        // 更改标题
        self.navigationItem.title = @"煎熬 - 李佳薇";
        // 初始化音量
        self.avAudioPlayer.volume = 10;
        // 循环播放
        self.avAudioPlayer.numberOfLoops = -1;
        // 播放我们的新歌曲
        [self play];
        // 将flag置为1 为下一手势做准备
        flag = 1;

    } else if(flag == 1){

        [self stop];

        NSString *string1 = [[NSBundle mainBundle] pathForResource:@"王菲 - 红豆" ofType:@"mp3"];
        self.url = [NSURL fileURLWithPath:string1];
        self.avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.url error:nil];
        self.photo.image = [UIImage imageNamed:@"faye000"];
        //self.photo.frame = CGRectMake(40, 62, 300, 330);
        self.navigationItem.title = @"王菲 - 红豆";
        self.avAudioPlayer.volume = 10;
        self.avAudioPlayer.numberOfLoops = -1;
        [self play];
        flag = 2;
    } else if(flag == 2){

        [self stop];

        NSString *string1 = [[NSBundle mainBundle] pathForResource:@"如果.爱 - 张学友" ofType:@"mp3"];
        self.url = [NSURL fileURLWithPath:string1];
        self.avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.url error:nil];
        self.photo.image = [UIImage imageNamed:@"张学友"];
        self.navigationItem.title = @"如果.爱 - 张学友";
        self.avAudioPlayer.volume = 10;
        self.avAudioPlayer.numberOfLoops = -1;
        [self play];
        flag = 0;

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}/*
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值