{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 音频\n", "\n", "显示一个音频播放器。\n", "\n", "- `src`: 音频源的URL或本地文件路径\n", "- `controls`: 是否显示音频控制,如播放、暂停和音量(默认值:`True`)\n", "- `autoplay`: 是否自动开始播放音频(默认值:`False`)\n", "- `muted`: 音频是否应初始为静音状态(默认值:`False`)\n", "- `loop`: 音频是否应该循环播放(默认值:`False`)\n", "\n", "查看[事件列表](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio#events),您可以使用通用事件订阅`on()`进行订阅。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from nicegui import ui\n", "\n", "a = ui.audio('https://cdn.pixabay.com/download/audio/2022/02/22/audio_d1718ab41b.mp3')\n", "a.on('ended', lambda _: ui.notify('Audio playback completed'))\n", "\n", "ui.button(on_click=lambda: a.props('muted'), icon='volume_off').props('outline')\n", "ui.button(on_click=lambda: a.props(remove='muted'), icon='volume_up').props('outline')\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 控制音频元素\n", "此演示展示了如何以编程方式播放、暂停和查找。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from nicegui import ui\n", "\n", "a = ui.audio('https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3')\n", "ui.button('Play', on_click=a.play)\n", "ui.button('Pause', on_click=a.pause)\n", "ui.button('Jump to 0:30', on_click=lambda: a.seek(30))\n", "\n", "# ui.run()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "py311", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.7" } }, "nbformat": 4, "nbformat_minor": 2 }