【问题标题】:Not hearing sound from AVAudioPlayer?听不到 AVAudioPlayer 的声音?
【发布时间】:2016-01-02 20:31:28
【问题描述】:

无法弄清楚这里出了什么问题,或者我听不到什么声音。它构建良好,没有错误。

从我所读的内容来看,它与 ARC.... 有关? 但是当我查看“构建设置:Apple LLVM 7.0:ARC”时,ARC 被列为“NO”


.h

@property (nonatomic, strong) AVAudioPlayer *player;

.m

@synthesize player;

...

NSURL *soundFileURL = [[NSBundle mainBundle] URLForResource:@"abc" withExtension:@"m4a"];

player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //Infinite

[player play];

这行得通:

AudioServicesPlaySystemSound(1005);

试试这个:(ARC __bridge modifiers demystified)

SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"m4a"];
NSLog(@"Filename:  %@", soundFile);
AudioServicesCreateSystemSoundID((__bridge  CFURLRef) [NSURL fileURLWithPath:soundFile], & soundID);
AudioServicesPlaySystemSound(soundID);

这会崩溃:* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* -[NSURL initFileURLWithPath:]: nil string parameter”
...所以看我看不到文件名...文件名:(空)

【问题讨论】:

  • 尝试这样获取声音的路径url: NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"m4a"]; NSURL * soundFileURL = [NSURL fileURLWithPath:soundFilePath];
  • @azimov 为什么? OP获取URL的代码很好。
  • 如何以及在哪里声明播放器。您必须在使用时保留播放器。例如通过强大的属性
  • 在财产方面做了“强”,但什么也没做。也许暂停一个线程? ... 试过了,什么都没有。

标签: objective-c iphone avaudioplayer


【解决方案1】:

不得不去:

Project:
Targets:
Build Phases:
Copy Bundle Resources: and add the abc.m4a file to it.

我需要的其他文件都在那里,除了那个。

更多信息:NSBundle pathForResource is NULL

【讨论】:

  • 一些简单的调试会告诉你soundFileURL 在你的原始代码中是nil。这也意味着player 也可能是nil。使用 error 参数也会有所帮助。
猜你喜欢
  • 2015-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多