我在iphone中没有声音,这是我的代码
NSString *filepath=[[NSBundle mainBundle]pathForResource:@"horror (2)" ofType:@"mp3" ];
NSLog(@"filepath=%@",filepath);
url=[[NSURL alloc]initFileURLWithPath:filepath ];
NSLog(@"url==%@",url);
audioplayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil ];
[self.audioplayer prepareToPlay];
self.audioplayer.currentTime=0;
[self.audioplayer play];
audioplayer.numberOfLoops=-1;
答案 0 :(得分:1)
检查文件是否存在以及AVAudioPlayer提供的错误:
NSString *filepath=[[NSBundle mainBundle]pathForResource:@"horror (2)" ofType:@"mp3" ];
if([[NSFileManager defaultManager] fileExistsAtPath:filepath])
{
url=[[NSURL alloc]initFileURLWithPath:filepath];
NSLog(@"url==%@",url);
if(url)
{
NSError *error;
audioplayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
self.audioplayer.numberOfLoops=-1;
[self.audioplayer prepareToPlay];
if (self.audioPlayer == nil)
NSLog([error description]);
else
[self.audioplayer play];
}
else
{
NSLog(@"URL not created");
}
}
else
{
NSLog(@"File not exists");
}
答案 1 :(得分:0)
检查实例化audioplayer
变量导致的错误:
...
NSError *err;
audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
NSLog(@"ERROR: %@", [err localizedDescription]);
...
这可能会为您提供问题根源的线索。