Search code examples
kodi

How can I add a local file to a kodi addon?


How can I add a local file to a kodi addon? In the following example the internet file (url='http://...') works. But the local file (url='file://...') do not.

import xbmc
import xbmcgui
import xbmcplugin
import xbmcaddon
import xbmcvfs
import sys
addon_handle = int(sys.argv[1])
xbmcplugin.setContent(addon_handle, 'songs')

#this works
xbmcplugin.addDirectoryItem(handle=addon_handle, url='http://www.noiseaddicts.com/samples_1w72b820/2537.mp3', listitem=xbmcgui.ListItem('internet_file'))

#this do not work
xbmcplugin.addDirectoryItem(handle=addon_handle, url='file://media/usb0/music/bn/local_file.mp3', listitem=xbmcgui.ListItem('local_file'))

xbmcplugin.endOfDirectory(addon_handle)

Solution

  • After searching for a while I found Kodi's special:// protocol: http://kodi.wiki/view/Special_protocol which I quote here:

    The "Special Protocol" is Kodi's solution to platform dependent directories. Common directory names are assigned a special://[name] path which is passed around inside Kodi and then translated to the platform specific path before the operating system sees it. This helps keep most of the platform mess centralized in the code.

    Using the special:// protocol following code will do it:

    xbmcplugin.addDirectoryItem(handle=addon_handle, 
                                url='special://home/bn/local_file.mp3',
                                listitem=xbmcgui.ListItem('local_file'))