Play Random Music Script Provided

General discussion relating to XBMC4XBOX. Please use the support forums for help on using the software or plugins.
Post Reply
Dogfyter
Posts: 28
Joined: Mon Jul 23, 2012 11:51 pm
Has thanked: 6 times

Play Random Music Script Provided

Post by Dogfyter »

Dear XBMC4XBOX,

Here is a simple script I found years ago for playing your own music files.

''' vim: ts=8 sw=4 noexpandtab
Author: Rune Hammersland
version: 0.3

Xbox Media Center comes with a script to play an mp3 file at startup
(StartUpMP3). This script will load a playlist if it exists, and if not,
traverse a directory and load all relevant music files. It is intended as a
replacement for StartUpMP3, and enables you to listen to a playlist, or all
music files in a given directory.

Changelog:
2005-05-27 13:10:33 - Rune Hammersland:
* Added possibility for multiple playlists.
* Added possibility for multiple music dirs.

'''

import xbmc
import os

# ---------------------------------------- #
# "Configuration"
# ---------------------------------------- #

# Change this to your playlist(s).
# Written like this: ['path\\to\\plist1', 'to\\plist2', 'plist\\3']
playlist_files= ['E:\\Media\\Music\\playlist.m3u', 'G:\\all.m3u']
# Dirs(s) to traverse if playlist does not exist. NB: No trailing slash.
# Written like this: ['path\\to\\dir1', 'to\\dir2', 'dir\\3']
music_dirs = ['E:\\Music', 'F:\\Music', 'G:\\Music']
# Shuffle playlist if this var equals 1.
shuffle_files = 1


# ---------------------------------------- #
# Code
# ---------------------------------------- #

# Function for adding files to playlist
def add_files(pl, dirname, names):
for filename in names:
if (os.path.isfile(dirname + "\\" + filename)):
add = 0

# Check extension of file.
if (filename[-4:] == ".mp3"): add = 1
if (filename[-4:] == ".ogg"): add = 1
if (filename[-4:] == ".wav"): add = 1
if (filename[-5:] == ".flac"): add = 1

# If file is to be added, do it.
if (add == 1): pl.add(dirname + "\\" + filename)
elif (os.path.isdir(dirname + "\\" + filename)):
os.path.walk(dirname + "\\" + filename, add_files, pl)

# Get music playlist from XBMC
plist = xbmc.PlayList(0)
plist.clear()

# Load playlist if it exists
for playlist_file in playlist_files:
if (os.path.isfile(playlist_file)):
plist.load(playlist_file)
# Else, find all available music
else:
for music_dir in music_dirs:
os.path.walk(music_dir, add_files, plist)

# Do the shuffle!
if (shuffle_files == 1): plist.shuffle()

xbmc.Player().play(plist)


You might want to place it in its own folder. I use this with another old script called "Auto Start Manager". These two scripts combined start random music from my MUSIC folders at BOOT. I am more than willing to share the "Auto Start Manager" script but, this script has a number of items in its folder and I do not know how to provide a folder full of items to you.

I did not write either script but, leave all credit to their authors. I hope there are those who will enjoy the startup music script.

I am curious to know if there is a way to incorporate the code from the Music script into XBMC. The "Playlist" option is ok but, not practicle if you have thousands of songs you have to add to the playlist every time you add music to your inventory. I am hoping this could be a simpler solution for those of us who have large hdds full of music files. Thanks in advance for your consideration.

Dogfyter
Post Reply