簡體   English   中英

我如何從collection.json中獲取音頻文件

[英]how do i get audio files out of collection.json

我如何從此collection.json中獲取mp3音頻鏈接。 我想要得到它,所以當用戶在搜索框中搜索音頻文件時,音頻文件會彈出播放,但我有點困惑

在此處輸入圖片說明

在此處輸入圖片說明

到目前為止,這是我的代碼

  const media = 'audio'; const searchInput = document.querySelector('.search'); const output = document.querySelector('.output') searchInput.addEventListener('change', searchData); async function searchData() { let search = this.value; const finalData = await getData(search); render(finalData); } function render(data) { let html; if(media == 'image') { html = data.map(result => { return ` <div class="image"> <img src="${result.links[0].href}"/> </div> ` }).join(""); } else { //parse the json console.log(data); data.map(result => { console.log(result); }) /* i want to get the mp3 audio out of the collection.json , how do i do this please? */ } output.innerHTML = html; } function getData(search) { const endpoint = `https://images-api.nasa.gov/search?q=${search}&media_type=${media}`; return fetch(endpoint) .then(blob => blob.json()) .then(data => data.collection.items); } 
 <input type="text" class="search" placeholder="planet"> <div class="output"></div> 

先感謝您

您僅在進行查詢以獲取API數據,該數據將返回對象的集合,每個對象僅描述一個資源。

對於每種資源,要獲取實際MP3的URL,您需要在href字段中提供的URL處對collection.json文件進行另一次提取。 這將返回該JSON文件的內容,在這種情況下,該文件是指向更多文件的URL數組,其中一些文件是mp3。 然后,您需要在該數組中找到一個以.mp3結尾的URL,並將其設置為<audio>元素的src

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM