ios-음악 재생기(1)

음악 플레이어
여기에 간단한 음악 플레이어를 만들었는데 데이터는 로컬로 불러와서 간단한 클릭 재생 기능을 실현했다
#import 프레임 이후 지속적으로 보완 음악 플레이어 업데이트
핵심 코드:
- (void) tableView: (UItableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {//음악 재생 QHMusic *music = self.musicArray [indexPath.row];//플레이어 만들기 NSURL *url = [[[NSBundle mainBundle]URLForResource:music.filename withExtension:nil];//전역 변수 AVAudio Player *audio Player = [[[AVAudio Player alloc] initWith Contents OfURL: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

좋은 웹페이지 즐겨찾기