android mediaplayer - How to play and pause Spotify embed with JAVASCRIPT? - Stack Overflow

Can someone explain me how can I play and pause this spotify embed object?I have tried to find the id

Can someone explain me how can I play and pause this spotify embed object? I have tried to find the id of the player but I couldn't.

URL →

This is the iframe code:

<iframe src=";amp&size=detail&amp;theme=light" style="border:0px #ffffff none;" name="myiFrame" scrolling="yes" frameborder="1" marginheight="0px" marginwidth="0px" height="100%" width="100%" allowfullscreen=""></iframe>

Can someone explain me how can I play and pause this spotify embed object? I have tried to find the id of the player but I couldn't.

URL → https://open.spotify./embed-podcast/show/5iKz9gAsyuQ1xLG6MFLtQg

This is the iframe code:

<iframe src="https://open.spotify./embed/show/5iKz9gAsyuQ1xLG6MFLtQg?utm_source=generator&amp&size=detail&amp;theme=light" style="border:0px #ffffff none;" name="myiFrame" scrolling="yes" frameborder="1" marginheight="0px" marginwidth="0px" height="100%" width="100%" allowfullscreen=""></iframe>

Thanks!!

Share Improve this question edited Apr 23, 2022 at 19:43 Clifford 93.8k14 gold badges92 silver badges171 bronze badges asked Apr 23, 2022 at 13:00 JaviR3TicSJaviR3TicS 351 silver badge6 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

It is odd that I couldn't find this documented anywhere, but I was able to look through the embed code to figure out that it listens for postMessage events for cross-origin munication and your request is possible.

The general code would be:

// Get a reference to the embed iframe element
const spotifyEmbedWindow = document.querySelector('iframe[src*="spotify./embed"]').contentWindow;
spotifyEmbedWindow.postMessage({mand: 'toggle'}, '*');

The above code should start the embed playing if it is paused or stopped, and pause it if it is already playing.

There are some other mands, but they probably aren't what you are looking for ('play', for example, actually restarts the player to the start of the track, and there is no explicit 'pause' mand).

PS: It looks like Spotify has actually implemented this munication both ways - you can receive updates from the embed (for example, if you want to know if someone has paused it), by listening for 'message' events in the parent window.

As of 2023, Spotify provided their own APIs to achieve play/pause/seek/etc: https://developer.spotify./documentation/embeds/references/iframe-api

You would use code like this, but it won't work because of cross-origin issues:

document.querySelector('iframe').contentWindow.document.body.querySelector('#play-button')

But you get: Uncaught DOMException: Permission denied to access property "document" on cross-origin object

So I believe this cannot be achieved - but I'd be happy to be proven wrong.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745590645a4634808.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信