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

发布于 2024-11-03 20:09:42 字数 148 浏览 1 评论 0原文

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

当我选择该行时,我希望将文件加载到 AVAudioPlayer 中并播放。

谁能建议我如何去做这件事?

任何帮助将不胜感激。

谢谢!

I have a bunch of mp3's displayed in a UITableView which are located in my apps documents directory.

When I select the row, I want the file to load into AVAudioPlayer and Play.

Can anyone suggest how I would go about doing this?

Any help would be greatly appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故笙诉离歌 2024-11-10 20:09:42

我终于明白了这一点:

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];
}

I finally figured this out:

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文