iOS音訊後台播放 鎖屏

來源:互聯網
上載者:User

標籤:

初始化AudioSession和基本配置

      音頻播放器採用的AVPlayer ,在程式啟動的時候需要配置AudioSession,AudioSession負責應用音訊設定,比如支不支援後台,打斷等等,這一步很重要,比如在viewdidload裡初始化AVplayer以後要調用下面的函數:

/** 設定音頻會話 */  //這種方式後台,可以連續播放非網路請求歌曲,遇到網路請求歌曲就廢,需要後台申請task

-(void)setAudioSession{

    AVAudioSession *audioSession=[AVAudioSession sharedInstance];

    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

    [audioSession setActive:YES error:nil];

}

除了代碼的初始化,很重要的一步是對info-plist的設定,讓應用支援音訊後台播放

 

庫的引入包括:

AudioToolBox.framework

MediaPlayer.framework

CoreMedia.framework

AVFoundation.framework

 

Remote控制

在播放視圖的ViewController裡加上這兩個函數:

- (void)viewDidAppear:(BOOL)animated {

[super viewDidAppear:animated];

 

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

[self becomeFirstResponder];

}

- (void)viewWillDisappear:(BOOL)animated {

[super viewWillDisappear:animated];

[[UIApplication sharedApplication] endReceivingRemoteControlEvents];

[self resignFirstResponder];

}

當然也可以同理放到delegate.m裡面的進入後台和回到前台的函數中,否則的話,上面的代碼只是允許當前視圖的情況下進入後台可以Remote控制

在AppDelegate裡要申請背景工作來進行處理

- (void)applicationDidEnterBackground:(UIApplication *)application {

[application beginReceivingRemoteControlEvents];
}

 

在添加遠端控制代碼:

 

-(void)remoteControlReceivedWithEvent:(UIEvent *)event{

    

    //if it is a remote control event handle it correctly

    

    if (event.type == UIEventTypeRemoteControl) {

        

        if (event.subtype == UIEventSubtypeRemoteControlPlay) {

            

            [self playBarSelector:self.mPlayButton];

            

        }if (event.subtype == UIEventSubtypeRemoteControlPause) {

            

            [self playBarSelector:self.mPlayButton];

            

        } else if (event.subtype == UIEventSubtypeRemoteControlNextTrack){

            

            [self playBarSelector:self.mNextButton];

          

            [self configNowPlayingInfoCenter];

            

        }else if (event.subtype == UIEventSubtypeRemoteControlPreviousTrack){

            

            [self playBarSelector:self.mUpwardButton];

           

            

            [self configNowPlayingInfoCenter];

        }

         }

}

 

最後切換上一首和下一首要更新鎖定屏資訊,重新調一下configNowPlayingInfoCenter方法

- (void)configNowPlayingInfoCenter {

    

    if (NSClassFromString(@"MPNowPlayingInfoCenter")) {

        

        // 1.播放資訊中心

        MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];

        

        // 2.初始化播放資訊

        NSMutableDictionary *info = [NSMutableDictionary dictionary];

        // 專輯名稱

        info[MPMediaItemPropertyAlbumTitle] = self.operatorObject.mExerciseText;

        // 歌手

        info[MPMediaItemPropertyArtist] = @"雅思聽聽小組";

        // 歌曲名稱

        info[MPMediaItemPropertyTitle] = [NSString stringWithFormat:@"%@ - %@", self.operatorObject.mTextName, [self.operatorObject.mTitle substringToIndex:9]];

        // 設定圖片

        info[MPMediaItemPropertyArtwork] = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"Default"]];

        // 設定期間(歌曲的總時間)

        info[MPMediaItemPropertyPlaybackDuration] = @(self.mAudioPlayerLong);

        // 設定當前播放進度

        info[MPNowPlayingInfoPropertyElapsedPlaybackTime] = @(self.mPlayerCurrentTime);

        

        // 3.切換播放資訊

        center.nowPlayingInfo = info;

 

    }

    

}

 

 

iOS音訊後台播放 鎖屏

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.