【问题标题】:how to play an audio file which is not in the main bundle?如何播放不在主包中的音频文件?
【发布时间】:2012-11-07 10:21:39
【问题描述】:

这是我从应用程序目录播放 mp3 文件的代码,但由于某种原因它无法正常工作。租赁找出这个代码有什么问题!

   -(IBAction)PlayLesson:(id)sender;
 {
NSString *folderAndFile = @"/Users/alaaalfadhel/Library/Application Support/iPhone        Simulator/5.1/Applications/1021CF5B-F664-4123-B9CB-529217225B74/Documents/file.mp3";
NSString *audioFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,               NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:folderAndFile];
   NSURL *url = [NSURL fileURLWithPath:audioFilePath];
                                         ofType:@"mp3"]];
   AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];

    [click play];
 }   

【问题讨论】:

  • 在调用initWithContentsOfURL: 之前插入NSLog(@"Path: %@, URL: %@", audioFilePath, url);。它打印什么?
  • 路径:/Users/alaaalfadhel/Library/Application Support/iPhone Simulator/5.1/Applications/5515AFBA-1E7D-4B06-A62E-F6FDFD7DD7C7/Documents/Users/alaaalfadhel/Library/Application Support/iPhone Simulator /5.1/Applications/1021CF5B-F664-4123-B9CB-529217225B74/Documents/file.mp3, URL:file://localhost/Users/alaaalfadhel/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/5515AFBA-1E7D -4B06-A62E-F6FDFD7DD7C7/Documents/Users/alaaalfadhel/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/1021CF5B-F664-4123-B9CB-529217225B74/Documents/file.mp3

标签: xcode xcode4 mp3 avplayer


【解决方案1】:

由于 NSSearchPathForDirectoriesInDomains 提供了 Documents 目录中的所有内容,因此您无需为其提供文件夹/文件组合,只需提供文件即可。

您确实希望您的 URL 是 /Users/alaaalfadhel/Library/Application Support/iPhone Simulator/5.1/Applications/5515AFBA-1E7D-4B06-A62E-F6FDFD7DD7C7/Documents/file.m‌​p3 而不是显示在你的日志。这是必要的,因为当您从模拟器中删除应用程序然后再次启动它时,长十六进制值会发生变化。

【讨论】:

  • 如何从我的 URL 中删除文件://localhost/Users
  • 抱歉……应该没问题。我反对的是从 /Users/ 到 /Documents/ 的重复路径部分。
猜你喜欢
  • 2012-02-02
  • 2011-11-09
  • 2013-06-17
  • 1970-01-01
  • 1970-01-01
  • 2011-04-26
  • 1970-01-01
  • 2021-02-07
相关资源
最近更新 更多