Spaces:
Sleeping
Sleeping
| # -*- coding: utf-8 -*- | |
| """AIR_Widget.ipynb | |
| Automatically generated by Colab. | |
| Original file is located at | |
| https://colab.research.google.com/drive/13DSMlvyBAyKz0NMD4uE8u26KOtQVK5mx | |
| """ | |
| import gradio as gr | |
| import base64 | |
| # AIR News 24x7 stream URL | |
| stream_url = "https://air.pc.cdn.bitgravity.com/air/live/pbaudio001/playlist.m3u8" | |
| station_name = "AIR News 24x7" | |
| def generate_iframe(): | |
| html_template = f""" | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script> | |
| <style> | |
| body {{ | |
| font-family: sans-serif; | |
| text-align: center; | |
| margin: 0; | |
| padding: 1em; | |
| }} | |
| video {{ | |
| width: 80%; | |
| max-width: 600px; | |
| height: 50px; | |
| background: #000; | |
| }} | |
| #notice {{ | |
| color: red; | |
| margin-top: 10px; | |
| }} | |
| </style> | |
| </head> | |
| <body> | |
| <h3>{station_name}</h3> | |
| <video id="player" controls autoplay></video> | |
| <div id="notice">π If playback doesn't start automatically, click play.</div> | |
| <script> | |
| var video = document.getElementById('player'); | |
| if (Hls.isSupported()) {{ | |
| var hls = new Hls(); | |
| hls.loadSource("{stream_url}"); | |
| hls.attachMedia(video); | |
| hls.on(Hls.Events.MANIFEST_PARSED, function () {{ | |
| var playPromise = video.play(); | |
| if (playPromise !== undefined) {{ | |
| playPromise.catch(function() {{ | |
| document.getElementById("notice").innerText = "Click play to start radio"; | |
| }}); | |
| }} | |
| }}); | |
| }} else if (video.canPlayType('application/vnd.apple.mpegurl')) {{ | |
| video.src = "{stream_url}"; | |
| video.addEventListener('loadedmetadata', function () {{ | |
| var playPromise = video.play(); | |
| if (playPromise !== undefined) {{ | |
| playPromise.catch(function() {{ | |
| document.getElementById("notice").innerText = "Click play to start radio"; | |
| }}); | |
| }} | |
| }}); | |
| }} else {{ | |
| document.getElementById("notice").innerText = "β Your browser does not support HLS audio."; | |
| }} | |
| </script> | |
| </body> | |
| </html> | |
| """ | |
| html_base64 = base64.b64encode(html_template.encode("utf-8")).decode("utf-8") | |
| iframe_src = f"data:text/html;base64,{html_base64}" | |
| return f"""<iframe src="{iframe_src}" width="100%" height="180px" frameborder="0"></iframe>""" | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## ποΈ AIR News 24x7 Live Stream") | |
| gr.Markdown("βΆ Autoplay enabled. If autoplay is blocked, click the play button.") | |
| output = gr.HTML(value=generate_iframe()) | |
| demo.launch() |