跪拜 Guibai
← Back to the summary

A Scriptable Script Turns a Local Music Folder into an Offline iPhone Player

iOS Development Plan (5) - Building an Offline Music Player

Off-topic: Words fail me

One day while taking a walk home, lost in thought, I wrote this in my phone's memo:

When writing, I always feel that words fail me, as if my mind reaches further, but the words I write down are close at hand. My thoughts have wandered to the heavens, but the words only express ten meters, not even escaping my line of sight.

Later I thought perhaps the wandering to the heavens is just an illusion, and I only thought about ten meters. The so-called "words fail me" is merely an excuse I invented because my heart cannot accept that I didn't think deeply enough, and I don't want to bear the gap.

This iOS development plan is the same. The original intention was to make an offline music player for my iPhone. I had many features in mind, but what I've built so far is just a crude little tool, and I don't know how far it will go.

There are many music player apps on the market, and I've tried many, but none fit my needs: I just want a simple, offline music player.

I like listening to music, especially Cantonese oldies. But there are several pain points:

  1. With the spread of music copyright, listening to music has become a paid pleasure.
  2. Besides the cost, the copyrights of various songs are scattered across different platforms, making it inconvenient. (I think that's the case, I don't know if it has improved now, is there now an aggregator platform that unifies copyrights?)
  3. Many features in music apps are unnecessary. In-app ads go without saying, new album pushes I don't need, music charts and friend activity even less. I rarely listen to new songs, mostly old ones. Every time I open a music app, I have to close pop-up after pop-up, which is a nuisance.
  4. The operation of listening to music is complex.

To solve the first two problems, I now download songs to a local folder and play local files.

To solve the last two, I've tried several offline music players, such as iPlayer, VLC, foobar2000, and QQ Music without network permission.

The one that currently comes closest to my needs is QQ Music without network permission, which is what I'm using now. Its remaining downsides are the launch ad and the need to manually switch to the local music list every time.

Another app with potential is iPlayer, which almost solves my pain points. But it still requires too many taps to play music from a local folder, the "cast time" is too long. Also, the home screen playlist doesn't support alphabet navigation. So I joined the developer's community and gave feedback. It's an indie developer's app, and the developer is nice, keeps updating, and is open to suggestions (maybe the word "开张圣听" is not appropriate here? It's too big a word, but I couldn't think of another). The developer said they'd consider it, but kept delaying the updates. After a few nudges, I gave up.

The offline music player I envision should start quickly, show the offline music list upon opening, and play with a tap. Additionally:

These are all pretty basic features, so I had the idea of doing it myself.

When I used an Android phone, I wrote an offline music player: https://github.com/Vic-wkx/OfflineMusicPlayer . It wasn't great, but it was usable for a while. But I soon switched to iPhone, so the Android version became useless.

Since signing native iPhone apps is a hassle, I'm first trying Scriptable.

Building an Offline Music Player

Runtime effect

[images]

The Script

const Logger = importModule("logger")

// 1. Home-screen widget: draw a centered progress ring, tap to open the music list
if (config.runsInWidget) {
  const widget = new ListWidget()
  widget.url = URLScheme.forRunningScript()
  const stack = widget.addStack()
  stack.centerAlignContent()
  stack.addSpacer()
  const ring = stack.addProgress(100, {
    thickness: 9
  })
  ring.size = new Size(80, 80)
  stack.addSpacer()
  Script.setWidget(widget)
  Script.complete()
  return
}

// 2. Read music directory from File Bookmarks
const fm = FileManager.local()
const bmName = fm.bookmarkExists("Music") ? "Music" : ""
const musicDir = bmName ? fm.bookmarkedPath(bmName) : ""
const songs = musicDir
  ? fm.listContents(musicDir).filter(f => /\.(mp3|m4a|flac|wav|aac)$/i.test(f)).sort()
  : []
await Logger.log(`📂 Read bookmark [${bmName || "not configured"}], total ${songs.length} songs`)

// 3. Show music list, tap to play with system player (QuickLook)
while (true) {
  let selectedSong = null
  const table = new UITable()
  table.showSeparators = true

  for (const name of songs) {
    const row = new UITableRow()
    row.addText(name)
    row.onSelect = () => { selectedSong = name }
    table.addRow(row)
  }

  await table.present()
  if (!selectedSong) break

  await Logger.log(`▶️ Playing: ${selectedSong}`)
  await QuickLook.present(fm.joinPath(musicDir, selectedSong))
}

Code Explanation

The script has three parts:

1. Import utility and draw home-screen widget

The first line imports a logging utility via importModule. Then it checks config.runsInWidget to see if it's running in a home-screen widget. If so, it creates a widget canvas and sets widget.url, so tapping the widget opens the script directly.

To center the ring, it creates a horizontal container stack and puts a flexible spacer on each side of the ring, squeezing the 80x80 ring into the middle, then lets the system display it and exits.

2. Read the music folder

Uses FileManager.local to get the local file manager, reads the Music folder bookmark path added in Scriptable settings. Then listContents gets all filenames, filters by a regex for mp3, m4a, etc., and sorts.

Note: Before using this script, you need to add the local music folder path in File Bookmarks, named Music.

[image]

3. Display playlist and call the system player

Uses UITable to create a native music list interface.

Because iOS doesn't allow a player to pop up while a list window is still open, when a row is tapped, onSelect only records the song name in selectedSong, and the list closes automatically.

After await table.present fully closes, QuickLook.present pops up the system player to play the song.

The outer while (true) loop is so that after you listen and close the player, it returns to the playlist; if you directly tap the close button at the top-left of the list, selectedSong is empty, triggering a break that ends the script.

Afterword

And that's it for a simple offline music player. It's still crude, only doing two things:

I'll continue to improve it.

Comments

Top 1 of 2 from juejin.cn, machine-translated. The original thread is authoritative.

Mervyn_Lee

Try Salt Player.

Vic_wkx

Thanks. I gave Salt Player a try, and the experience is genuinely nice. The UI is clean, and the home screen shows a list that plays directly. It's the effect I wanted to achieve. The only thing it lacks is the letter index bar. The alphabetical index bar on the right side of QQ Music is genuinely handy; once you've used it, you can't go back. When I want to find a song, tapping the search box at the top and typing is a bit of a hassle, and scrolling down through the list takes too long. With a letter index bar, one-hand swiping gets you to the song quickly. So satisfying. There are also two minor flaws: first, "Locate Current Song" is buried in the collapsible menu; I think this feature could be pinned as a floating button. Second, I don't think the "Stats" tab needs to be fixed in the tab bar; it could just be hidden.