在iOS7中播放声音似乎不起作用

时间:2021-01-12 15:23:15

Im having a little problem playing an audio file in iOS7. The audio file doesn't get played because there is no sound when this code is called:

在iOS7中播放音频文件有点问题。音频文件不会播放,因为当这个代码被调用时没有声音:

NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"PistolShootSound" ofType:@"mp3"]];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[click setVolume:1.0];
[click play];

But no sound is coming out :(

但是没有声音出来:

I have checked the sound on my computer and it is working, I have rechecked the "Play user interface sound effects" option in system preferences but with no use. I have tried opening an AVAudioSession like this:

我检查了我电脑上的声音,它正在工作,我重新检查了“播放用户界面声音效果”选项在系统首选项中,但没有使用。我试过这样开一个AVAudioSession:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];

But that didn't work either.

但这也不管用。

Is there another way to play a sound in iOS7? or doesn't the simulator play sounds anymore?

在iOS7中还有其他播放声音的方式吗?或者模拟器不再播放声音了?

1 个解决方案

#1


20  

Try this..

试试这个. .

@property (nonatomic,strong) AVAudioPlayer *click;

@synthesize click;

NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"PistolShootSound" ofType:@"mp3"]];
click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[click setVolume:1.0];
[click preparetoPlay];
[click play];

#1


20  

Try this..

试试这个. .

@property (nonatomic,strong) AVAudioPlayer *click;

@synthesize click;

NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"PistolShootSound" ofType:@"mp3"]];
click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[click setVolume:1.0];
[click preparetoPlay];
[click play];