0

我最近一直在尝试让我的应用程序从打开到关闭时播放声音,但我遇到了困难。当我按下按钮时,我有一些关于播放声音的代码,这很好并制作我玩游戏的声音循环。

。H

#import <AudioToolbox/AudioToolbox.h>


    - (IBAction)t1:(id)sender;

.m

- (IBAction)t1:(id)sender {

    CFBundleRef mainbundle = CFBundleGetMainBundle();
    CFURLRef soundFileUrlRef;
    soundFileUrlRef = CFBundleCopyResourceURL(mainbundle, (CFStringRef) @"t1", CFSTR ("wav"), NULL); 
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileUrlRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
}
4

1 回答 1

1

如何尝试 AVAudioPlayer 并通过将循环数设置为 -1 来打开连续播放:

    myPlayer.numberOfLoops = -1

    [myPlayer prepareToPlay];

    [myPlayer play];


    //when application did enter background or something
    [myPlayer stop];

http://developer.apple.com/library/ios/#DOCUMENTATION/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html

还要确保为后台播放正确设置了音频会话:http: //developer.apple.com/library/ios/#DOCUMENTATION/Audio/Conceptual/AudioSessionProgrammingGuide/Basics/Basics.html#//apple_ref/doc/uid/ TP40007875-CH2-SW2

于 2012-07-05T17:43:48.013 回答