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;
{
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;
}
-(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];
}
- (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];
}
- Objective C编写音乐播放器
- API编写音乐播放器
- Objective-C简单的音乐播放器(边下边播)
- Objective-C简单的音乐播放器(边下边播)
- 音乐播放器 c语言版
- C实现音乐播放器
- 使用FFmpeg编写音乐播放器
- C#编写简单音乐播放器
- Android Broadcast编写的音乐播放器
- java编写的音乐播放器
- 音乐播放器的编写-前台
- 音乐播放器的编写 -Flex
- 56.利用service编写音乐播放器
- 用UWP编写音乐播放器
- pygame+threading编写音乐播放器
- c编写播放器
- C实现简单音乐播放器
- 音乐播放器 c语言版加强版
- 初入软件行业的物联网新人
- andriod SimpleAdapter和ArrayAdapter
- TRIZ系列-创新原理-35-物理或化学参数改变原理
- centos下的hadoop部署(64位)
- NYOJ 399 整除个数
- Objective C编写音乐播放器
- 一句js将博客园文章转成手机/kindle易于阅读样式
- linux内核编译详解
- objective-c 中随机数的用法 (3种:arc4random() 、random()、CCRANDOM_0_1() )
- OGRE1.7.2 + CEGUI0.7.5环境配置 【转载】 需要配制出自己的基础环境
- 彩色图像的直方图均衡化
- Android系统显示原理简介
- 循环控制 while
- POJ1050 HOJ2558 TO the max