CYC-AVPlayer播放器的簡單運用-02

上一篇中 簡單寫到了關於AVPlayer播放器中 有關單例方面的 東西 單例相對於其他 還是比較方便耐用的 接下來 開始寫大致佈局 以及控制器中的實現

播放列表界面就不提了 大致講下 播放界面的 佈局 在播放界面上 有一個ImageView圖片 圓形狀的 隨着歌曲時間不斷旋轉 劃一下 會出現一個tableView界面 會顯示具體的歌詞

大致上能用到的

#import <UIKit/UIKit.h>
@interface AudioPlayerView : UIView

@property (nonatomic, retain) UIImageView *audioPlayerIamge;

@property (nonatomic, retain) UITableView *geCiView;

@property (nonatomic, retain) UIScrollView *BGScrollView;

@property (nonatomic, retain) UISlider *volumeSlider;

@property (nonatomic, retain) UISlider *shiJianSlider;

@property (nonatomic, retain) UIButton *lastButton;

@property (nonatomic, retain) UIButton *stopButton;

@property (nonatomic, retain) UIButton *nextButton;

@property (nonatomic, retain) UILabel *beginLabel;

@property (nonatomic, retain) UILabel *endLabel;
@end

播放列表 註釋的代碼是可以替換的代碼 那種方式都能實現

#import "FirstTableViewController.h"
#import "FirstTableViewCell.h"
#import "MusicListModel.h"
#import "AudioPlayerViewController.h"

@interface FirstTableViewController ()

@property (nonatomic, retain) NSMutableArray *dataArray;

@end

@implementation FirstTableViewController

- (void)viewDidLoad {

    [self setUpData];
    [super viewDidLoad];
   self.navigationItem.title = @"音樂列表";
}

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


- (void)setUpData
{
    [[MusicListManager shareManager] requestDataForReloadWithBlock:^(NSArray *array) {
        self.dataArray = [NSMutableArray arrayWithArray:array];

        [self.tableView reloadData];

    }];
}




#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[MusicListManager shareManager]getAllDataArrayCount];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *idenitifier = @"MyCell";
    FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idenitifier];
    if (cell == nil) {
        cell = [[FirstTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:idenitifier];
    }

//     MusicListModel *model = [[MusicListManager shareManager] getMusicListModelAtIndex:indexPath.row];

//    MusicListModel *model = self.dataArray[indexPath.row];

//      [cell setModel:model];

      cell.model =  [[MusicListManager shareManager]getMusicListModelAtIndex:indexPath.row];

      return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AudioPlayerViewController *audioPlayerVC = [AudioPlayerViewController shareManager];

//    MusicListModel *model = [[MusicListManager shareManager] getMusicListModelAtIndex:indexPath.row];

//     audioPlayerVC.model = model;
     audioPlayerVC.index = indexPath.row;

    [self.navigationController pushViewController:audioPlayerVC  animated:NO];
}

播放界面的代碼

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>


@interface AudioPlayerViewController : UIViewController<AVAudioPlayerDelegate>


@property (nonatomic, retain) MusicListModel *model;

@property (nonatomic, assign) NSInteger index;

-(NSMutableArray *) returnArray:(MusicListModel *)model;
+ (instancetype)shareManager;

@end

具體實現

#import "AudioPlayerViewController.h"
#import "AudioPlayerView.h"
#import "GeCiTableViewCell.h"
#import "MusicLyricManager.h"

@interface AudioPlayerViewController ()<UITableViewDelegate,UITableViewDataSource,MusicPlayerManagerDelegate>

{
    // 設置接收進來的index 對應取出model
    NSInteger correntIndex;
}

@property (nonatomic, strong) GeCiTableViewCell *cell;

@property (nonatomic, retain) NSMutableArray *musicWordArray;

@end

@implementation AudioPlayerViewController

#pragma mark -  單例初始化視圖控制器

// 單例
+ (instancetype)shareManager
{

    static AudioPlayerViewController  *manager = nil;

    static dispatch_once_t onceToken;

    dispatch_once (&onceToken, ^{

        manager = [[[self class] alloc] init];

    });

    return manager;
}




#pragma mark - 視圖將要出現
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    if (correntIndex == self.index) {
        [self updateUI];
        return;
    } else {
        correntIndex = self.index;
    }
    [self updateUI]; 
}

- (void)viewDidLoad {

    [super viewDidLoad];
    self.navigationItem.title = self.model.name;
    self.view.backgroundColor = [UIColor whiteColor];
    // 遮蓋圖片四周的白色 
    self.edgesForExtendedLayout = UIRectEdgeNone;
    // 代理
    [MusicPlayerManager shareMusicPlayer].delegete = self;
    [self addSubview];
}




#pragma mark - 刷新界面
- (void)updateUI
{
     [[MusicPlayerManager shareMusicPlayer] playingWithUrl:self.model.mp3Url];
#pragma mark - 刷新圖片
     AudioPlayerView *audioView = (AudioPlayerView *)[self.view viewWithTag:100];
     [audioView.audioPlayerIamge sd_setImageWithURL:[NSURL URLWithString:self.model.picUrl]];


#pragma mark - 標題名
    self.navigationItem.title = self.model.name;

//#pragma mark - 刷新歌曲時間
    CGFloat duration = [self.model.duration floatValue]/1000;
    audioView.shiJianSlider.maximumValue = duration;

#pragma mark - 刷新歌詞

    [[MusicLyricManager shareManager] formatLricModelWithLyric:self.model.lyric];
    [audioView.geCiView reloadData];
}

#pragma mark - 獲取當前播放的model
- (MusicListModel*)model
{
    MusicListModel *model = [[MusicListManager shareManager] getMusicListModelAtIndex:correntIndex];

    return model;
}


#pragma mark - 佈置視圖
- (void)addSubview
{
    AudioPlayerView *audioPlayerView = [[AudioPlayerView alloc] init];

    audioPlayerView.tag = 100;
    [audioPlayerView.audioPlayerIamge sd_setImageWithURL:[NSURL URLWithString:self.model.picUrl]];

   audioPlayerView.geCiView.separatorStyle = UITableViewCellSeparatorStyleNone;

    audioPlayerView.geCiView.delegate = self;
    audioPlayerView.geCiView.dataSource = self;


#pragma mark - 兩個Slider 設置點擊方法
   [audioPlayerView.volumeSlider addTarget:self action:@selector(voiceSliderAction:) forControlEvents:(UIControlEventValueChanged)];
    [audioPlayerView.shiJianSlider addTarget:self action:@selector(playSliderAction:) forControlEvents:(UIControlEventValueChanged)];
    audioPlayerView.shiJianSlider.value = 0;

#pragma mark - Button 點擊事件
    [audioPlayerView.lastButton addTarget:self action:@selector(actionShang:) forControlEvents:(UIControlEventTouchUpInside)];

    [audioPlayerView.stopButton addTarget:self action:@selector(actionStop:) forControlEvents:(UIControlEventTouchUpInside)];

    [audioPlayerView.nextButton addTarget:self action:@selector(actionNext:) forControlEvents:(UIControlEventTouchUpInside)];


     self.view = audioPlayerView;

}


#pragma mark - 播放 暫停
- (void)actionStop:(UIButton *)button
{

    BOOL isPlaying = [[MusicPlayerManager shareMusicPlayer] musicPlayOrPause];
    if (isPlaying) {
        [button setTitle:@"暫停" forState:(UIControlStateNormal)];
    } else {
        [button setTitle:@"開始" forState:(UIControlStateNormal)];
    }
}

#pragma mark - 上一首
- (void)actionShang:(UIButton *)button
{
    correntIndex--;
    if (correntIndex < 0) {
        correntIndex = [[MusicListManager shareManager] getAllDataArrayCount] - 1;
    }
    [self updateUI];

}


#pragma mark - 下一首
- (void)actionNext:(UIButton *)button
{
    correntIndex++;
    if (correntIndex > [[MusicListManager  shareManager]getAllDataArrayCount ] - 1) {
        correntIndex = 0;
    }
    [self updateUI];
}

#pragma make - 聲音滑動條

- (void)voiceSliderAction:(UISlider *)sender
{
    [MusicPlayerManager shareMusicPlayer].volum = sender.value;

}

#pragma mark - 快進快退方法
-(void)playSliderAction:(UISlider *)sender
{
    NSLog(@"!!!!!!!%@",sender);
    CGFloat dutation = [self.model.duration floatValue] / 1000;

    if (sender.value >= dutation) {
        return;
    }
    [[MusicPlayerManager shareMusicPlayer] musicSeekToTime:sender.value];
}


#pragma mark - 歌詞界面的tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[MusicLyricManager shareManager] getLyricModelCount];
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *idenitifier = @"GeCiCell";
    self.cell = [tableView dequeueReusableCellWithIdentifier:idenitifier];
    if (self.cell == nil) {
        self.cell = [[GeCiTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:idenitifier];
    }
    self.cell.geCiLabel.text = [NSString stringWithFormat:@"%@", [[MusicLyricManager shareManager]lyricAtIndex:indexPath.row]];

    // 選中
    UIView * selectedView = [[UIView alloc]init];
    selectedView.backgroundColor = [UIColor clearColor];
    self.cell.selectedBackgroundView = selectedView;
    self.cell.highlighted = YES;

    self.cell.geCiLabel.highlightedTextColor = [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1];
    return self.cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 60;
}

#pragma mark - 實現代理方法
- (void)playingWithProgress:(CGFloat)progress
{

    AudioPlayerView *audioView = (AudioPlayerView *)[self.view viewWithTag:100];

    // 圖片旋轉
    audioView.audioPlayerIamge.transform = CGAffineTransformRotate(audioView.audioPlayerIamge.transform, M_1_PI / 180);
    // 進度條
    audioView.shiJianSlider.value = progress;

    // 當前播放時間
    audioView.beginLabel.text = [self changeFormatWithTime:progress];

    // 剩餘時間
    CGFloat duration = [[self.model duration] floatValue] / 1000;
    audioView.endLabel.text = [self changeFormatWithTime:(duration - progress)];

    // 獲取當前進度對應的下標
    NSInteger index = [[MusicLyricManager shareManager] indexOfTime:progress];
    // 組拼 indexPath
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];

    [audioView.geCiView selectRowAtIndexPath:indexPath animated:YES scrollPosition:(UITableViewScrollPositionMiddle)];


}

#pragma maek - 轉換時間格式
- (NSString *)changeFormatWithTime:(CGFloat)time
{
    // 計算分鐘
    int minute = time / 60;
    // 計算秒
    int  secondes = (int) time % 60;
    NSString *timeFormat = [NSString stringWithFormat:@"%02d:%02d", minute, secondes];
    return timeFormat;
}

小貼士

// imageView設置成正方形 通過下面的代碼 可以讓他變成圓形
 self.audioPlayerIamge.layer.cornerRadius = self.audioPlayerIamge.frame.size.width / 2;
 self.audioPlayerIamge.layer.masksToBounds = YES;

效果圖
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章