2

我正在尝试使用chrome.getMediaGalleries API列出用户设备上的媒体画廊。它的工作原理直到item.name部分。使用chrome.mediaGalleries API获取用户画廊的名称

我观看了Google Developers YouTube频道的video,其中他们使用了JSON.parse。但是,这会抛出预期的Uncaught SyntaxError: Unexpected token c in JSON at position 0。这不是JSON。

所以我对你的问题是:如何检索人类可读的(视频,音乐等)。这是我到目前为止的代码:

getGalleriesInfo(/*Array of DOMFileSystem objects*/ results) { 

    if (!results.length) 
     return console.log('No galleries found'); 

    console.log('Gallery count: ' + results.length); 

    results.forEach((item, index) => { 
     let gallery = item.name;// JSON.parse(item.name) doesn't work 
     console.log(index, ' => ', gallery); // 0 => chrome-extension_hmnfbbnkjlgjgmlgpacnkkndfidbejpo_0:External_media_galleries-Default-hmnfbbnkjlgjgmlgpacnkkndfidbejpo-1, etc. 
    }); 
} 

// I'm using a custom element (document.registerElement) 
createdCallback() { 
    chrome.mediaGalleries.getMediaFileSystems({ 
     interactive: 'if_needed' 
    }, this.getGalleriesInfo); 
} 

感谢,
贾斯汀

编辑:Chrome的调试视图的item Chrome debugger view http://jtprogramming.com/screen.png

+0

当你在函数内设置断点时,你在调试器中看到了多少'item'值?问题是,视频中的代码没有意义,因为DOMFileSystem API不是基于JSON的。 – wOxxOm

+0

@wOxxOm我迷路了。你是说没有办法实现我的目标?在Chrome文档中,它讲述了如何访问iTunes图库,视频等。我如何区分它们? –

+0

@wOxxOm我没有看到任何我不知道的东西,但我不太熟悉chrome的调试器,所以也许我错过了一些东西。我在源代码面板的问题中包含了一个图片,其中包含'item'上的断点# –

回答

2

chrome.getMediaGalleries API似乎已更改。要找到任何给定图库的名称,您必须使用getMediaFileSystemMetadata方法。

_getGalleriesInfo(results) { 

    if (!results.length) 
     return console.log('No galleries found'); 

    console.log('Gallery count: ' + results.length); 

    results.forEach((item, index) => { 
     console.log(chrome.mediaGalleries.getMediaFileSystemMetadata(item)); 
    }); 
} 

它返回一个对象与DOMFileSystem的元数据。

Object { 
    galleryId: (string) int, 
    isAvailable: boolean, 
    isMediaDevice: boolean, 
    isRemovable: boolean, 
    name: "C:\example\path\to\gallery" 
} 
相关问题