ControlKit Light ControlKit Dark

ControlKit

ControlKit 是一个极简的 Swift Package,能够控制媒体播放和设备音量。

Build Status Lint Contributors

TLDR

一行代码控制设备音量

Control.Volume.increase() // 🔊 🆙

安装

将 ControlKit 添加为 Xcode 项目的依赖项

  1. 前往 File > Add Package Dependencies…

  2. 在搜索栏中输入包 URL

https://github.com/superturboryan/ControlKit.git
  1. 选择您想要包含的库

Screenshot 2024-10-28 at 17 46 50

要求

SpotifyController 需要 Spotify Client IDRedirect URL 以授权并控制 Spotify 应用。

  1. 为您的应用定义自定义 URL Scheme。将 URL Type 添加到目标的 Info.plist 文件中。

  2. Spotify Developer Dashboard 中创建一个应用,以获取 client ID 并注册您的 redirect URL (scheme)。

Screenshot 2024-10-29 at 17 31 24

警告 👇

默认情况下,Spotify 访问令牌不会在应用启动之间持久保存

如果您希望访问令牌持久保存,则必须提供一个符合 DAO<String> 的对象。

用法

控制

import Control

// 🔊 Decrement system volume
Control.Volume.decrease()

// 🕵️‍♂️ Check if audio is being played (by another app)
if Control.Playback.isAudioPlaying { 
    // 💃🕺 
}

// ⏭️ Skip to next track (Apple Music only - use Controllers.SpotifyController for Spotify 💚)
Control.Playback.AppleMusic.skipToNextTrack()

// 🫨 Vibrate
Control.Haptics.vibrate()

控制器

// App.swift

import Controllers
import SwiftUI

@main struct YourApp: App {

    @StateObject var spotify = SpotifyController(
        config: .init(
            clientID: Secrets.clientID,
            redirectURL: "controlkit://spotify"
        )
    )
    
    var body: some Scene {
        WindowGroup {
            ContentView()
            .environmentObject(spotify)
            .onOpenURL { spotify.setAccessToken(from: $0) } // Parse access token from URL
        }
    }
    
    func skipToNextSpotifyTrack() {
        spotify.skipToNextTrack()
    }
}

// Secrets.swift 🔐
// Don't forget to gitignore this 🙈
enum Secrets {
    static let clientID = "<your client id>"
}

依赖项

📚 AVFAudio
📚 Media Player
📦 SpotifyiOS

贡献

欢迎贡献和反馈! 🧑‍💻👩‍💻

以下是一些指南