ios-음악 재생기(1)
4442 단어 데이터ios 개발UITableView음악 플레이어컨트롤
여기에 간단한 음악 플레이어를 만들었는데 데이터는 로컬로 불러와서 간단한 클릭 재생 기능을 실현했다
#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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[Core Javascript] JS 분석 from scratch: 데이터, 변수, 메모리 관련 기본지식이런 언어의 기반이 되는 지식을 알아야 나중에 더 능숙하게 다룰 수 있겠다 싶었습니다. 그렇다면 기본형과 참조형 데이터를 구분하는 기준은 무엇일까요? 이것을 이해하기 위해 알아야할 배경지식들이 있습니다. 변수와 식별...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.