2012-07-05 135 views
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); 
} 

回答

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

+0

我只是添加该代码或我需要更多的代码伙伴 – Picm 2012-07-06 09:49:51

+0

感谢伙计! – Picm 2012-07-06 14:03:28

+0

你需要初始化音频播放器,并给它一个源音频文件...像这样: AVAudioPlayer * myPlayer = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@“audioFile “ofType:@”wav“]] error:nil]; – 2012-07-08 18:45:00

相关问题