2012-07-05 136 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

相關問題