Playing youtube video using iframe in ionic 2 app

2019-05-28 07:12发布

问题:

In an Ionic 2 app, I am trying to embed one youtube video using iframe. The code looks like this:

<iframe width="320" height="315" src="https://www.youtube.com/embed/VIDEOID?autoplay=1&controls=1" frameborder="0" allowfullscreen></iframe>

However when I navigate to the page I am getting this error. I can't find a good answer how to fix this.

XMLHttpRequest cannot load https://googleads.g.doubleclick.net/pagead/id. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'https://www.youtube.com' is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute.

Not sure how to fix this. Any help/lead would be appreciated.

BTW, I am developing/testing in chrome browser developer tool.

Thanks,

回答1:

You better add this to your config.xml:

<preference name="AllowInlineMediaPlayback" value="true"/>
<access origin="*" subdomains="true" /> 

Also make sure your android manifest has:

<application android:hardwareAccelerated="true" ...>


回答2:

I have solved iframe problem by editing two html_sanitizer.js files. You ca find them under node_modules\@angular\platform-browser\src\security and node_modules\@angular\platform-browser\esm\src\security folders.

Open them and search for this line var BLOCK_ELEMENTS, insert iframe tag name and save. Now it should work, at least it works for me.