#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    2

    Pymedia with index


    Hello, I'm now trying to solve two problems in pymedia:
    1. Is there any way to set up an index of my playlist? because I want to control my playlist to play 'next' or 'previous' song. Here's the code:

    Code:
    import pymedia
    player = pymedia.Player()
    player.start()
    ListSong =('C:\LoveRecipe.mp3','C:\LoveYou.mp3','C:\ItsCold.mp3')   
    
    i = 0
    
    def playSong():
       player.startPlayback(ListSong[i])
    
    def NxtSong():
       
       player.startPlayback(ListSong[i+1])
    
    def PrevSong():
       player.startPlayback(ListSong[i-1])
    Can somebody please tell me how can I use "i" as a global index to control these 3 functions perfectly? In this way I know that the index "i" can not go further than "1" but I don't know how to set the index for i correctly, please help.

    and as you can see in my ListSong, it has to fill the song manually so I'd like to ask you. Is there a way to load the song just once without filling manually? like ListSong = 'C:\MusicFolder' and it will play automatically from the first song in that folder.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Code:
    def NxtSong():
       global i
       i += 1
       player.startPlayback(ListSong[i])
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    2
    Ohhh~hh you help me again!!! I'm glad to have your answer, thank you so much !! By the way, do you have any idea about load music from one folder or not? because I have to write the file's path manually this way, it's not comfortable at all.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by MynE
    Ohhh~hh you help me again!!! I'm glad to have your answer, thank you so much !! By the way, do you have any idea about load music from one folder or not? because I have to write the file's path manually this way, it's not comfortable at all.
    This will create a list of all files in "C:\MusicFolder"

    Code:
    import os
    file_list = []
    for file in os.listdir('C:\MusicFolder'):
        file_list.append(file)
    NB, when I tried this the list will not be sorted alphabetically.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Once data is digital, I don't need to retype it. On the other hand, I spent time learning to use the command shell and a few programming languages.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    2
    Thank you to you two, I followed your code and I can list my song very well. The problem is my Pymedia wants the full path of the song in order to play so I add:

    Code:
    ListSong = []
    for file in os.listdir('C:\MusicLibrary'):
       ListSong.append('C:\MusicLibrary'+file)
    Here is the result of my ListSong: ['C:\\MusicLibraryItsCold.mp3', 'C:\\MusicLibraryLoveRecipe.mp3', 'C:\\MusicLibraryLoveYou.mp3']

    As you can see it has no '\' between MusicLibrary and the song. I've been trying to add this guy into string to make it works properly but i couldn't. Do you have any idea to join this '/' into list? It should be 'C:\\MusicLibrary\ItsCold.mp3'
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    import os
    os.path.join('C:\MusicLibrary',file)

    or

    os.path.abspath(file)


    Other than that, choose variable names that differ from builtin names. file is a poor choice.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    2
    Wow, thank you so much sir!!!!

IMN logo majestic logo threadwatch logo seochat tools logo