2

我在玩files一些AVAudioPlayer. 当我尝试播放某个 m4a 时,它工作正常。它也适用于我尝试的 mp3。但是mp3,无论我尝试播放它们的顺序如何,它每次都会失败(15 Step,Radiohead)。音频只是没有play,尽管视图加载和同时发生的所有事情都正确发生。代码如下。我得到“播放器加载”。记录其他两首歌曲的输出,但不是 15 Step。我知道这file path是正确的(我在应用程序中早些时候输出了它的日志,它是正确的)。有任何想法吗?

NSData *musicData = [NSData dataWithContentsOfURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[song filename] ofType:nil]]];

NSLog([[NSBundle mainBundle] pathForResource:[song filename] ofType:nil]);
if(musicData)
{
    NSLog(@"File found.");
}

self.songView.player = [[AVAudioPlayer alloc] initWithData:musicData error:nil];

if(self.songView.player) 
{
    NSLog(@"Player loaded.");
}

[self.songView.player play];
NSLog(@"You should be hearing something now.");
4

2 回答 2

1

很抱歉这么晚才参加聚会。只是想发布一个提示,以便将来对任何提及此内容的人有用。

对毛茸茸的青蛙答案的小修正。正确的用法是:

NSError *anError = nil;
anAVAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:someURL] error:&anError];
if (!anAVAudioPlayer) {
    NSLog(@"localizedDescription : %@", [anError localizedDescription]);
}
于 2012-08-09T13:49:47.790 回答
0
NSData *musicData = [NSData dataWithContentsOfURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[song filename] ofType:nil]]];

在这里,是musicData nil针对问题案例吗?

还:

self.songView.player = [[AVAudioPlayer alloc] initWithData:musicData error:nil];

您不应该传递nil接受NSError对象的方法。如果您正确使用它,它可能会更清楚地说明您的问题:

NSError* error = nil;
self.songView.player = [[AVAudioPlayer alloc] initWithData:musicData error:&error];
if (error)
{
    NSLog(@"Error with initWithData: %@", [error localizedDescription]);
}
于 2010-04-12T21:43:35.287 回答