Objective C编写音乐播放器

来源:互联网 发布:淘宝详情页950显示 编辑:程序博客网 时间:2024/04/29 15:07

        前段时间因为从手机上传的音乐都是.aac格式的,以前也不知道还有这种格式的音乐,还好mac上有自带的播放器来播放音乐,但是那个播放器确实不是很好用,每个aac文件都需要打开一个播放器,然后手动播放。确实很不好用,抱怨之余,就想着自己来动手编写一个播放器。仅供自己娱乐,肯定会有很多编程不当的地方,也给自己以后写代码一个参考吧。话不多说,开始动手了。

首先新建singleview工程,在viewcontroller里添加相应的按钮(播放,下一首,上一首,单曲循环),tableview用于显示歌曲列表,label用于显示当前的歌曲。界面如下图:

          在ViewController的类里添加tableview的两个代理<UITableViewDataSource,UITableViewDelegate>,然后在viewdidload里设置代理对象为self,注意一定要在viewdidload里设置。代码如下: 

    self.musicListTable.delegate =self;

    self.musicListTable.dataSource =self;

因为在xib中添加的控件都是在viewdidload前创建的,比init早,所以如果在init中设置代理对象的话,那个时候tableview的对象musicListTable还是nil。
   除了tableview所必需的两个代理,还有播放音乐对象的代理<AVAudioPlayerDelegate>后边会介绍相应的代理回调方法。
   设置属性及变量如下:

{

   AVAudioPlayer *_myMusicPlayer;

   NSInteger _currentMusicCount;       // 当前曲目位置

   NSInteger _allSongsNum;             // 所有曲目数

}


@property (strong,nonatomic) NSMutableArray *allSongsPathArray;

@property (strong,nonatomic) IBOutletUIButton *playMusicButton;

@property (strong,nonatomic) IBOutletUILabel  *MusicInfoShow;

@property (strong,nonatomic) IBOutletUITableView *musicListTable;

@property (strong,nonatomic) NSMutableArray *musicNameArray;

@property (strong,nonatomic) IBOutletUISlider *progressSlider;



 接着在ViewController的init方法里写入:

-(id)init

{

    self = [superinit];

    if (self) {

        self.allSongsPathArray  = [[NSMutableArrayalloc] initWithCapacity:0];

        self.musicNameArray = [[NSMutableArrayalloc] initWithCapacity:0];

        _currentMusicCount =0;

    }

    return self;

}


在initmusic的方法里写入:

-(void)initMusic

{

   NSString *path =MUSIC_PATH;  // MUSIC_PATH为音乐文件所在的路径

    NSArray *fileList = [[[NSFileManagerdefaultManager] contentsOfDirectoryAtPath:patherror:nil]

                         

                         pathsMatchingExtensions:[NSArrayarrayWithObjects:@"aac",@"mp3",nil]];

    

    [fileList enumerateObjectsUsingBlock:^(id obj,NSUInteger idx, BOOL *stop) {

        NSString *musicPath = [NSStringstringWithFormat:@"%@/%@",path,(NSString *)obj];

//        [musicPath componentsSeparatedByString:@"/"];

        [self.musicNameArrayaddObject:obj];             // 设置数组存储所有的歌曲名

        [self.allSongsPathArrayaddObject:musicPath];    // 设置数组存储所有的歌曲所在的路径

    }];

    _allSongsNum =self.musicNameArray.count;

}


接下来是加载歌曲的方法

-(void)loadMusic:(NSString*)musicPath

{

    if ([[NSFileManagerdefaultManager] fileExistsAtPath:musicPath])

    {

       NSURL *musicURL = [NSURLfileURLWithPath:musicPath];

       NSError *myError = nil;

       // 创建播放器

       _myMusicPlayer = [[AVAudioPlayeralloc] initWithContentsOfURL:musicURLerror:&myError];

        _myMusicPlayer.delegate =self;


       if (_myMusicPlayer ==nil)

        {

           NSLog(@"error %@",[myErrordescription]);

        }

        

        [_myMusicPlayersetVolume:1];

        _myMusicPlayer.numberOfLoops =0;

        [_myMusicPlayerprepareToPlay];

    }

}



下面分别是播放按键,下一首,上一首按键的响应事件

- (IBAction)play:(id)sender

{

    if(_myMusicPlayer.playing)

    {

        [sendersetTitle:@"Pause"];

        [_myMusicPlayerpause];

    }

   else

    {

        [sendersetTitle:@"Play"];

        [_myMusicPlayerplay];

    }

}


- (IBAction)nextOne:(id)sender

{

    if (_currentMusicCount <_allSongsNum - 2) {

        _currentMusicCount++;

    }

   else

        _currentMusicCount =0;

    

    [selfmusicPlayWithInfoShow];

}


- (IBAction)preforeOne:(id)sender

{

    if(_currentMusicCount >0)

        _currentMusicCount--;

   else

        _currentMusicCount =_allSongsNum - 1;

    

    [selfmusicPlayWithInfoShow];

}


在viewdidload方法里写入

- (void)viewDidLoad {

    [superviewDidLoad];

    [self.navigationControllersetNavigationBarHidden:YES];

    self.musicListTable.delegate =self;

    self.musicListTable.dataSource =self;

    

    self.progressSlider.minimumValue =0;

    self.progressSlider.maximumValue =4.5;

    

    [self.musicListTablereloadData];

    [selfinitMusic];

    self.MusicInfoShow.text = [self.musicNameArrayobjectAtIndex:_currentMusicCount];

    [selfloadMusic:[self.allSongsPathArrayobjectAtIndex:_currentMusicCount]];

    [_myMusicPlayerplay];

}


单曲循环对应的事件

- (IBAction)singleCircleSwitch:(id)sender

{

   if ([sender isOn])

    {

       _myMusicPlayer.numberOfLoops = -1;

    }

   else

    {

        _myMusicPlayer.numberOfLoops =0;

    }

}




因为有很几个方法都需要加载音乐,故而写一个方法,减少胶水代码

-(void)musicPlayWithInfoShow

{

    [selfloadMusic:[self.allSongsPathArrayobjectAtIndex:_currentMusicCount]];

    self.MusicInfoShow.text = [self.musicNameArrayobjectAtIndex:_currentMusicCount];

   NSIndexPath *currentIndexPath = [NSIndexPathindexPathForRow:_currentMusicCountinSection:0];

    [self.musicListTableselectRowAtIndexPath:currentIndexPath animated:YESscrollPosition:UITableViewScrollPositionMiddle];

    [_myMusicPlayerplay];

}



后边就是几个代理回调的方法实现啦

#pragma mark- 

#pragma Table View delegate


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return_allSongsNum;

}


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

{

    staticNSString *CellIdentifier =@"MY_MUSIC_PLAYER_CELL_IDENTIFIER";

    

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   if (cell == nil)

    {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];

    }

    

    cell.textLabel.text = [self.musicNameArrayobjectAtIndex:indexPath.row];

   return cell;

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

   _currentMusicCount = indexPath.row;

    [selfmusicPlayWithInfoShow];

}


- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

   return indexPath;

}


#pragma mark-

#pragma AVAudioPlayer Delegate

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag

{

   if (!flag) {

       return;

    }

    

    if(_currentMusicCount <_allSongsNum - 2)

        _currentMusicCount++;

    

    [selfmusicPlayWithInfoShow];

}


到此整个过程结束了。















0 0