1. 程式人生 > >iPhone中播放指定路徑的MP3等音訊檔案

iPhone中播放指定路徑的MP3等音訊檔案

          在指定的目錄獲取所有的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