2013-04-26 10 views
6

मैं एक छोटे से ऐप पर काम करता हूं जहां मैं अपने स्थान नियंत्रक से किसी ईवेंट पर ध्वनि फ़ाइल चलाने के लिए चाहता हूं। समस्या तब होती है जब ऐप पृष्ठभूमि मोड में होता है AVAudioPlayer ऑडियो फ़ाइल प्रारंभ नहीं करता है लेकिन कोड निर्धारित होता है, NSLog आउटपुट प्राप्त करता है। एक और बात यह सिम्युलेटर में काम करती है लेकिन फोन पर नहीं।आईओएस: पृष्ठभूमि में ऐप के दौरान ध्वनि चलाएं

Whys नहीं?

- (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__); 
} 

लॉग इम इस हो रही में:

इस फ़ाइल में खेलने के लिए मेरी कोड है।

Apr 26 13:57:48 iPhone CommCenter[58] <Notice>: Client [com.apple.persistentconnection[dataaccessd,85]] is telling PDP context 0 to go active. 
Apr 26 13:57:50 iPhone Herning100[1467] <Warning>: Playing audiofile - filename is Fil03 
Apr 26 13:57:50 iPhone mediaserverd[39] <Warning>: 13:57:50.653 <AudioControl> WARNING translating CMSession error: -12985 
Apr 26 13:57:50 iPhone mediaserverd[39] <Warning>: 13:57:50.656 <AudioControl> WARNING translating CMSession error: -12985 
Apr 26 13:57:50 IPhone mediaserverd[39] <Error>: 13:57:50.734 <AudioControl> AudioQueue: Error -12985 from AudioSessionSetClientPlayState(1467) 
Apr 26 13:57:50 iPhone Herning100[1467] <Warning>: -[AppDelegate tryPlayTaleSound:] - 
Apr 26 13:57:50 iPhone com.apple.debugserver-199[1465] <Warning>: 9 +28.832083 sec [05b9/0303]: error: ::mach_vm_region_recurse (task = 0x1603, address => 0xfffffffe, size => 0, nesting_depth => 1024, info => 0x2fd7fe48, infoCnt => 12) addr = 0xfffffffe err = (os/kern) invalid address (0x00000001) 
Apr 26 13:57:51 iPhone com.apple.debugserver-199[1465] <Warning>: 10 +0.187961 sec [05b9/0303]: error: ::mach_vm_region_recurse (task = 0x1603, address => 0xfffffffe, size => 0, nesting_depth => 1024, info => 0x2fd7fe48, infoCnt => 12) addr = 0xfffffffe err = (os/kern) invalid address (0x00000001) 

वहाँ एक अंतर अगर मैं AVAudioPlayer या AVAudioSession इस्तेमाल होता है।?

उम्मीद है कि कोई मदद कर सकता है।

/लासे

+0

मुझे आशा है कि यह लिंक आपकी मदद कर सकता है। http://stackoverflow.com/questions/6785925/how-to-playing-audio-in-background-mode-when-if-i-click-on-iphone-button-to-min – rir

उत्तर

0

iOS पर केवल आवेदनों में से कुछ पृष्ठभूमि में चलाने की अनुमति है। उनमें से एक ऑडियो प्लेयर एप्लिकेशन है लेकिन उनके लिए आपको "info" (ऐप ऑडियो चला सकता है) के साथ "आवश्यक पृष्ठभूमि मोड" सेट करने की आवश्यकता है।

और हाँ यदि आप पृष्ठभूमि में स्थान प्राप्त करने में सक्षम हैं तो आपको अपनी कुंजी में यह कुंजी सेट करनी होगी। प्लेस्ट में ऑडियो अनुमति भी जोड़ें।

+0

धन्यवाद लेकिन मैं वास्तव में प्लिस्ट फ़ाइल में स्थान और ऑडियो दोनों सेट करें। यदि ऐप चलाने के दौरान कोई ऑडियो प्रारंभ होता है तो ऐप को कम करने के बाद खेलना जारी नहीं रहता है। लेकिन जब यह आवाज पूरी हो जाती है तो यह कभी भी पृष्ठभूमि मोड में नया ऑडियो शुरू नहीं होता है। – lskov

9

में पृष्ठभूमि मोड audio होने अपने Info.plist आप अपने अनुप्रयोग के लिए सही मोड पर साझा ऑडियो सत्र निर्धारित करने की आवश्यकता है और बस पृष्ठभूमि में अपने ध्वनि खेलने से पहले इसे सक्रिय करने के अलावा:

AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
NSError *error = nil; 
NSLog(@"Activating audio session"); 
if (![audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDuckOthers error:&error]) { 
    NSLog(@"Unable to set audio session category: %@", error); 
} 
BOOL result = [audioSession setActive:YES error:&error]; 
if (!result) { 
    NSLog(@"Error activating audio session: %@", error); 
} 
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 

डॉन ' टी ऑडियो सत्र निष्क्रिय करने के लिए जब आप खेल रहे हैं लगता है किया जाता है भूल जाते हैं:

AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
NSError *error = nil; 
NSLog(@"Deactivating audio session"); 
BOOL result = [audioSession setActive:NO error:&error]; 
if (!result) { 
    NSLog(@"Error deactivating audio session: %@", error); 
} 
+0

ओपी: मैं वही कर रहा हूं जैसा आप और यह समाधान पूरी तरह से काम करता है। मेरे पास कुछ अन्य कोड है जिसे मैंने इसमें जोड़ा है, इसलिए यदि यह आपके लिए काम नहीं करता है तो मुझे बताएं और मैं जो कुछ भी कर रहा हूं उसे साझा करने के लिए मैंने जो किया है, उसे पहले से लिखा है। –

+0

धन्यवाद, ऐप पृष्ठभूमि में होने पर मुझे ऑडियो चलाने के लिए "AVAudioSessionCategoryPlayback" श्रेणी को सेट करने की आवश्यकता है। –

0

पृष्ठभूमि में एक ध्वनि खेलने शुरू, एक पृष्ठभूमि कार्य की स्थापना से पहले। ऐसा लगता है कि शेष पृष्ठभूमि समय 10 सेकंड से कम होने पर पृष्ठभूमि में ध्वनि शुरू करने से इंकार कर देता है।

संबंधित मुद्दे