Swiftpack.co -  yoheimuta/RxMusicPlayer as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
A reactive library to make it easy for audio playbacks using RxSwift.
.package(url: "https://github.com/yoheimuta/RxMusicPlayer.git", from: "2.0.1")


Build Status Carthage compatible Version License

RxMusicPlayer is a wrapper of avplayer backed by RxSwift to make it easy for audio playbacks.


  • Following the Audio Guidelines for User-Controlled Playback and Recording Apps.
  • Support for streaming both remote and local audio files.
  • Functions to play, pause, stop, play next, play previous, prefetch metadata, repeat mode(repeat, repeat all), shuffle mode desired playback rate and seek to a certain second.
  • Loading metadata, including title, album, artist, artwork, duration, and lyrics.
  • Background mode integration with MPNowPlayingInfoCenter.
  • Remote command control integration with MPRemoteCommandCenter.
  • Interruption handling with AVAudioSession.interruptionNotification.
  • Route change handling with AVAudioSession.routeChangeNotification.
  • Including a fully working example project.

Runtime Requirements

  • iOS 10.0 or later


Swift Package Manager

With 2.0.1 and above.


github "yoheimuta/RxMusicPlayer"


pod "RxMusicPlayer"


For details, refer to the Example project. Plus, see also Users section below.



You can implement your audio player with the custom frontend without any delegates, like below.

import RxCocoa
import RxMusicPlayer
import RxSwift
import UIKit

class TableViewController: UITableViewController {

    @IBOutlet private var playButton: UIButton!
    @IBOutlet private var nextButton: UIButton!
    @IBOutlet private var prevButton: UIButton!
    @IBOutlet private var titleLabel: UILabel!
    @IBOutlet private var artImageView: UIImageView!
    @IBOutlet private var lyricsLabel: UILabel!
    @IBOutlet private var seekBar: ProgressSlider!
    @IBOutlet private var seekDurationLabel: UILabel!
    @IBOutlet private var durationLabel: UILabel!
    @IBOutlet private var shuffleButton: UIButton!
    @IBOutlet private var repeatButton: UIButton!
    @IBOutlet private var rateButton: UIButton!

    private let disposeBag = DisposeBag()

    // swiftlint:disable cyclomatic_complexity
    override func viewDidLoad() {

        // 1) Create a player
        let items = [
        .map({ RxMusicPlayerItem(url: URL(string: $0)!) })
        let player = RxMusicPlayer(items: items)!

        // 2) Control views
        player.rx.canSendCommand(cmd: .play)
            .do(onNext: { [weak self] canPlay in
                self?.playButton.setTitle(canPlay ? "Play" : "Pause", for: .normal)
            .disposed(by: disposeBag)

        player.rx.canSendCommand(cmd: .next)
            .disposed(by: disposeBag)

        player.rx.canSendCommand(cmd: .previous)
            .disposed(by: disposeBag)

        player.rx.canSendCommand(cmd: .seek(seconds: 0, shouldPlay: false))
            .disposed(by: disposeBag)

            .disposed(by: disposeBag)

            .disposed(by: disposeBag)

            .do(onNext: { [weak self] _ in
            .disposed(by: disposeBag)

            .map {
                guard let rest = $0 else { return "--:--" }
                return "-\(rest)"
            .disposed(by: disposeBag)

            .disposed(by: disposeBag)

            .map { Float($0?.seconds ?? 0) }
            .do(onNext: { [weak self] in
                self?.seekBar.maximumValue = $0
            .disposed(by: disposeBag)

        let seekValuePass = BehaviorRelay<Bool>(value: true)
            .withLatestFrom(seekValuePass.asDriver()) { ($0, $1) }
            .filter { $0.1 }
            .map { Float($0.0?.seconds ?? 0) }
            .disposed(by: disposeBag)
            .do(onNext: {
            .disposed(by: disposeBag)
            .do(onNext: {
            .disposed(by: disposeBag)

            .disposed(by: disposeBag)

            .do(onNext: { [weak self] mode in
                self?.shuffleButton.setTitle(mode == .off ? "Shuffle" : "No Shuffle", for: .normal)
            .disposed(by: disposeBag)

            .do(onNext: { [weak self] mode in
                var title = ""
                switch mode {
                case .none: title = "Repeat"
                case .one: title = "Repeat(All)"
                case .all: title = "No Repeat"
                self?.repeatButton.setTitle(title, for: .normal)
            .disposed(by: disposeBag)

            .do(onNext: { index in
                if index == player.queuedItems.count - 1 {
                    // You can remove the comment-out below to confirm the append().
                    // player.append(items: items)
            .disposed(by: disposeBag)

        // 3) Process the user's input
        let cmd = Driver.merge(
            playButton.rx.tap.asDriver().map { [weak self] in
                if self?.playButton.currentTitle == "Play" {
                    return RxMusicPlayer.Command.play
                return RxMusicPlayer.Command.pause
            nextButton.rx.tap.asDriver().map { RxMusicPlayer.Command.next },
            prevButton.rx.tap.asDriver().map { RxMusicPlayer.Command.previous },
                .map { [weak self] _ in
                    RxMusicPlayer.Command.seek(seconds: Int(self?.seekBar.value ?? 0),
                                               shouldPlay: false)

        // You can remove the comment-out below to confirm changing the current index of music items.
        // Default is 0.
        // player.playIndex = 1

        player.run(cmd: cmd)
            .do(onNext: { status in
                UIApplication.shared.isNetworkActivityIndicatorVisible = status == .loading
            .flatMap { [weak self] status -> Driver<()> in
                guard let weakSelf = self else { return .just(()) }

                switch status {
                case let RxMusicPlayer.Status.failed(err: err):
                    return Wireframe.promptOKAlertFor(src: weakSelf,
                                                      title: "Error",
                                                      message: err.localizedDescription)

                case let RxMusicPlayer.Status.critical(err: err):
                    return Wireframe.promptOKAlertFor(src: weakSelf,
                                                      title: "Critical Error",
                                                      message: err.localizedDescription)
                return .just(())
            .disposed(by: disposeBag)

            .drive(onNext: {
                switch player.shuffleMode {
                case .off: player.shuffleMode = .songs
                case .songs: player.shuffleMode = .off
            .disposed(by: disposeBag)

            .drive(onNext: {
                switch player.repeatMode {
                case .none: player.repeatMode = .one
                case .one: player.repeatMode = .all
                case .all: player.repeatMode = .none
            .disposed(by: disposeBag)

            .flatMapLatest { [weak self] _ -> Driver<()> in
                guard let weakSelf = self else { return .just(()) }

                return Wireframe.promptSimpleActionSheetFor(
                    src: weakSelf,
                    cancelAction: "Close",
                    actions: PlaybackRateAction.allCases.map {
                        player.desiredPlaybackRate == $0.toFloat ? "\($0.rawValue)✓" : $0.rawValue
                    .do(onNext: { [weak self] action in
                        if let rate = PlaybackRateAction(rawValue: action)?.toFloat {
                            player.desiredPlaybackRate = rate
                            self?.rateButton.setTitle(action, for: .normal)
                    .map { _ in }
            .disposed(by: disposeBag)



  • Fork it
  • Run make bootstrap
  • Create your feature branch: git checkout -b your-new-feature
  • Commit changes: git commit -m 'Add your feature'
  • Push to the branch: git push origin your-new-feature
  • Submit a pull request


  • Create a new release on GitHub
  • Publish a new podspec on Cocoapods
    • bundle exec pod trunk push RxMusicPlayer.podspec

Bug Report

While any bug reports are helpful, it's sometimes unable to pinpoint the cause without a reproducible project.

In particular, since RxMusicPlayer depends on RxSwift that is prone to your application program mistakes, it's more essential to decouple the problem.

Therefore, I highly recommend that you submit an issue with that project.

You can create it like the following steps.

  • Fork it
  • Create your feature branch: git checkout -b your-bug-name
  • Add some changes under the Example directory to reproduce the bug
  • Commit changes: git commit -m 'Add a reproducible feature'
  • Push to the branch: git push origin your-bug-name
  • (Optional) Submit a pull request
  • Share it in your issue

The code should not be intertwined but concise, straightforward, and naive.

NOTE: If you can't prepare any reproducible code, you have to elaborate the detail precisely and clearly so that I can reproduce the problem.


The MIT License (MIT)


Thank you to the following projects and creators.


Stars: 26
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

Support Swift Package Manager
1 week ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API