iOS简单音乐播放器 歌词显示 自动切换背景


1.创建一个Songs类保存歌曲信息

#import <Foundation/Foundation.h>

@interface Songs : NSObject

@property(nonatomic,copy)NSString * songPath;//歌曲路径

@property(nonatomic,copy)NSString * lrc;//歌词路径

@end


2.向工程中导入歌词解析类

#import "LZXLrcItem.h"
#import "LZXLrcParser.h"


3.创建一个ViewController类,导入

在ViewController.h中

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@class Songs;//声明Songs类
@interface ViewController : UIViewController<AVAudioPlayerDelegate>
{
    AVAudioPlayer * avAudioPlayer;//声明一个播放器
    UIProgressView * progressV;//声明进度条
    UISlider * volumeSlider;//声明音量控制条
    NSTimer * timer;//声明一个计时器
    NSMutableArray * songArray;//声明一个歌曲数组 存放所有歌曲对象
    int i;//声明一个变量 控制播放歌曲songArray数组中的第i首歌曲
    Songs * so;//
    NSMutableArray * _picture;//声明一个数组 保存背景图片
}
@end

ViewController.m中


#import "ViewController.h"
#import "LZXLrcItem.h"
#import "LZXLrcParser.h"
#import "Songs.h"
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    i=0;
/背景图片设置
    //设置默认背景图片
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"lantian.jpg"]];
    //初始化背景图片数组
    _picture = [[NSMutableArray alloc]init];
    int j;
    for (j=0;j<=10;j++) {
        UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",j]];
        [_picture addObject:image];
    }
/实例化歌曲对象并导入歌曲数组///
    
    songArray = [[NSMutableArray alloc]init];
    //创建歌曲链接并导入到所有歌曲songArray数组中
    //第一首歌
    Songs * song1 = [[Songs alloc]init];
    song1.songPath = [[NSBundle mainBundle]pathForResource:@"大海" ofType:@"mp3"];
    song1.lrc = @"/Users/Desktop/我的项目/MusicPlayer/Songs/大海.txt";
    [songArray addObject:song1];
    //第二首歌
    Songs * song2 = [[Songs alloc]init];
    song2.songPath = [[NSBundle mainBundle]pathForResource:@"童话" ofType:@"mp3"];
    song2.lrc = @"/Users/Desktop/我的项目/MusicPlayer/Songs/童话.txt";
    [songArray addObject:song2];
    //第三首歌
    Songs * song3 = [[Songs alloc]init];
    song3.songPath = [[NSBundle mainBundle]pathForResource:@"练习" ofType:@"mp3"];
    song3.lrc = @"/Users/Desktop/我的项目/MusicPlayer/Songs/练习.txt";
    [songArray addObject:song3];
    //第四首歌
    Songs * song4 = [[Songs alloc]init];
    song4.songPath = [[NSBundle mainBundle]pathForResource:@"飞得更高" ofType:@"mp3"];
    song4.lrc = @"/Users/Desktop/我的项目/MusicPlayer/Songs/飞得更高.txt";
    [songArray addObject:song4];
 创建播放 暂停 停止三个按钮//
    
    
    //创建开始button
	UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:CGRectMake(130, 440, 60, 40)];
    [button setTitle:@"播放" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(mp3Play) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    //创建暂停button
    UIButton * button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button1 setFrame:CGRectMake(50, 440, 60, 40)];
    [button1 setTitle:@"暂停" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(mp3Pause) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    //创建停止button
    UIButton * button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button2 setFrame:CGRectMake(210, 440, 60, 40)];
    [button2 setTitle:@"停止" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(mp3Stop) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
///初始化一个歌曲播放进度条//
    progressV = [[UIProgressView alloc]initWithFrame:CGRectMake(30, 430, 260, 10)];
    progressV.trackTintColor = [UIColor darkGrayColor];
    progressV.tintColor = [UIColor cyanColor];
    [self.view addSubview:progressV];
/
    //找到要播放的songArray数组中的第i首歌曲的路径
    so = [[Songs alloc]init];
    so = [songArray objectAtIndex:i];
    //路径转化为链接
    NSURL * url = [[NSURL alloc]initFileURLWithPath:so.songPath];
    //使用上面的链接创建播放器
    avAudioPlayer  = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    //给播放器设置代理 设置代理 主要是为了监测音乐是否播放完毕
    avAudioPlayer.delegate = self;
    //设置播放次数 A value of zero means to play the sound just once. A value of one will result in playing the sound twice, and so on..
    avAudioPlayer.numberOfLoops = 0;
    //预播放
    [avAudioPlayer prepareToPlay];
/
    //用timer控制播放进度
    timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(playProgress) userInfo:nil repeats:YES];
/
    //初始化音量控制
    volumeSlider = [[UISlider alloc]initWithFrame:CGRectMake(240, 330, 100, 20)];
    //将横向音量条 旋转成竖向
    volumeSlider.transform = CGAffineTransformRotate(volumeSlider.transform, -3.14/2);
    [volumeSlider addTarget:self action:@selector(volumeChange) forControlEvents:UIControlEventValueChanged];
    //设置最小音量
    volumeSlider.minimumValue = 0.0f;
    //设置最大音量
    volumeSlider.maximumValue = 10.0f;
    //初始化音量
    volumeSlider.value = 5.0f;
    [self.view addSubview:volumeSlider];
/
   //歌词label
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 30)];
    label.tag = 10;
    //歌词显示居中于label
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
/
}
//播放开始
-(void)mp3Play{
    //用计时器控制背景图片的切换:点击开始播放 背景就开始变幻
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(viewChange) userInfo:nil repeats:YES];
    [avAudioPlayer play];
    i = i+1;
}
//背景变幻
- (void)viewChange{
    self.view.backgroundColor = [UIColor colorWithPatternImage:[_picture objectAtIndex:arc4random()%10+1]];
}

//播放暂停
- (void)mp3Pause{
    [avAudioPlayer pause];
}
//播放停止
-(void)mp3Stop{
    avAudioPlayer.currentTime = 0;
    [avAudioPlayer stop];
}
//播放进度  在播放时 解析歌词 并显示在歌词label上
-(void)playProgress{
    progressV.progress = avAudioPlayer.currentTime/avAudioPlayer.duration;
    //找到tag值为10的label
    UILabel * label = (UILabel *)[self.view viewWithTag:10];
    //初始化一个歌词解析对象
    LZXLrcParser * lzx = [[LZXLrcParser alloc]initWithFile:so.lrc];
    //更新歌词
    label.text = [lzx getLrcByTime:avAudioPlayer.currentTime];
}
//音量控制
-(void)volumeChange{
    avAudioPlayer.volume = volumeSlider.value;
}
//歌曲播放结束(代理<AVAudioPlayerDelegate>中的方法)
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    if (i==4) {//总共添加了4首歌在songArray数组,所以需要判断
        i=0;
    }
    ///播放结束后 继续播放下一首
    //找到路径
    so = [[Songs alloc]init];
    so = [songArray objectAtIndex:i];
    
    //路径转化为链接
    NSURL * url = [[NSURL alloc]initFileURLWithPath:so.songPath];
    //使用上面的链接创建播放器
    avAudioPlayer  = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    //给播放器设置代理 设置代理 主要是为了监测音乐是否播放完毕
    avAudioPlayer.delegate = self;
    //设置自动循环
    avAudioPlayer.numberOfLoops = 0;
    //预播放
    [avAudioPlayer prepareToPlay];
    [avAudioPlayer play];
    i++;
    
}

@end


最终效果如下



楼主是iOS新手,代码写得比较乱,也难免有错误之处,还望前辈指出. 不过在播放一首歌结束切换到另一首时,不知道是不是使用我上面那样的方法,虽然能成功切换到下一首,但总感觉有点牵强,谁有好的方法告诉下..


第一次发帖,有点小激动,希望能与大家一起学习,一起成长.

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值