小编lsk*_*kov的帖子

IOS:在应用程序中播放声音

我在一个小应用程序上工作,我希望手机在我的位置控制器的事件上播放声音文件.问题是,当应用程序处于后台模式时,AVAudioPlayer不会启动音频文件,但会调度代码,获取NSLog输出.另一件事是它在模拟器中工作但不在手机上.

不是吗?

这是我播放文件的代码:

- (void)tryPlayTaleSound:(NSString*)fileName {

    NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"];
    NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
    NSError *error;

    backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];

    // Check to see if iPod music is already playing
    //UInt32 propertySize = sizeof(otherMusicIsPlaying);
    //AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &otherMusicIsPlaying);

    // Play the music if no other music is playing and we aren't playing already
    //if (otherMusicIsPlaying != 1 && !backgroundMusicPlaying) {
    //[backgroundMusicPlayer prepareToPlay];
    [backgroundMusicPlayer play];
    backgroundMusicPlaying = YES;
    //}
    NSLog(@"%s - ", __FUNCTION__);
}
Run Code Online (Sandbox Code Playgroud)

在日志我得到这个.

Apr …
Run Code Online (Sandbox Code Playgroud)

iphone avaudioplayer ios avaudiosession

7
推荐指数
2
解决办法
9249
查看次数

标签 统计

avaudioplayer ×1

avaudiosession ×1

ios ×1

iphone ×1