溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

iOS如何實(shí)現(xiàn)鎖屏頁(yè)面控制音樂(lè)播放

發(fā)布時(shí)間:2021-05-24 13:45:03 來(lái)源:億速云 閱讀:725 作者:小新 欄目:移動(dòng)開(kāi)發(fā)

小編給大家分享一下iOS如何實(shí)現(xiàn)鎖屏頁(yè)面控制音樂(lè)播放,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

本文實(shí)例為大家分享了iOS鎖屏頁(yè)面控制音樂(lè)播放的具體代碼,供大家參考,具體內(nèi)容如下

//1、調(diào)整音頻會(huì)話設(shè)置,確保應(yīng)用進(jìn)入后臺(tái)或靜音開(kāi)關(guān)已開(kāi)啟時(shí)音頻仍將繼續(xù)播放 
//2、鎖屏狀態(tài)下顯示媒體信息 
//3、鎖屏上的空間可以控制音頻播放

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

@interface ViewController ()

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@property (weak, nonatomic) UIButton *playButton;

@end

@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];

 UIButton * playButton = [UIButton buttonWithType:UIButtonTypeSystem];
 playButton.frame = CGRectMake(0, 0, 200, 40);
 playButton.center = self.view.center;
 [playButton setTitle:@"在后臺(tái)播放音頻" forState:UIControlStateNormal];
 [playButton addTarget:self action:@selector(playMusicInBackground:) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:playButton];

 NSError *playerInitError = nil;

 NSString *audioPath =
 [[NSBundle mainBundle] pathForResource:@"background_audio"
         ofType:@"mp3"];

 NSURL *audioURL = [NSURL fileURLWithPath:audioPath];

 self.audioPlayer = [[AVAudioPlayer alloc]
      initWithContentsOfURL:audioURL
      error:&playerInitError];

 AVAudioSession *session = [AVAudioSession sharedInstance];

 NSError *activeError = nil;
 if (![session setActive:YES error:&activeError]) {
  NSLog(@"Failed to set active audio session!");
 }

 //No.1
 //開(kāi)始寫(xiě)代碼,調(diào)整音頻會(huì)話設(shè)置,確保即便應(yīng)用進(jìn)入后臺(tái)或靜音開(kāi)關(guān)已開(kāi)啟,音頻仍將繼續(xù)播放

 NSError *categoryError = nil;
 [session setCategory:AVAudioSessionCategoryPlayback error:&categoryError];


 //end_code


}
- (void)playMusicInBackground:(id)sender {

 if ([self.audioPlayer isPlaying]) {
  [self.audioPlayer stop];

  [self.playButton setTitle:@"正在播放音樂(lè)"
       forState:UIControlStateNormal];

 } else {
  UIImage *lockImage = [UIImage imageNamed:@"belongToMe.jpg"];

  MPMediaItemArtwork *artwork =
  [[MPMediaItemArtwork alloc] initWithImage:lockImage];

  NSDictionary *mediaDict =
  @{
   MPMediaItemPropertyTitle: @"BackgroundTask Audio",
   MPMediaItemPropertyMediaType: @(MPMediaTypeAnyAudio),
   MPMediaItemPropertyPlaybackDuration:
    @(self.audioPlayer.duration),
   MPNowPlayingInfoPropertyPlaybackRate: @1.0,
   MPNowPlayingInfoPropertyElapsedPlaybackTime:
    @(self.audioPlayer.currentTime),
   MPMediaItemPropertyAlbumArtist: @"Some User",
   MPMediaItemPropertyArtist: @"Some User",
   MPMediaItemPropertyArtwork: artwork };

  [self.audioPlayer play];

  [self.playButton setTitle:@"停止播放后臺(tái)音樂(lè)"
       forState:UIControlStateNormal];

  //No.2
  //開(kāi)始寫(xiě)代碼,將媒體信息顯示在鎖定屏幕上,并使鎖屏上控件可以控制音頻播放


  [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];


  //end_code
 }
}

//No.3
//開(kāi)始寫(xiě)代碼,響應(yīng)遠(yuǎn)程控制,使得進(jìn)入鎖屏狀態(tài)后可以控制音樂(lè)“播放”和“暫?!?


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

 if (receivedEvent.type == UIEventTypeRemoteControl) {
  switch (receivedEvent.subtype) {

   case UIEventSubtypeRemoteControlPlay:
    [self.audioPlayer play];
    break;

   case UIEventSubtypeRemoteControlPause:
    [self.audioPlayer pause];
    break;

   default:
    NSLog(@"沒(méi)有處理過(guò)這個(gè)事件------receivedEvent.subtype==%ld",(long)receivedEvent.subtype);
    break;
  }
 }
}


//end_code

- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
}

@end

看完了這篇文章,相信你對(duì)“iOS如何實(shí)現(xiàn)鎖屏頁(yè)面控制音樂(lè)播放”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI