音乐播放器
这里做一个简单的音乐播放器,数据是加载的本地的 实现了简单的点击播放的功能
使用了#import <AVFoundation/AVFoundation.h> 框架 之后会持续的完善更新音乐播放器
核心代码:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//播放音乐
QHMusic *music = self.musicArray[indexPath.row];
//创建播放器
NSURL *url = [[NSBundle mainBundle]URLForResource:music.filename withExtension:nil];
//注意这里必须使用全局变量
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
//缓冲(以便后面播放比较流畅)
[audioPlayer prepareToPlay];
//开始播放
[audioPlayer play];
self.audioPlayer = audioPlayer;
}
主要代码
数据模型
#import <Foundation/Foundation.h>
@interface QHMusic : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *filename;
@property(nonatomic,copy)NSString *singer;
@property(nonatomic,copy)NSString *singerIcon;
@property(nonatomic,copy)NSString *icon;
+(id)musicWithDict:(NSDictionary *)dict;
-(id)initWithDict:(NSDictionary *)dict;
@end
#import "QHMusicViewController.h"
#import "QHMusic.h"
#import <AVFoundation/AVFoundation.h>
@interface QHMusicViewController ()
@property(nonatomic,strong)NSArray *musicArray;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)AVAudioPlayer *audioPlayer;
@end
@implementation QHMusicViewController
-(NSArray *)musicArray
{
if (!_musicArray) {
NSString *path = [[NSBundle mainBundle]pathForResource:@"Musics.plist" ofType:nil];
NSArray *array = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *objs = [NSMutableArray array];
for(NSDictionary * dict in array)
{
QHMusic *music = [QHMusic musicWithDict:dict];
[objs addObject:music];
}
_musicArray = objs;
}
return _musicArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
// Return the number of rows in the section.
return self.musicArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
QHMusic *music = self.musicArray[indexPath.row];
static NSString *cellName = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName];
}
cell.imageView.image = [UIImage imageNamed:music.singerIcon];
cell.textLabel.text = music.name;
cell.detailTextLabel.text = music.singer;
NSLog(@"%@",music.singer);
NSLog(@"%@",cell.detailTextLabel.text);
// Configure the cell...
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
-(BOOL)prefersStatusBarHidden
{
return YES;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//播放音乐
QHMusic *music = self.musicArray[indexPath.row];
//创建播放器
NSURL *url = [[NSBundle mainBundle]URLForResource:music.filename withExtension:nil];
//注意这里必须使用全局变量
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
//缓冲(以便后面播放比较流畅)
[audioPlayer prepareToPlay];
//开始播放
[audioPlayer play];
self.audioPlayer = audioPlayer;
}
@end
#import "QHMusic.h"
@implementation QHMusic
+(id)musicWithDict:(NSDictionary *)dict
{
return [[self alloc]initWithDict:dict];
}
-(id)initWithDict:(NSDictionary *)dict{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
@end