iPhone中播放指定路径的MP3等音频文件
来源:互联网 发布:淘宝店铺租赁代运营 编辑:程序博客网 时间:2024/05/16 01:06
在指定的目录获取所有的MP3等音频文件, 然后使用SDK中AVAudioPlayer进行播放控制, 代码示例如下
首先取得指定路径的所有mp3文件
musicArray = [[NSMutableArray alloc]init]; //资源目录 NSString *resoucePath = [[NSBundle mainBundle]resourcePath]; NSLog(@"resoucePath:%@",resoucePath); //取出资源目录下所有mp3文件// NSArray *fileArray = [[NSBundle mainBundle]pathsForResourcesOfType:@"mp3" inDirectory:resoucePath]; NSArray *mp3Array = [NSBundle pathsForResourcesOfType:@"mp3" inDirectory:[[NSBundle mainBundle]resourcePath]]; for (NSString *filePath in mp3Array) { //NSLog(@"filePath:%@",filePath); //fileURLWithPath是初始化文件路径url得,urlWithPath是初始化网络链接得 NSURL *fileURL = [NSURL fileURLWithPath:filePath]; //初始化urlAsset,options中可以指定要求准确播放时长 AVURLAsset *avURLAsset = [AVURLAsset URLAssetWithURL:fileURL options:nil]; [musicArray addObject:avURLAsset]; }
再取出专辑封面等信息,列表展示出播放曲目
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *musicCell = [DataTable dequeueReusableCellWithIdentifier:@"musicCell"]; if (musicCell==nil) { musicCell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"musicCell"]autorelease]; } //取出每一行对应得AVURLAsset AVURLAsset *mp3Asset = [musicArray objectAtIndex:indexPath.row]; NSLog(@"mp3Asset:%@",[[[mp3Asset URL]absoluteString]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]); //取出url并取消百分号utf8转码 NSString *mp3FilePath = [[[mp3Asset URL]absoluteString]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //取最后一个/后面得路径 NSString *musicTitle = [mp3FilePath lastPathComponent]; //去掉.mp3得到歌曲名称 musicTitle = [musicTitle substringToIndex:[musicTitle rangeOfString:@"."].location]; musicCell.textLabel.text = musicTitle; for (NSString *format in [mp3Asset availableMetadataFormats]) { NSLog(@"-------format:%@",format); for (AVMetadataItem *metadataItem in [mp3Asset metadataForFormat:format]) { NSLog(@"commonKey:%@",metadataItem.commonKey); if ([metadataItem.commonKey isEqualToString:@"artwork"]) { //取出封面artwork,从data转成image显示 musicCell.imageView.image = [UIImage imageWithData:[(NSDictionary*)metadataItem.value objectForKey:@"data"]]; }// if ([metadataItem.commonKey isEqualToString:@"title"]) {//// NSString *titleStr = [NSString stringWithCString:(const char *)metadataItem.value encoding:NSASCIIStringEncoding];// CFStringRef titleStrRef = CFStringCreateWithCString(CFAllocatorGetDefault(), (const char *)metadataItem.value, kCFStringEncodingASCII);// musicCell.textLabel.text = (NSString*)titleStrRef;// } } } return musicCell;}
最后,点击某首歌曲进行播放
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:musicURL error:NULL]; [audioPlayer play];
PS: 本文中示例源代码来源如下:https://github.com/yuyi012/AVFoundationDemo2/downloads
- iPhone中播放指定路径的MP3等音频文件
- iphone利用AudioQueue播放音频文件(mp3,aac,caf,wav等)
- iphone利用AudioQueue播放音频文件(mp3,aac,caf,wav等)
- iphone利用AudioQueue播放音频文件(mp3,aac,caf,wav等)
- iphone利用AudioQueue播放音频文件(mp3,aac,caf,wav等)
- iphone利用AudioQueue播放音频文件(mp3,aac,caf,wav等)
- C#中用API实现MP3等音频文件的播放类
- C#中用API实现MP3等音频文件的播放类
- C#中用API实现MP3等音频文件的播放类
- symbian中打开wav,mp3等音频文件
- 使用API函数播放MP3等音频文件的类设计(C#)
- 使用API函数播放MP3等音频文件的类设计(C#)
- 使用API函数播放MP3等音频文件的类设计(C#)
- 使用API函数播放MP3等音频文件的类设计(C#)
- (转)C#中用API实现MP3等音频文件的播放类
- C#获取音频文件(MP3等)播放时间长度的几种方式
- 在LINUX下怎么播放MP3等音频文件啊?
- iOS利用AudioQueue播放音频文件(mp3,aac,caf,wav等)
- 用Eclipse开发android原码 .
- 编程珠玑 12 取样问题
- FND_FLEX_VALUES_VL--请记得此表以VL结尾表示是多语言表描述在不同环境下是有可能不一致的
- linux开发更改环境变量
- itoa以及atoi C实现
- iPhone中播放指定路径的MP3等音频文件
- JS控制HTML全选和反选
- SQL Server FOR XML PATH 语句的应用
- 一个关于设计模式的网站。喜欢的大家可以上去看看
- 解包、编辑、打包boot.img文件
- 展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告
- python vs pythonw
- poj 1830 异或方程组
- 使用Settings Bundle为程序添加设置项