如何通过“ didSelectRowAtIndexPath”使用我的应用程序文档目录中的文件?

| 我的appDocument目录中的UITableView中显示了一堆mp3 \。 选择该行时,我希望文件加载到AVAudioPlayer中并播放。 谁能建议我该怎么做? 任何帮助将不胜感激。 谢谢!     
已邀请:
我终于想通了:
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];
}
    

要回复问题请先登录注册