3 回答

TA貢獻1790條經驗 獲得超9個贊
V14 中沒有開箱即用的組件,但很容易制作一個自己的組件,如下所述:使用 Element API 創建簡單組件:)
所以我簡單地檢查了一下,這似乎有效:
AudioPlayer.java
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Tag;
@Tag("audio")
public class AudioPlayer? extends Component {
? ? public AudioPlayer(){
? ? ? ? getElement().setAttribute("controls",true);
? ? }
? ? public? void setSource(String path){
? ? ? ? getElement().setProperty("src",path);
? ? }
}
使用:
AudioPlayer player=new AudioPlayer();
player.setSource("https://file-examples.com/wp-content/uploads/2017/11/file_example_WAV_1MG.wav");
add(player);
我本地沒有任何音樂文件,因此從互聯網上隨機獲取了一些音樂文件并作為來源傳遞。當然,這不是一個萬無一失的解決方案,只是一個概念證明:)

TA貢獻1789條經驗 獲得超10個贊
我想為那些可能使用流的人添加一個簡短的補充。
阿納斯米的 AudioPlayer.java:
public void setSource(final AbstractStreamResource resource) {
getElement().setAttribute("src", resource);
}
要使其正常工作,您必須設置流的內容類型:
var stream = new StreamResource("foo", () -> {
byte[] data = getBytesFromFileMP3(soundfile);
return new ByteArrayInputStream(data); })
.setContentType("audio/mpeg"); // For MP3

TA貢獻1806條經驗 獲得超8個贊
如果你想故意播放聲音(比如點擊按鈕或類似的東西時),你可以用這種方式添加方法播放:
public void play() {
? ? getElement().callJsFunction("play");
}
希望它能幫助某人。
添加回答
舉報