2011-12-30 36 views
6

Tôi đang phát nhạc bằng AVAudioPlayer ở chế độ nền. Vấn đề là: nếu có một cuộc gọi đến làm gián đoạn trình phát, nó sẽ không bao giờ tiếp tục trừ khi chuyển sang tiền cảnh và thực hiện thủ công.Cách tiếp tục AVAudioPlayer sau khi bị gián đoạn trong nền

Mã này rất đơn giản, để chơi nó ở chế độ nền:

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil]; 
[[AVAudioSession sharedInstance] setActive: YES error: nil]; 

url = [[NSURL alloc] initFileURLWithPath:...]; 

audio_player = [[AVAudioPlayer alloc] initWithContentsOfURL: url error:NULL]; 
audio_player.delegate = self; 
bool ret = [audio_player play]; 

đại biểu để xử lý gián đoạn:

-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player 
{ 
    //tried this, not working [[AVAudioSession sharedInstance] setActive: NO error: nil]; 
    NSLog(@"-- interrupted --"); 
} 


//----------- THIS PART NOT WORKING WHEN RUNNING IN BACKGROUND ---------- 
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player 
{ 
    NSLog(@"resume!"); 
    //--- tried, not working: [[AVAudioSession sharedInstance] setCategory:    AVAudioSessionCategoryPlayAndRecord error: nil]; 
    //--- tried, not working: [[AVAudioSession sharedInstance] setActive: YES error: nil]; 
    //--- tried, not working: [audio_player prepareToPlay]; 
    [audio_player play]; 
} 

Bất kỳ một thể giúp tôi?

+0

Cùng một pb, bạn có tìm thấy giải pháp không? – fvisticot

+1

cho đến thời điểm này, không. Nhưng có một giải pháp. Ứng dụng cửa hàng Apple mẫu đã làm điều đó. – Paul

Trả lời

0

Hãy thử điều này

-(void)audioPlayerEndInterruption:(AVAudioPlayer *)audioPlayer withFlags:(NSUInteger)flags{ 

    if (flags == AVAudioSessionFlags_ResumePlay) { 
     [audioPlayer play]; 
    }  

Hy vọng nó giúp.

+1

không hoạt động ở chế độ nền. – Paul

8

Tìm thấy giải pháp! Tôi gặp sự cố tương tự, ứng dụng của tôi đã tiếp tục phát lại âm thanh độc đáo sau một gián đoạn, chỉ khi ứng dụng của tôi mở. Khi nó được trên nền nó không thành công ro tiếp tục phát âm thanh sau khi bị gián đoạn.

Tôi đã sửa lỗi này bằng cách thêm các dòng mã sau đây:

Thêm dòng này bất cứ khi nào ứng dụng của bạn bắt đầu phát âm thanh. [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

Và trên phương thức kết thúc, đợi 1 hoặc 2 giây trước khi tiếp tục phát âm thanh. Điều này cho phép hệ điều hành ngừng sử dụng kênh âm thanh.

- (void)endInterruptionWithFlags:(NSUInteger)flags { 
    // Validate if there are flags available. 
    if (flags) { 
     // Validate if the audio session is active and immediately ready to be used. 
     if (AVAudioSessionInterruptionFlags_ShouldResume) { 
       dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1), dispatch_get_main_queue(), ^{ 
        // Resume playing the audio. 
       }); 
     } 
    } 
} 

Bạn cũng có thể thêm dòng này khi ứng dụng của bạn dừng (không tạm dừng) phát âm thanh. Nhưng không bắt buộc. [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

+0

Tôi có nên thay đổi dòng này nếu (AVAudioSessionInterruptionFlags_ShouldResume) thành if (flags == AVAudioSessionInterruptionFlags_ShouldResume) – jailani

+0

Hoạt động tốt paul cảm ơn u rất nhiều ..... – jailani

+0

Việc sử dụng giao thức AVAudioSessionDelegate không được chấp nhận trong iOS 6 –

Các vấn đề liên quan