我正在使用iOS 9的XCode 7,试图播放声音。我已经搜索了多天,尝试了各种可能的解决方案,但没有一个是有效的。
这是我的代码:
.h
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController <DTDeviceDelegate, AVAudioPlayerDelegate>
{
}
这是我的.m文件:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"autumn_leaves" ofType:@"m4a"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:&error];
player.numberOfLoops = -1; //Infinite
[player play];
没有声音,没有错误,没有警告,什么都没有。
soundFilePath
和soundFileURL
不是nil
,与播放器一样被填充。我的手机音量已经尽可能地调到最大了。
我还尝试在.m文件中:
@property(strong, nonatomic) AVAudioPlayer *player;
self.myPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:&error];
[self.myPlayer play];
同时没有播放声音也没有错误信息。
这是我的资源文件夹的截图:
请帮忙!