admin管理员组文章数量:1023273
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&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&&size=detail&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 badges3 Answers
Reset to default 6It 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.
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&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&&size=detail&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 badges3 Answers
Reset to default 6It 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.
本文标签: android mediaplayerHow to play and pause Spotify embed with JAVASCRIPTStack Overflow
版权声明:本文标题:android mediaplayer - How to play and pause Spotify embed with JAVASCRIPT? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745590643a2157863.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论