簡體   English   中英

如果我打開蘋果音樂應用程序,則無法在后台模式下播放我的應用程序媒體

[英]my app media play in background mode didn't play if once I open apple music app

我在后台模式下播放媒體時遇到問題。 在我的應用中,當服務器通過套接字連接發送任何通知時,我必須在后台模式下播放媒體播放。 就我而言,媒體播放器在后台模式下正常運行。 問題是當應用程序在后台模式下播放音樂應用程序並停止音樂播放器並將通知發送到我的應用程序時。我的應用程序沒有播放媒體播放器。 我在plist中添加了這個“應用程序使用AirPlay播放音頻或流音頻/視頻”的功能,我正在使用“ AVPlayer”

請幫我。 謝謝。

如果查看AVPlayer文檔,它說一次只能播放一項資產。 我認為Apple Music使用了與API相關的東西(如果不是相同的API)。

AVPlayer旨在一次播放單個媒體資產。 播放器實例可以使用其replaceCurrentItem(with :)方法重用於播放其他媒體資產,但是一次只能管理一個媒體資產的播放。

1)。 將此代碼放入您的AppDelegate.m(didFinishLaunchingWithOptions)方法中:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

2)。 之后,將音頻,Airplay和畫中畫的背景模式設置為活動

在此處輸入圖片說明

可能是由於當前AVplayer運行項目中斷所致。

另外,在將任何項目加載到AVPlayer之前,請務必實現它(以下代碼)。 並且啟用了在后台播放音頻的功能。

NSError *myErr;
[self becomeFirstResponder];
[[UIApplication sharedApplication]beginReceivingRemoteControlEvents];  

AVAudioSession *aSession = [AVAudioSession sharedInstance];

[aSession setCategory:AVAudioSessionCategoryPlayback error:&myErr];

[aSession setMode:AVAudioSessionModeDefault error:&myErr];

[aSession setActive: YES error: &myErr];

實施並添加中斷處理程序,因此您可以在另一個音頻/中斷停止后重新播放已停止的音頻。 並在下面的監聽器中調用。

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleAudioSessionInterruption:)
                                             name:AVAudioSessionInterruptionNotification
                                           object:aSession];

並在其中再次管理AVplayer的播放/暫停:

- (void)handleAudioSessionInterruption:(NSNotification*)notification {

    NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
    NSNumber *interruptionOption = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey];


    switch (interruptionType.unsignedIntegerValue) {
        case AVAudioSessionInterruptionTypeBegan:{
            // • Audio has stopped, already inactive
            // • Change state of UI, etc., to reflect non-playing state
            IsInteruptionOccured=YES;
        } break;
        case AVAudioSessionInterruptionTypeEnded:{
            // • Make session active
            // • Update user interface
            // • AVAudioSessionInterruptionOptionShouldResume option
            IsInteruptionOccured=NO;
            [[AVAudioSession sharedInstance] setActive: YES error: nil];
            if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume) {
                // Here you should continue playback.
                // Resume after exteranl interruption.


                     [_audioPlayer play];


                BOOL isPlayingWithOthers = [[AVAudioSession sharedInstance] isOtherAudioPlaying];
                // test it with...
                NSLog(@"other audio is playing %d",isPlayingWithOthers);
            }
        } break;
        default:
            break;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM