I wrestled with this issue for a while and found the issue. For the initial script declaration:
<script
src="https://js-cdn.music.apple.com/musickit/v3/musickit.js"
/>
I removed the async property. The result was synchronous loading of music kit. If you remove async, setQueue() will always return void and play() won't play or provide any error message as to why it isn't working.
I don't think this is intended behaviour but on the slim chance that it is, the documentation could really benefit from specifying that the removal of the async property may apply for the return of search queries but not for playback.
The reason for I removed async in the first place was because the initial load was failing more often than not, with:
Uncaught TypeError: Cannot read properties of undefined (reading 'node')
at musickit.js:13:10194
at musickit.js:13:140
at musickit.js:13:209
It's successful < 25% of the time.
Removing async resolved this issue, which may be helpful to others with different app requirements. I'll create a new post regarding this async issue.
Post
Replies
Boosts
Views
Activity
Had the same issue. This worked for me:
const privateKey = await readFile(filePath, { encoding: 'utf-8' })
const now = Math.floor(Date.now() / 1000)
return jwt.sign(
{
iss: issuer,
iat: now,
exp: now + 15776999, // ~ less than 6 months from now
origin: ['http://localhost:3000'], // recommended in the docs
},
privateKey,
{
header: {
alg: 'ES256',
kid,
typ: 'JWT',
},
},
)
Hi @scottfinkelstein what version of the API are you using? I've got the same issue but using V3. No errors or anything
@hntdhs you can create an auth token with something similar to this:
const generateAppleMusicToken = async ({
filePath,
issuer,
kid,
}: { filePath: string; issuer: string; kid: string }) => {
console.log('Generating apply music token...')
const privateKey = await readFile(filePath, { encoding: 'utf-8' })
return jwt.sign({}, privateKey, {
issuer,
expiresIn: '180d',
header: {
alg: 'ES256',
kid,
},
})
}
you should have a .p8 file if you've signed up as an apple developer and created a key. For more info on JWTs, check out https://jwt.io/introduction