ios开发:一个音乐播放器的设计与实现案例

2020-01-18 16:44:35丽君

需要注意的是,单例要在并发的条件下测试,我采用的是dispatch_group,主要是考虑到,必须要等待所有并发结束才能比较结果,否则可能会出错。比如说,并发条件下,x线程已经执行完毕了,它所对应的a对象已有值;而y线程还没开始初始化,它所对应的b对象还是为nil,为了避免这种条件的产生,我采用dispatch_group来等待所有并发结束,再去做相应的判断。

首页控制器的代码:


 #import "ZYMusicViewController.h"
#import "ZYPlayingViewController.h"
#import "ZYMusicTool.h"
#import "ZYMusic.h"
#import "ZYMusicCell.h"
 
@interface ZYMusicViewController ()
@property (nonatomic, strong) ZYPlayingViewController *playingVc;
 
@property (nonatomic, assign) int currentIndex;
@end
 
@implementation ZYMusicViewController
 
- (ZYPlayingViewController *)playingVc
{
  if (_playingVc == nil) {
    _playingVc = [[ZYPlayingViewController alloc] initWithNibName:@"ZYPlayingViewController" bundle:nil];
  }
  return _playingVc;
}
 
- (void)viewDidLoad {
  [super viewDidLoad];
   
  [self setupNavigation];
}
 
- (void)setupNavigation
{
  self.navigationItem.title = @"音乐播放器";
}
 
#pragma mark ----TableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 
  return 1;
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return [ZYMusicTool musics].count;
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  ZYMusicCell *cell = [ZYMusicCell musicCellWithTableView:tableView];
  cell.music = [ZYMusicTool musics][indexPath.row];
  return cell;
}
 
#pragma mark ----TableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  return 70;
}
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  [tableView deselectRowAtIndexPath:indexPath animated:YES];
   
  [ZYMusicTool setPlayingMusic:[ZYMusicTool musics][indexPath.row]];
   
  ZYMusic *preMusic = [ZYMusicTool musics][self.currentIndex];
  preMusic.playing = NO;
  ZYMusic *music = [ZYMusicTool musics][indexPath.row];
  music.playing = YES;
  NSArray *indexPaths = @[
              [NSIndexPath indexPathForItem:self.currentIndex inSection:0],
              indexPath
              ];
  [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
   
  self.currentIndex = (int)indexPath.row;
   
  [self.playingVc show];
}
 
@end 

重点需要说说的是这个界面的实现:
ios音乐播放器,ios音乐播放器代码,ios音乐播放器开发
 这里做了比较多的细节控制,具体在代码里面有相应的描述。主要是想说说,在实现播放进度拖拽中遇到的问题。