0

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)
wolfrevo
  • 6,651
  • 2
  • 26
  • 38

2 Answers2

2

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'))
wolfrevo
  • 6,651
  • 2
  • 26
  • 38
1

This should be an absolute path in your filesystem without any prefixes, for example '/foo/bar/spam.mp4' (*nix) or 'c:\\foo\\bar\\spam.mp4' (Win). I think, network filesystems will work too and for them you do need prefixes like smb:// or nfs://`, but not for local files.

Roman Miroshnychenko
  • 1,496
  • 1
  • 10
  • 16
  • your answer is correct. therefore upvoted. Nonetheless it won't solve the problem in an addon which is supposed to work in diferent filesystems (linux, win etc.) – wolfrevo Sep 30 '16 at 18:44
  • Your comment makes no sense. A path to a local file will always be in a filesystem where this file resides. You need to somehow obtain this path, for example, by scanning some directory with `os.listdir()`. A hardcoded path won't work everywhere because it's simply impossible. How can you guarantee that the file is located in the same place on every machine that will run your addon? This is not to mention the fact, that compatibility with different filesystems was not in your initial question. – Roman Miroshnychenko Sep 30 '16 at 21:22
  • For my purposes it makes sense because I want the addon to work in different filesystems. And fortunately the developers of Kodi addressed this issue when they implemented the `special://` protocol, which makes possible what seems impossible. See my answer. IMHO compatibility with different filesystems is implicitly required by addon. Sorry if I did not explicitly uttered it. Anyhow: thanks for your valuable answer and your efforts. – wolfrevo Oct 02 '16 at 06:55