如何使用“didSelectRowAtIndexPath”从我的apps文档目录中使用文件?

时间:2011-04-29 19:01:50

标签: cocoa-touch avaudioplayer didselectrowatindexpath

我在UITableView中显示了一堆mp3,它们位于我的apps文档目录中。

当我选择行时,我希望将文件加载到AVAudioPlayer和Play中。

有人可以建议我这样做吗?

非常感谢任何帮助。

谢谢!

1 个答案:

答案 0 :(得分:1)

我终于明白了:

NSArray *songs;
AVAudioPlayer *player;

- (void)viewDidLoad
{
  [self.player prepareToPlay];

  // Point to Document directory
  NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  NSError *error = nil;
  NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];

  if (array == nil) {
    // Handle the error
  }
  self.songs = array;
  //[array release];

  //self.title = @"Song List";
  [super viewDidLoad];
  // Do any additional setup after loading the view from its nib.
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
  NSString *filePath = [applicationDocumentsDirectory stringByAppendingPathComponent: [songs objectAtIndex:indexPath.row]];
  NSURL *url = [NSURL fileURLWithPath:filePath];

  player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];

  [player play];
}