ios- 音乐播放器(1)

88 篇文章 0 订阅
6 篇文章 0 订阅

音乐播放器

这里做一个简单的音乐播放器,数据是加载的本地的 实现了简单的点击播放的功能 

使用了#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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值