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