2013-04-26 42 views
6

Tôi đang làm việc trên một ứng dụng nhỏ mà tôi muốn điện thoại phát tệp âm thanh trên sự kiện từ bộ điều khiển vị trí của tôi. Vấn đề là khi Ứng dụng ở chế độ Nền, AVAudioPlayer không khởi động tệp Âm thanh nhưng mã được lên lịch, nhận đầu ra NSLog. Một điều khác là nó hoạt động trong trình mô phỏng nhưng không phải trên điện thoại.IOS: Phát âm thanh trong khi ứng dụng ở chế độ nền

Whys not?

Đây là mã của tôi để chơi các tập tin:

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

Trong nhật ký Im nhận này.

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) 

Có sự khác biệt nào nếu tôi sử dụng AVAudioPlayer hoặc AVAudioSession.?

Hy vọng ai đó có thể trợ giúp.

/Lasse

+0

Tôi hy vọng liên kết này có thể giúp bạn. http://stackoverflow.com/questions/6785925/how-to-playing-audio-in-background-mode-when-if-i-click-on-iphone-button-to-min – rir

Trả lời

0

Trên iOS chỉ một số ứng dụng được phép chạy ẩn. Một trong số đó là ứng dụng Trình phát âm thanh nhưng đối với họ, bạn cần đặt "chế độ nền bắt buộc" bằng "âm thanh" (Ứng dụng có thể phát âm thanh) trong Info.plist của bạn.

Và có nếu bạn có thể nhận Vị trí trong nền thì bạn phải đặt khóa này trong Plist của mình. Thêm cũng cho phép âm thanh trong plist.

+0

Cảm ơn nhưng tôi thực sự có cả Vị trí và Âm thanh được đặt trong tệp Plist. Nếu âm thanh được khởi động trong khi ứng dụng chạy thì không tiếp tục phát sau khi tôi thu nhỏ ứng dụng. Nhưng khi âm thanh này được thực hiện nó không bao giờ bắt đầu âm thanh mới trong chế độ nền. – lskov

9

Ngoài việc có chế độ nền audio trong Info.plist của bạn, bạn cần phải thiết lập phiên âm thanh chia sẻ cho các chế độ chính xác cho ứng dụng của bạn và kích hoạt nó ngay trước khi chơi âm thanh của bạn ở chế độ nền:

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]; 

Don' Đừng quên tắt phiên âm thanh khi bạn phát xong âm thanh:

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

OP: Tôi đang làm điều tương tự như bạn và giải pháp này hoạt động hoàn hảo. Tôi có một số mã khác mà tôi thêm vào này, vì vậy nếu điều này không làm việc cho bạn cho tôi biết và tôi có thể chia sẻ những gì tôi đã làm để làm cho nó làm việc ontop của những gì glebd đã viết. –

+0

cảm ơn, tôi cần đặt danh mục thành "AVAudioSessionCategoryPlayback" để phát âm thanh là khi ứng dụng ở chế độ nền. –

0

Trước khi bắt đầu phát âm thanh ẩn, hãy thiết lập tác vụ nền. Dường như iOS từ chối khởi động âm thanh trong nền nếu thời gian nền còn lại nhỏ hơn 10 giây.