Play Random Music Script and ?

Requests, suggestions, support, and everything else python / plugin related.
Post Reply
Dogfyter
Posts: 28
Joined: Mon Jul 23, 2012 11:51 pm
Has thanked: 6 times

Play Random Music Script and ?

Post by Dogfyter »

Here is a copy of a script I have submitted before wich allows you to "Play Random Music". If you couple this with the autoexec.py it will start music/ playlist(s) on start up.

The question I have does anybody know how a "LIMIT" of songs could be here. This will keep you from the ongoing need of having to make all your music has been scanned into the library. Also putting a limit here will free up a lot of RAM and, avoid system crashes for those of us who have several gigs of music on file. Thanks in advance for the help. Again, this is not my script. All credit is due to its creator.


''' 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)
User avatar
sahib12
Posts: 285
Joined: Wed Jan 02, 2013 3:13 pm
Location: Liberty City
Has thanked: 8 times
Been thanked: 16 times
Contact:

Re: Play Random Music Script and ?

Post by sahib12 »

This script sounds great, did you forget to upload it? Or do we have to make it ourselves?
Image
Dogfyter
Posts: 28
Joined: Mon Jul 23, 2012 11:51 pm
Has thanked: 6 times

Re: Play Random Music Script and ?

Post by Dogfyter »

The site will not let me load this python file. It says txt files are not allowed. I guess you will just have to copy it from my previous post.
User avatar
Dan Dar3
Posts: 1176
Joined: Sun Jul 08, 2012 4:09 pm
Has thanked: 273 times
Been thanked: 257 times
Contact:

Re: Play Random Music Script and ?

Post by Dan Dar3 »

@Dogfyter
Python is quite particular about the amount of indentation, if you want to paste code in the forums you need to use the "code" tags in the editor toolbar here, e.g.

Code: Select all

first line
  second line, with indentation
Dogfyter
Posts: 28
Joined: Mon Jul 23, 2012 11:51 pm
Has thanked: 6 times

Re: Play Random Music Script and ?

Post by Dogfyter »

How's this?

Code: Select all

''' 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 = [':\\Music', 'F:\\Music',':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)


Post Reply