Swiftpack.co - tsuruken0802/MusicPlayer as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by tsuruken0802.
tsuruken0802/MusicPlayer v2.6.1
Music Player for iOS. this repository contains sample music player app like iOS Music App.
⭐️ 0
🕓 2 weeks ago
iOS
.package(url: "https://github.com/tsuruken0802/MusicPlayer.git", from: "v2.6.1")

Music Player

This package is a Music Player created by SwiftUI.

Description

It plays music in Apple Music in iOS device.

Features

  • Play music, pause, play next, play previous
  • Change playback rate
  • Change playback pitch
  • Trimming playback
  • Shuffle items
  • Repeat items
  • Change playback current time(seek bar also supported)
  • Play music in background
  • Operate playback in off screen(Using MPRemoteCommandCenter)
  • Playback and pause process due to interruptions from phone calls, earphones, etc.

Demo

This is a demo video of the sample project.

demo

Requirement

iOS 13 or later.

Usage

Play music

// songs are MPSongItem's list
// MPSongItem is song data that contains MPMediaItem, effect, triming, and so on.
// index is the index of music playback
MusicPlayer.shared.play(items: songs, index: index)
  • Pause
MusicPlayer.shared.pause()
  • Play next
MusicPlayer.shared.next()
  • Play previous
MusicPlayer.shared.back()

Change playback rate

You can change the playback rate using MusicPlayer.shared.rate

You can bind the value with Slider of SwiftUI.

Slider(value: $musicPlayer.rate, in: musicPlayer.rateOptions.minValue...musicPlayer.rateOptions.maxValue, step: musicPlayer.rateOptions.unit)

To raise or lower the rate value by one step, use following.

// increment rate
MusicPlayer.shared.incrementRate()

// decrement rate
MusicPlayer.shared.decrementRate()

Change playback pitch

You can change the pitch using MusicPlayer.shared.pitch

You can bind the value with Slider of SwiftUI.

Slider(value: $musicPlayer.pitch, in: musicPlayer.pitchOptions.minValue...musicPlayer.pitchOptions.maxValue, step: musicPlayer.pitchOptions.unit)

To raise or lower the pitch value by one step, use following.

// increment pitch
MusicPlayer.shared.incrementPitch()

// decrement pitch
MusicPlayer.shared.decrementPitch()

Trimming playback

You can trim playback.

Below code that trim from a minute to three minutes.

MusicPlayer.shared.playbackTimeRange = 60...180

Shuffle items

You can shuffle items.

Below code that change shuffle flg.

MusicPlayer.shared.isShuffle.toggle()

Repeat items

You can repeat items.

Below code that change repeat type.

switch MusicPlayer.shared.repeatType {
case .list:
    MusicPlayer.shared.repeatType = .one

case .one:
    MusicPlayer.shared.repeatType = .none

case .none:
    MusicPlayer.shared.repeatType = .list
}

Change playback current time

You can change the current playback time using MusicPlayer.shared.currentTime

To raise or lower the pitch value by one step, use following.

Slider(value: $musicPlayer.currentTime, in: 0...Float(duration), step: 0.1) { isEditing in
    if isEditing {
        musicPlayer.stopCurrentTimeRendering()
    }
    else {
        musicPlayer.startCurrentTimeRedering()
        musicPlayer.setSeek(withPlay: musicPlayer.isPlaying)
    }
}

Install

Add it in the latest main branch in Swift Package Manager.

Licence

Copyright 2022 Kentaro Tsurumoto.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Author

Tsuruken

GitHub

link
Stars: 0
Last commit: 19 hours ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

v2.6.1
4 weeks ago
  • to public property

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