- vừa được xem lúc

MPNowPlayingInfoCenter - Điều khiển trình phát nhạc ở màn hình Lock

0 0 15

Người đăng: 漢輝

Theo Viblo Asia

Khi bạn làm một ứng dụng nghe nhạc, chắc chắn bạn đâu muốn lúc nào cũng mở ứng dụng nghe nhạc ở ngay màn hình chính đâu. Đôi lúc bạn sẽ muốn khoá điện thoại chỉ để nghe nhạc, nhưng bạn vẫn muốn điều khiển được trình nghe nhạc như nghe bản nhạc tiếp theo, bản nhạc trước hoặc một chút thông tin ...

Âm nhạc

Âm nhạc là thứ mang lại vẻ đẹp dưới dạng nụ cười trên khuôn mặt. Một trong những ứng dụng được sử dụng nhiều nhất và thịnh hành nhất là ứng dụng âm nhạc. Mỗi người trong chúng ta đều có một hoặc ứng dụng âm nhạc khác trong điện thoại của mình để giúp chúng ta xả stress. ?

Chủ đề

Bây giờ mình sẽ hướng dẫn làm thể nào để điều khiển ở trình phát nhạc trên màn hình khoá.

Điều kiện cần và đủ để áp dụng vào ứng dụng

Để phát nhạc và mong muốn điều khiển thì ứng dụng iOS của bạn cần những điều sau:

  1. Media playback
  2. Remote commands configuration
  3. Playing info center configuration

1. Hỗ trợ phát nhạc ở nền

Để khi ứng dụng iOS ở nền vẫn có thể phát nhạc được, bạn cần đoạn code đăng kí như sau:

do { //keep alive audio at background try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category(rawValue: AVAudioSession.Category.playback.rawValue)) try AVAudioSession.sharedInstance().setActive(true)
} catch _ { }

Bật Audio ở Background Modes

2. Remote Commands

MPNowPlayingInfoCenter là một class cho phép tương tác giữa ứng dụng và remote control ở màn hình khoá. Class này cung cấp MPRemoteCommand để handle trong ứng dụng của mình.

  • Trước tiên đăng kí Remote này ở ứng dụng bằng câu lệnh sau

UIApplication.shared.beginReceivingRemoteControlEvents()

  • Các hàm handle mà bạn mong muốn
func addActionsToControlCenter(){ addActionToPlayCommand() addActionToPauseCommnd() addActionToPreviousCommand() addActionToNextCommand() addActionToChangePlayBackPosition() addActionToseekForwardCommand() addActionToseekBackwordCommand()
} func addActionToPlayCommand(){ MPRemoteCommandCenter.shared().playCommand.isEnabled = true MPRemoteCommandCenter.shared().playCommand.addTarget(self, action: #selector(playCommand))
} func addActionToPauseCommnd(){ MPRemoteCommandCenter.shared().pauseCommand.isEnabled = true MPRemoteCommandCenter.shared().pauseCommand.addTarget(self, action: #selector(pauseCommand))
} func addActionToPreviousCommand(){ MPRemoteCommandCenter.shared().previousTrackCommand.isEnabled = true MPRemoteCommandCenter.shared().previousTrackCommand.addTarget(self, action: #selector(previousButtonTapped))
} func addActionToNextCommand(){ MPRemoteCommandCenter.shared().nextTrackCommand.isEnabled = true MPRemoteCommandCenter.shared().nextTrackCommand.addTarget(self, action: #selector(nextButtonTapped))
} func addActionToChangePlayBackPosition(){ MPRemoteCommandCenter.shared().changePlaybackPositionCommand.isEnabled = true MPRemoteCommandCenter.shared().changePlaybackPositionCommand.addTarget(self, action: #selector(changePlaybackPosition))
} func addActionToseekForwardCommand(){ MPRemoteCommandCenter.shared().seekForwardCommand.isEnabled = true MPRemoteCommandCenter.shared().playCommand.addTarget(self, action: #selector(seekForward))
} func addActionToseekBackwordCommand(){ MPRemoteCommandCenter.shared().seekBackwardCommand.isEnabled = true MPRemoteCommandCenter.shared().playCommand.addTarget(self, action: #selector(seekBackword))
}

3. Playing info center configuration

func updateInfoCenter() { guard let item = currentItem else {return} var nowPlayingInfo : [String : AnyObject] = [ MPMediaItemPropertyPlaybackDuration : item.duration ?? 0 as AnyObject, MPMediaItemPropertyTitle : item.contentObj.title as AnyObject, MPNowPlayingInfoPropertyElapsedPlaybackTime : item.currentTime as AnyObject, MPNowPlayingInfoPropertyPlaybackQueueCount : totalCount as AnyObject, MPNowPlayingInfoPropertyPlaybackQueueIndex : playIndex as AnyObject, MPMediaItemPropertyMediaType : MPMediaType.anyAudio.rawValue as AnyObject, MPMediaItemPropertyArtist ] MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}

Note

Ngoài ra còn nhiều thứ khác mà bạn có thể handle được thông qua MPRemoteCommandCenter như seek, duration, elapsed, rate ....

Hy vọng bài viết này, có thể giúp ích được cho bạn một phần nào đó.

Nguồn:

https://medium.com/@g4gurpreetoberoi/controlling-music-from-lock-screen-mpnowplayinginfocenter-3f75ec7972d6

https://medium.com/@varundudeja/showing-media-player-system-controls-on-notification-screen-in-ios-swift-4e27fbf73575

Bình luận

Bài viết tương tự

- vừa được xem lúc

Học Flutter từ cơ bản đến nâng cao. Phần 1: Làm quen cô nàng Flutter

Lời mở đầu. Gần đây, Flutter nổi lên và được Google PR như một xu thế của lập trình di động vậy.

0 0 254

- vừa được xem lúc

Học Flutter từ cơ bản đến nâng cao. Phần 3: Lột trần cô nàng Flutter, BuildContext là gì?

Lời mở đầu. Màn làm quen cô nàng FLutter ở Phần 1 đã gieo rắc vào đầu chúng ta quá nhiều điều bí ẩn về nàng Flutter.

0 0 189

- vừa được xem lúc

Swift: Tạo custom phép toán tử (Operator) của riêng bạn!

Swift cho phép bạn tạo các toán tử có thể tùy chỉnh của riêng bạn. Điều này đặc biệt hữu ích khi bạn xử lý các loại dữ liệu của riêng mình. Operator Types in Swift. Có năm loại toán tử chính trong Swift.

0 0 45

- vừa được xem lúc

Chương 6 Protocol oriented programming.

Cuốn sách này là về lập trình hướng protocol. Khi Apple thông báo swift 2 ở WWDC 2015.

0 0 28

- vừa được xem lúc

Ví dụ về UIActivityViewController

Trên iOS, UIActivityViewController cung cấp giao diện thống nhất để người dùng chia sẻ và thực hiện các hành động trên văn bản, hình ảnh, URL và các mục khác trong ứng dụng. let string = "Hello, world!". let url = URL(string: "https://nshipster.com").

0 0 45

- vừa được xem lúc

Quản lý self và cancellable trong Combine.

. . Công việc quản lý bộ nhớ memory management thường trở nên phức tạp khi chúng ta thực hiện các tác vụ bất đồng bộ asynchronous vì chúng ta thường phải lưu giữ một số object nằm ngoài scope mà object được define trong khi vẫn phải đảm bảo được việc giải phóng object đó được thực hiện đúng quy trìn

0 0 28