[REQUEST] Script that make music always play in background
#1
I know that you can have a playlist play on startup, but once you watch a movie or play a game and come back to XBMC, the music is no longer playing. My suggestion is two-fold:

1. Add the option to always randomly play music from [directory] while navigating menus in XBMC (even after watching a video or playing a game).
2. Remove the requirement that it be a playlist. Just have it play music from an entire directory, like shuffle on an iPod.

Thanks
Reply
#2
such a script would actually be very helpful to me if i ever finish a certain skin i started working on a...long time ago. I basically need it as a background ambient sound track though rather than a random music dealio
Reply
#3
Is it too late to formally submit this as a feature request? I really think a lot of people would like this, just always have music playing in the background all the time.
Reply
#4
its never too late to submit a feature request, as far as I know. Though, they might not look at it until after the feature freeze is complete.
Reply
#5
this isn't so hard to do if i completely ignore making a GUI...

copy and paste this into a default.py file, and start it with autoexec.py

read the comments in the file, they explain how to use it.

@ timdog82001 - you're probably going to be more concerned with the sfx part. be warned though, the one wav file I tried to play with this method sounded awful, and it made the GUI slow. It was a 11,025Hz 16bit PCM I grabbed from here.

if you use partymode, it will act just like partymode always does and change the activewindow to now playing, so it's probably not what you guys want to do.

Code:
"""
    media is what you want playing.
        -Note: for partymode you should leave it as media="", and set playercontrol1="Partymode(music)"
        -Note: if you use a playlist, it will start over everytime.  the script will NOT remember where it left off.
        This can be a playlist, music, or video file or an Url.
            ex. media="C:\\Program Files\\XBMC\\userdata\\playlists\\music\\sampleplaylist.m3u"
            ex. media="F:\\Media\\Music\\Primus\\Pork Soda\\01 - Pork Chop's Little Ditty.mp3"
        
    playercontrol1 & playercontrol2 control how playlists behave.
        options are: "Random", "RepeatOne", "RepeatAll", or "Partymode(music)"
        ex. to repeat all songs in a playlist do this:
            playercontrol1="RepeatAll"
            playercontrol2="Random"
        ex. to repeat all songs in a playlist in order, do this:
            playercontrol1="RepeatAll"
            playercontrol2=""
        ex. for partymode leave media as media=""
            playercontrol1="Partymode(music)"

    sfx is for when you want a short ambient file to always play in the background.
        it won't show up anywhere as playing.
        this was intended for short sound effects, so don't get mad if you crash a lot with this enabled.  i've only done limited testing
        needs to be a full path to a .wav
        
"""

media="C:\\test.wav"
# This can be a playlist, music, or video file or an Url.
playercontrol1=""
playercontrol2=""
# "Random", "RepeatOne", "RepeatAll", "Partymode(music)", "Partymode(path to .xsp file)"
sfx=True
# True or False | Note that there are no quotes around True or False
# media needs to be a path to a .wav file if this is true


####################################
#Don't mess with anything down here#
#unless you know what you're doing #
####################################

import xbmc,xbmcgui
from os import path

xbmc.executehttpapi('SetResponseFormat&parameter=WebHeader;False;WebFooter;False')
if (media!="" and not path.isfile(media)):
    xbmcgui.Dialog().ok('Invalid Path','Path to media not found')

y=1
while y==1:
    rawResponse=xbmc.executehttpapi('getcurrentlyplaying')
    if rawResponse=='<li>Filename:[Nothing Playing]':
        if(media==""):
            if (playercontrol1=="Partymode(music)" or playercontrol2=="Partymode(music)"):
                xbmc.executebuiltin('XBMC.PlayerControl(Partymode(music))')
                xbmc.sleep(5000)
        elif(sfx==True):
            try:
                xbmc.playSFX(media)
            except:
                xbmc.log('playSFX failed')
        else:
            xbmc.Player().play(media)
            if playercontrol1!="":
                xbmc.executebuiltin('XBMC.PlayerControl('+playercontrol1+')')
            if playercontrol2!="":
                xbmc.executebuiltin('XBMC.PlayerControl('+playercontrol2+')')
            xbmc.sleep(5000)
    else:
        xbmc.sleep(2000)
Always read the XBMC online-manual, FAQ and search and search the forum before posting.
For troubleshooting and bug reporting please read how to submit a proper bug report.

If you're interested in writing addons for xbmc, read docs and how-to for plugins and scripts ||| http://code.google.com/p/xbmc-addons/
Reply
#6
Thanks for this script. I just put it in my XBMC install today and it works great.

I made a couple changes that others might find helpful, particularly if you use playlists.

1) Instead of "Random" as a playcontrol, you should probably be using "RandomOn" now -- I think "Random" is now a toggle so calling it repeatedly would turn random on and off.

2) If loading a playlist, even when using "RandomOn", the first song will start playing before the playlist is shuffled. This means the same song plays every time you start XBMC. If you want a random song every time you start XBMC, you can add xbmc.executebuiltin('XBMC.PlayerControl(Next)') to the script, so that the script immediately skips to the second (random) song. (Is there a "shuffle on load" function that do this in a simpler way?)

3) I also like to add a SetVolume([percent]) line in here. That way when I crank up the volume for movies, the music won't start blasting when the movie ends.
Reply
#7
Care to post your modified version?
-stoli-
Reply
#8
Here you go. I cleaned up my changes and added some comments, hopefully it is self-explanatory.
Code:
media="playlist.m3u"
# This can be a playlist, music, or video file or an Url.
playercontrol1="RandomOn"
playercontrol2=""
# "RandomOn", "RandomOff", "RepeatOne", "RepeatAll", "Partymode(music)", "Partymode(path to .xsp file)"
sfx=False
# True or False | Note that there are no quotes around True or False
# media needs to be a path to a .wav file if this is true
randomplaylist=True
# Set randomplaylist to True and playercontrol1 to RandomOn to start the playlist
# on the second (random) song. Otherwise set this to False.
volume="40"
# This is a percent between 0 and 100. 0 is mute and 100 is max volume.


####################################
#Don't mess with anything down here#
#unless you know what you're doing #
####################################

import nt,xbmc,xbmcgui
from os import path

xbmc.executehttpapi('SetResponseFormat&parameter=WebHeader;False;WebFooter;False')
# if (media!="" and not path.isfile(media)):
#    xbmcgui.Dialog().ok('Invalid Path','Path to media not found')

y=1
while y==1:
    rawResponse=xbmc.executehttpapi('getcurrentlyplaying')
    if rawResponse=='<li>Filename:[Nothing Playing]':
        xbmc.executebuiltin('SetVolume('+volume+')')
        if(media==""):
            if (playercontrol1=="Partymode(music)" or playercontrol2=="Partymode(music)"):
                xbmc.executebuiltin('XBMC.PlayerControl(Partymode(music))')
                xbmc.sleep(5000)
        elif(sfx==True):
            try:
                xbmc.playSFX(media)
            except:
                xbmc.log('playSFX failed')
        else:
            xbmc.executebuiltin('XBMC.PlayMedia('+media+')')
            if playercontrol1!="":
                xbmc.executebuiltin('XBMC.PlayerControl('+playercontrol1+')')
            if playercontrol2!="":
                xbmc.executebuiltin('XBMC.PlayerControl('+playercontrol2+')')
            if randomplaylist==True:
                xbmc.executebuiltin('XBMC.PlayerControl(Next)')
            xbmc.sleep(5000)
    else:
        xbmc.sleep(2000)
Reply
#9
Can I make this play a youtube playlist?
Reply
#10
It works perfectly with a music file but with a video file (not the goal of this script I know...), I have to press esc everytime I need the menu to appear....
Reply
#11
Question 
Sorry to be a noob, but I'm relatively new to this forum and I have always wanted this feature since I started playing about with XBMC. However, I have NO experience in programming and was wondering if someone could explain to me, step by step how to get this to work?
Reply
#12
Sorry to bring up an old thread. I would love this though and do not have the slightest clue how to begin installing the script here. I've looked all over the forums to no avail for some sort of guide or help, so if anyone could provide just a short tutorial for how to go about installing this script here that would be great! Thanks!
Reply
#13
In your userdata folder (in Windows 7 its under %appdata%\xbmc\userdata), create a text file called autoexec.py (if you do not have one) or edit your autoexec.py (if you have the file).

Put this in your autoexec.py:
Code:
#execute targeted script at startup.
import xbmc
xbmc.executescript('default.py')

This tells XBMC to run the script called "default.py" on startup.

Then make a text file called default.py and put in the script from the earlier posts.

Note that beginning with Eden, we're not supposed to be using autoexec.py scripts anymore. It'd be great if somebody could convert this to a proper service.
Reply
#14
PRE-EDEN NIGHTLY ONLY

After upgrading to a pre-Eden nightly build, I noticed that XBMC was taking a long time to quit/shutdown. Based on the debug log, the problem seemed to be that this script was taking a long time to terminate. I made some fixes so that it will quit more cleanly.

The main change is the use of the new "xbmc.abortRequested" function which checks if XBMC is shutting down. This is a new function in pre-Eden only so Dharma users should not use this script.

Other minor changes:
- The volume setting (volume="##") is now optional. Leave it as volume="" if you don't want to the script to reset the volume every time.
- I replaced the executehttpapi functions with the more straightforward xbmc.getCondVisibility('Player.HasMedia'). I'm not sure why executehttpapi was used in the first place; let me know if there is a good reason for keeping it.
- I changed the last "sleep" command from 2 seconds to 1 second (the script will now check every second). The main reason is so that there will be no more than 1 second delay when exiting XBMC.

Code:
media="c:\\music\\playlist.m3u"
# This can be a playlist, music, or video file or an Url.
playercontrol1="RandomOn"
playercontrol2=""
# "RandomOn", "RandomOff", "RepeatOne", "RepeatAll", "Partymode(music)", "Partymode(path to .xsp file)"
sfx=False
# True or False | Note that there are no quotes around True or False
# media needs to be a path to a .wav file if this is true
randomplaylist=True
# Set randomplaylist to True and playercontrol1 to RandomOn to start the playlist
# on the second (random) song. Otherwise set this to False.
volume=""
# If you want to reset the volume each time before the playlist starts, put a number in the "" quotes.
# The number is a percent between 0 and 100. 0 is mute and 100 is max volume.


####################################
#Don't mess with anything down here#
#unless you know what you're doing #
####################################

import nt,xbmc,xbmcgui
from os import path

# if (media!="" and not path.isfile(media)):
#    xbmcgui.Dialog().ok('Invalid Path','Path to media not found')

while (not xbmc.abortRequested):
    rawResponse=xbmc.getCondVisibility('Player.HasMedia')
    if rawResponse!=1:
        if(volume!="") : xbmc.executebuiltin('SetVolume('+volume+')')
        if(media==""):
            if (playercontrol1=="Partymode(music)" or playercontrol2=="Partymode(music)"):
                xbmc.executebuiltin('XBMC.PlayerControl(Partymode(music))')
                xbmc.sleep(5000)
        elif(sfx==True):
            try:
                xbmc.playSFX(media)
            except:
                xbmc.log('playSFX failed')
        else:
            xbmc.executebuiltin('XBMC.PlayMedia('+media+')')
            if playercontrol1!="":
                xbmc.executebuiltin('XBMC.PlayerControl('+playercontrol1+')')
            if playercontrol2!="":
                xbmc.executebuiltin('XBMC.PlayerControl('+playercontrol2+')')
            if randomplaylist==True:
                xbmc.executebuiltin('XBMC.PlayerControl(Next)')
            xbmc.sleep(5000)
    else:
        xbmc.sleep(1000)
Reply
#15
I thought "xbmc.getCondVisibility('Player.HasMedia')" would be more straightforward, but for some reason it interfered with some of my addons. (The Chinese Tudou and Youku addons would freeze when going through some dialogs.)

I replaced it with "xbmc.Player().isPlaying()" which is probably as straightforward as it gets. Seems to do the same thing and doesn't interfere with my addons.

Revised script is as follows:

Code:
media="c:\\music\\playlist.m3u"
# This can be a playlist, music, or video file or an Url.
playercontrol1="RandomOn"
playercontrol2=""
# "RandomOn", "RandomOff", "RepeatOne", "RepeatAll", "Partymode(music)", "Partymode(path to .xsp file)"
sfx=False
# True or False | Note that there are no quotes around True or False
# media needs to be a path to a .wav file if this is true
randomplaylist=True
# Set randomplaylist to True and playercontrol1 to RandomOn to start the playlist
# on the second (random) song. Otherwise set this to False.
volume=""
# If you want to reset the volume each time before the playlist starts, put a number in the "" quotes.
# The number is a percent between 0 and 100. 0 is mute and 100 is max volume.


####################################
#Don't mess with anything down here#
#unless you know what you're doing #
####################################

import nt,xbmc,xbmcgui
from os import path

# if (media!="" and not path.isfile(media)):
#    xbmcgui.Dialog().ok('Invalid Path','Path to media not found')

while (not xbmc.abortRequested):
    if not xbmc.Player().isPlaying():
        if(volume!="") : xbmc.executebuiltin('SetVolume('+volume+')')
        if(media==""):
            if (playercontrol1=="Partymode(music)" or playercontrol2=="Partymode(music)"):
                xbmc.executebuiltin('XBMC.PlayerControl(Partymode(music))')
                xbmc.sleep(5000)
        elif(sfx==True):
            try:
                xbmc.playSFX(media)
            except:
                xbmc.log('playSFX failed')
        else:
            xbmc.executebuiltin('XBMC.PlayMedia('+media+')')
            if playercontrol1!="":
                xbmc.executebuiltin('XBMC.PlayerControl('+playercontrol1+')')
            if playercontrol2!="":
                xbmc.executebuiltin('XBMC.PlayerControl('+playercontrol2+')')
            if randomplaylist==True:
                xbmc.executebuiltin('XBMC.PlayerControl(Next)')
            xbmc.sleep(5000)
    else:
        xbmc.sleep(1000)
Reply

Logout Mark Read Team Forum Stats Members Help
[REQUEST] Script that make music always play in background1