闲来无事,准备自己写一个简单的音乐播放器玩玩

首先我想到的是,如果只是自己用的话,用户名密码应该不需要的吧,但是这样就体现不出是我自己用的了,所以我准备设置一个登陆账号,只有用自己的登陆账号登陆的时候才会有一些特殊的权限。那么我们首先来创建一个登陆界面,类似于这样。

ios 音乐播放器开发 ios smb 音乐播放器_#import

这个界面搭建也很简单,就直接上代码了

//
//  ViewController.h
//  ZQMusic
//
//  Created by 赵前 on 16/6/7.
//  Copyright © 2016年 赵前. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end



//
//  ViewController.m
//  ZQMusic
//
//  Created by 赵前 on 16/6/7.
//  Copyright © 2016年 赵前. All rights reserved.
//

#import "ViewController.h"
#import "PlayViewController.h"
#import "HomeViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface ViewController ()
@property(nonatomic ,strong)UIImageView *bacImageView;
@property(nonatomic ,strong)UIImageView *headImageView;
@property(nonatomic ,strong)UITextField *accountTextField;
@property(nonatomic, strong)UIButton *button;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.bacImageView];
    [self.view addSubview:self.headImageView];
    [self.view addSubview:self.accountTextField];
    [self.view addSubview:self.button];
}


- (void)respondsToBtn:(UIButton *)sender{
    HomeViewController *HomeVc = [HomeViewController new];
//    [self presentViewController:HomeVc animated:YES completion:nil];
    [self.navigationController pushViewController:HomeVc animated:YES];
//    PlayViewController *playerVc = [[PlayViewController alloc]init];
//    [self presentViewController:playerVc animated:YES completion:nil
//     ];
}
- (UIImageView *)bacImageView{
    if (!_bacImageView) {
        _bacImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
        _bacImageView.image = [UIImage imageNamed:@"backImg.jpg"];
        _bacImageView.backgroundColor = [UIColor clearColor];
    }
    return _bacImageView;
}
- (UIImageView *)headImageView{
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
        _headImageView.center = CGPointMake(WIDTH/2, HEIGHT*0.3);
        _headImageView.backgroundColor = [UIColor blueColor];
        _headImageView.image = [UIImage imageNamed:@"headimg.jpg"];
        _headImageView.layer.cornerRadius = 40;
        _headImageView.clipsToBounds = YES;
    }
    return _headImageView;
}
- (UITextField *)accountTextField{
    if (!_accountTextField) {
        _accountTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];
        _accountTextField.center = CGPointMake(WIDTH/2, HEIGHT *0.4);
        _accountTextField.backgroundColor = [UIColor whiteColor];
        _accountTextField.font = [UIFont systemFontOfSize:25];
        _accountTextField.text = @"zhaoqian";
    }
    return _accountTextField;
}
-(UIButton *)button{
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.frame = CGRectMake(0, 0, 150, 40);
        _button.center = CGPointMake(WIDTH/2, HEIGHT*0.5);
        [_button setTitle:@"进入" forState:UIControlStateNormal];
        _button.titleLabel.font = [UIFont systemFontOfSize:25];
        [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _button.backgroundColor = [UIColor colorWithRed:92.0/255 green:176.0/255 blue:254.0/255 alpha:0.8f];
        _button.layer.cornerRadius = 10.0f;
        _button.clipsToBounds = YES;
        [_button addTarget:self action:@selector(respondsToBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
}

@end




上面的是登陆界面,然后我想登陆进去是怎么样的呢,想了很久 ,写了一个简单的主页

ios 音乐播放器开发 ios smb 音乐播放器_#pragma_02

大概写成这样子,写一个分段控件,然后结合ScrollView,看代码

//
//  HomeViewController.h
//  ZQMusic
//
//  Created by 赵前 on 16/6/8.
//  Copyright © 2016年 赵前. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface HomeViewController : UIViewController

@end



//
//  HomeViewController.m
//  ZQMusic
//
//  Created by 赵前 on 16/6/8.
//  Copyright © 2016年 赵前. All rights reserved.
//

#import "HomeViewController.h"
#import "mineView.h"
@interface HomeViewController ()<UIScrollViewDelegate>
{
    NSArray *_segDataSource;
}
@property(nonatomic, strong)UIImageView *imageView;
@property(nonatomic, strong)UISegmentedControl *segmentedControl;
@property(nonatomic, strong)UIScrollView *scrollView;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
    
    
    mineView *myView = [[mineView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    [self.scrollView addSubview:myView];
}
- (void)initializeUserInterface{
    self.view.backgroundColor  = [UIColor whiteColor];
    [self.view addSubview: self.imageView];
    [self.view addSubview:self.segmentedControl];
    [self.view addSubview:self.scrollView];
}
- (void)initializeDataSource{
    _segDataSource = @[@"我的",@"热歌",@"搜索"];
}
#pragma mark *** delegate ***
//-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
//    self.segmentedControl.selectedSegmentIndex = scrollView.contentOffset.x/(self.view.bounds.size.width);
//}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.segmentedControl.selectedSegmentIndex = scrollView.contentOffset.x/(self.view.bounds.size.width);
}
#pragma mark *** Events ***

- (void)respondsToSegmentControl:(UISegmentedControl *)sender{
    self.scrollView.contentOffset = CGPointMake(sender.selectedSegmentIndex*self.view.bounds.size.width, 0);
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
}

#pragma mark *** lazy loading ***
-(UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
        _imageView.image = [UIImage imageNamed:@"beijing1.jpg"];
    }
    return _imageView;
}
-(UISegmentedControl *)segmentedControl{
    if (!_segmentedControl) {
        _segmentedControl = [[UISegmentedControl alloc]initWithItems:_segDataSource];
        _segmentedControl.frame = CGRectMake(0, 0, 300, 30);
        _segmentedControl.center = CGPointMake(self.view.bounds.size.width/2, 50);
        _segmentedControl.selectedSegmentIndex = 0;
        [_segmentedControl setWidth:100 forSegmentAtIndex:0];
        [_segmentedControl addTarget:self action:@selector(respondsToSegmentControl:) forControlEvents:UIControlEventValueChanged];
        _segmentedControl.tintColor = [UIColor brownColor];
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil];
        [_segmentedControl setTitleTextAttributes:dic forState:UIControlStateNormal];
//        _segmentedControl.momentary = YES;
    }
    return _segmentedControl;
}
-(UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView  = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height - 100)];
        _scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * _segDataSource.count, self.view.bounds.size.height - 100);
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate =self;
        _scrollView.backgroundColor = [UIColor clearColor];
        
    }
    return _scrollView;
}
@end



我的这个界面是我自己封装起来加到scrollView上的

代码如下

//
//  mineView.h
//  ZQMusic
//
//  Created by 赵前 on 16/6/8.
//  Copyright © 2016年 赵前. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface mineView : UIView
@property (nonatomic, strong)UIImageView *headView;
@property (nonatomic, strong)UILabel *userName;
@property (nonatomic, strong)UITableView *tableView;
@end



//
//  mineView.m
//  ZQMusic
//
//  Created by 赵前 on 16/6/8.
//  Copyright © 2016年 赵前. All rights reserved.
//

#import "mineView.h"
#import "mineCustomTableViewCell.h"
#define WIDTH self.bounds.size.width
#import "PlayViewController.h"
#import "HomeViewController.h"

@interface mineView()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray *_dataSource;
}

@end
@implementation mineView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _dataSource = [self returnData];
        [self addSubview:self.headView];
        [self addSubview:self.userName];
        [self addSubview:self.tableView];
    }
    return self;
}
- (NSArray *)returnData{
    return @[@"本地歌曲",@"我的收藏"];
}
#pragma mark *** delegateANDdatasource ***
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    mineCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"zhaoqian"];
    if (!cell) {
        cell = [[mineCustomTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"zhaoqian"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = _dataSource[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:22];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.layer.borderWidth = 0.4f;
    cell.layer.borderColor = [UIColor blackColor].CGColor;
    cell.selectionStyle =  UITableViewCellSelectionStyleDefault;
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        PlayViewController *playerVc = [[PlayViewController alloc]init];
//        [(HomeViewController *)self.superview.nextResponder presentViewController:playerVc animated:YES completion:nil
//             ];
        [((HomeViewController *)self.superview.superview.nextResponder).navigationController pushViewController:playerVc animated:YES];
    }
}

#pragma mark *** lazy loding ***
- (UIImageView *)headView{
    if (!_headView ) {
        _headView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 00, 60, 60)];
        _headView.center = CGPointMake(WIDTH/2, 30);
        _headView.layer.cornerRadius = 30;
        _headView.clipsToBounds = YES;
        _headView.backgroundColor = [UIColor clearColor];
        _headView.image = [UIImage imageNamed:@"headimg.jpg"];
    }
    return _headView;
}
-(UILabel *)userName{
    if (!_userName) {
        _userName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
        _userName.center = CGPointMake(WIDTH/2, CGRectGetMaxY(self.headView.frame) + 20 + 10);
        _userName.backgroundColor = [UIColor clearColor];
        _userName.text = @"zhaoqian";
        _userName.textColor = [UIColor whiteColor];
        _userName.font = [UIFont systemFontOfSize:20];
        _userName.textAlignment = NSTextAlignmentCenter;
    }
    return _userName;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.userName.frame) + 10, WIDTH, self.bounds.size.height - CGRectGetMaxY(self.userName.frame) - 10) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
    }
    return _tableView;
}

@end



实现了这个界面之后,肯定要有一个播放界面

于是我又写了一个播放界面,当点击本地歌曲的时候跳转(暂时支持本地音乐,后面我会继续写代码)

ios 音乐播放器开发 ios smb 音乐播放器_#import_03

当点击选择某个音乐文件,然后再点击paly按钮的时候回播放音乐

代码如下

//
//  PlayViewController.h
//  ZQMusic
//
//  Created by 赵前 on 16/6/7.
//  Copyright © 2016年 赵前. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface PlayViewController : UIViewController

@end



//
//  PlayViewController.m
//  ZQMusic
//
//  Created by 赵前 on 16/6/7.
//  Copyright © 2016年 赵前. All rights reserved.
//

#import "PlayViewController.h"
#import <AVFoundation/AVFoundation.h>
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface PlayViewController ()<AVAudioPlayerDelegate,UITableViewDelegate,UITableViewDataSource>
{
    AVAudioPlayer *avAudioPlayer;
    NSMutableArray *_dataSource;
}
@property (nonatomic, strong)UIImageView *BimageView;
@property (nonatomic, strong)UITableView *tabView;
@end

@implementation PlayViewController

- (void)viewDidLoad {
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.BimageView];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:CGRectMake(0, 0, 60, 40)];
    button.center = CGPointMake(WIDTH/4, HEIGHT - 40);
    [button setTitle:@"Play" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor colorWithRed:221.0/255 green:57.0/255 blue:57.0/255 alpha:1.0];
    [self.view addSubview:button];
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button1 setFrame:CGRectMake(0, 0, 60, 40)];
    button1.center = CGPointMake(WIDTH/2, HEIGHT - 40);
    [button1 setTitle:@"pause" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside];
    button1.backgroundColor = [UIColor colorWithRed:221.0/255 green:57.0/255 blue:57.0/255 alpha:1.0];
    [self.view addSubview:button1];
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button2 setFrame:CGRectMake(0, 0, 60, 40)];
    button2.center = CGPointMake(WIDTH*3/4, HEIGHT - 40);
    [button2 setTitle:@"stop" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    button2.backgroundColor = [UIColor colorWithRed:221.0/255 green:57.0/255 blue:57.0/255 alpha:1.0];
    [super viewDidLoad];
    [self.view addSubview:self.tabView];
    
    _dataSource = [@[] mutableCopy];
    [[NSArray arrayWithArray:[[NSBundle mainBundle] pathsForResourcesOfType:@"mp3" inDirectory:nil]] enumerateObjectsUsingBlock:^(NSString* _Nonnull musicName, NSUInteger idx, BOOL * _Nonnull stop) {
        [_dataSource addObject:[musicName lastPathComponent]];
    }];
    NSLog(@"%@",_dataSource);
    avAudioPlayer.volume = 1;
    avAudioPlayer.delegate = self;
    avAudioPlayer.numberOfLoops = -1;
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    [session setActive:YES error:nil];
    
}
- (void)play
{
    [avAudioPlayer play];
}
- (void)pause
{
    [avAudioPlayer pause];
}
- (void)stop
{
    avAudioPlayer.currentTime = 0;
    [avAudioPlayer stop];
}
#pragma mark *** delegateANDdatasource***
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"zhaoqian"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"zhaoqian"];
    }
    cell.textLabel.text = _dataSource[indexPath.row];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *path = [[NSBundle mainBundle] pathForAuxiliaryExecutable:_dataSource[indexPath.row]];
    NSURL *url = [NSURL fileURLWithPath:path];
    avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [avAudioPlayer prepareToPlay];
}

-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}
#pragma mark *** lazy loding  ***
-(UIImageView *)BimageView{
    if (!_BimageView) {
        _BimageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
        _BimageView.image = [UIImage imageNamed:@"beijing2.jpg"];
    }
    return _BimageView;
}
-(UITableView *)tabView{
    if (!_tabView) {
        _tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 144) style:UITableViewStylePlain];
        _tabView.delegate = self;
        _tabView.dataSource = self;
        _tabView.backgroundColor = [UIColor clearColor];
    }
    return _tabView;
}

@end



到这里 就能简单的播放音乐了

然后还有一个在mineView中的自定义cell

代码如下

//
//  mineCustomTableViewCell.h
//  ZQMusic
//
//  Created by 赵前 on 16/6/8.
//  Copyright © 2016年 赵前. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface mineCustomTableViewCell : UITableViewCell

@end



//
//  mineCustomTableViewCell.m
//  ZQMusic
//
//  Created by 赵前 on 16/6/8.
//  Copyright © 2016年 赵前. All rights reserved.
//

#import "mineCustomTableViewCell.h"

@implementation mineCustomTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
//    self.backgroundColor = [UIColor colorWithRed:225.0/255 green:161.0/255 blue:198.0/255 alpha:0.6];
    // Configure the view for the selected state
}

@end



这就差不多了

然后下面是我的工程结构图

ios 音乐播放器开发 ios smb 音乐播放器_#import_04

这个playBtnView是我接下来要把播放界面的播放按钮,暂停按钮,和stop按钮重新封装成一个视图,并且重新布局播放界面用的,可以暂时不用管,背景图片和mp3格式的音乐可以自己导入。mp3只要放到工程目录下就可以,背景图片放到工程目录下之后还需要修改imageNamed后面的字符串。


有空继续写了我就会传上来。