Computer Science Canada

Media Player Help

Author:  Anonymous [ Thu Jun 22, 2006 4:23 pm ]
Post subject:  Media Player Help

Ok, here is a media player I'm making so it doesn't hog up as much power as Windows Media Player. But I'm having one small problem. I'm using setscreen("text"), and I was wondering how do I locate when I ask the user to enter a song back in the same position in "Selective mode", so I can remove the "Press Any Key To Exit, and just have the "Play which song" looping in 1 spot, and maybe have a press q to quit thing., so the user can switch tracks.

PS: It's still in proggress, I will make it more fancy maybe later on and add buttons or something.

code:

%MEDIA PLAYER

loop
    var winID := Window.Open ("graphics:0;0")
    var dir, song : string
    var cnt := 0
    dir := "C:/Documents and Settings/" + Sys.GetUserName + "/My Documents/My Music/"
    setscreen ("text")
    var streamNumber := Dir.Open (dir)
    var num : int
    var choice : char
    var fileName : string

    put "1 - Sequential"
    put "2 - Selective"
    loop
        choice := getchar
        exit when choice not= ""
    end loop
    put ""

    for i : 1 .. streamNumber
        fileName := Dir.Get (streamNumber)
        exit when fileName = ""
        if fileName (length (fileName)) = "3" then
            cnt += 1
            put cnt, ". ", fileName
            if (choice = '1') then
                Music.PlayFileReturn (dir + fileName)
                Input.Pause
                Music.PlayFileStop
            end if
        end if
    end for

    Dir.Close (streamNumber)

    put ""
    put "Play which song? " ..
    get song : *
    Music.PlayFileReturn (dir + song + ".mp3")
    put ""
    put "Press any key to exit " ..
    Input.Pause
    Music.PlayFileStop
    Window.Close (winID)
    return
end loop

Author:  Anonymous [ Thu Jun 22, 2006 4:34 pm ]
Post subject: 

Also, how would I locate so I can remove the .mp3 when I display the files in "Sequential and Selective" modes. Times like this I wish there was an edit button... Cause I had to delete my first post and this post once each to edit them.

Author:  aldreneo [ Thu Jun 22, 2006 6:48 pm ]
Post subject: 

Sorry, don't have an answer for you....But I agree this section of the forums needs an edit button...You would not beleve how many times I see someone correct a problem with his or her code

such as something like this:
code:

put "hi""


or

code:

print "hi"


or

code:

var cow:string;
get cow;
put cow;


or

code:

put ('Hi');

Author:  Delos [ Thu Jun 22, 2006 7:02 pm ]
Post subject: 

If you mean using locate() as in:
code:

locate (1, 1)
put "Hello world"


then I'm afraid that won't work in text-mode. There's a reason why it's called that Very Happy! Locate's are essentially a graphical manipulation of the screen, hence you have to do them in graphics mode. I don't see any problem with using graphics-mode over pure-text, since it offers your that flexibility while maintaining the minimalistic style of text-only.

@Edit button question - and just as many times as you've seen such questions, we've heard this same question. The answer is simple - not likely. It was removed a while ago for reasons of wanton abuse. That's pretty much all there is to it...but of course there still is that ever-useful Preview button whose use would somewhat diminish the need of an Edit button...

Author:  Dan [ Thu Jun 22, 2006 7:24 pm ]
Post subject:  Re: Media Player Help

vahnx wrote:
Ok, here is a media player I'm making so it doesn't hog up as much power as Windows Media Player.


Sorry to break it to you but a "hello world" program in turing with a loop of any kind that dose not exit right away will take up as much if not more power then windows media player cpu wise. Memeory wise it might be close but turing loads alot of undeed things, esptaly when made in to an exe.

Author:  Clayton [ Thu Jun 22, 2006 8:35 pm ]
Post subject: 

@Delos: it most certainly does Very Happy
code:

View.Set ("text")
locate (1,1)
put "Hello World"


will output Hello World at the top left hand corner of the screen. I think you are thinking of locatexy, which locates in pixels, not columns and rows, which is what text mode uses Very Happy, one character takes up one row and one column (ie. "L" at (1,1) will take up column 1 and row 1), in text mode instead of using maxx and maxy you use maxrow, and maxcol Very Happy

Author:  Delos [ Thu Jun 22, 2006 9:13 pm ]
Post subject: 

Curious.

This does not work:
code:

View.Set ("text")

put "Hello"
locate (2, 1)
put "World"


And neither does your code. You'd better double check to make sure. It crashes with:

Error.LastMsg wrote:

Output from "Text.Locate" cannot be sent to a text window.

Author:  Clayton [ Thu Jun 22, 2006 9:54 pm ]
Post subject: 

hm, that is weird, it works on my computer, i dont know why it wouldnt on yours, good luck finding out why

Author:  Anonymous [ Thu Jun 22, 2006 10:39 pm ]
Post subject: 

=(, Maybe instead of text I could use graphics... I will needa find out how to scroll tho, using the scrolly commandy thingy, whatever.

Author:  Delos [ Fri Jun 23, 2006 9:13 am ]
Post subject: 

SuperFreak82 wrote:
hm, that is weird, it works on my computer, i dont know why it wouldnt on yours, good luck finding out why


Though it should make no difference, what version of Turing are you running?

Author:  Anonymous [ Fri Jun 23, 2006 11:44 am ]
Post subject:  Back To My Media Player...

Ok, here is my Media Player So Far. I was wondering, how exactly do I scroll? I'm new to this GUI.Scroll thing, and I'm trying to use the increments, but the Help menu doesn't give me an example of GUI.**ScrollButtonFull.

Also, after I'm done scrolling, how would I allow the user to still Input, then if they don't want an Input, they can still scroll without having to enter an Input. Because once I have my Input statement, it doesn't allow me to scroll because it's waiting for an input.

code:

%MEDIA PLAYER
import GUI

var window1 := Window.Open ("graphics:1000;600,nobuttonbar")
var dir, song : string
var cnt := 0
dir := "C:/Documents and Settings/" + Sys.GetUserName + "/My Documents/My Music/"
var streamNumber := Dir.Open (dir)
var scrollBar : int
var num : int
var choice : char
var fileName : string

put "Hello ", Sys.GetUserName, " on ", Sys.GetComputerName
put ""
put "1 - Sequential (any key for next song)"
put "2 - Selective (type in name of song)"

loop
    choice := getchar
    exit when choice not= ""
end loop
put ""

for i : 1 .. streamNumber
    fileName := Dir.Get (streamNumber)
    exit when fileName = ""
    if fileName (length (fileName)) = "3" then
        cnt += 1
        put cnt, ". ", fileName
        if (choice = '1') then
            Music.PlayFileReturn (dir + fileName)
            Input.Pause
            Music.PlayFileStop
        end if
    end if
end for

Dir.Close (streamNumber)

procedure Scrolling (value : int)
    locate (20, 50)
    put value : 3
    GUI.Refresh
end Scrolling

scrollBar := GUI.CreateVerticalScrollBarFull (maxx - 20, 50, maxy - 50,
    1, cnt, 1, Scrolling, 0, 0, 0)

put ""
GUI.Refresh
loop
    % put "Play which song? " ..
    % get song : *
    % Music.PlayFileReturn (dir + song + ".mp3")
    % if File.Exists (dir + song + ".mp3") then
    %     put "Currently playing '", song, "'. " ..
    % else
    %     put "File does not exist! " ..
    % end if
    % put "Press any key for next song..."
    % Input.Pause
    % locate (maxrow - 2, 1)
    % put ""
    % locate (maxrow - 2, 1)
    % Music.PlayFileStop
    exit when GUI.ProcessEvent
end loop

Author:  Delos [ Fri Jun 23, 2006 12:26 pm ]
Post subject: 

GUI scrolling must be done with GUI objects. Right now, you're working with text objects. I'm afraid you can't have both - you have to choose. Either the minimalistic and functional advantage of text, or the prettiness and brain-busting of GUI.
If you do decide to go for the latter, my advice to you is not to use the Turing GUI. You are a competent enough programmer to create your own GUI - in fact, this will essentially allow you to get as close to the above mentioned compromise as possible. Creating a scroll box won't be too difficult, as far as such things go. Simply have a flexy array of each line, and some mouse-activated arrows that allow you to determine which lines are displayed.
As a related aside - one of the primary reasons why I discourage Turing GUI use is its own issues with itself. You've likely heard of instances where one programme causes another to have a hissie fit, but in Turing's case, one object in the GUI is incompatible with another. I forget exactly which ones were involved, but I think it was a text box and a text field - put them together wrongly and they simply wouldn't work.
These sorts of problems are avoidable if you can recreate your own customizable content. Check out Cervantes' custom text fields if you want a great example of what custom GUI looks like. (AFAIK, the S-Engine isn't custom GUI, it's just the standard one skinned - but I may be wrong).

Author:  Anonymous [ Fri Jun 23, 2006 10:40 pm ]
Post subject:  Ok All Good So Far

I was wondering, what's wrong with line 97 of this code. If you remove the Return on Music.PlayFileReturn, it works fine when you type the ID number. Otherwords, it doesn't play the song at all. (The message you get is true because the song you entered was a number if it's Music.PlayFileReturn.) I use mostly text mode.


code:

%MEDIA PLAYER
import GUI

var window0 := Window.Open ("0,0")
var mode : char

put "1-GUI   2-TEXT"
put "Choose A Mode: " ..
loop
    exit when (mode = '1') or (mode = '2')
    mode := getchar
end loop
Window.Close (window0)

if (mode = '2') then
    setscreen ("text")
else
    var window1 := Window.Open ("graphics:1000;2000,nobuttonbar")
end if

var dir, song, rename : string
var cnt := 0
dir := "C:/Documents and Settings/" + Sys.GetUserName + "/My Documents/My Music/"
var streamNumber := Dir.Open (dir)
var scrollBar : int
var num : int
var choice : char
var fileName : string
var songID : array 1 .. 200 of string

put "Hello ", Sys.GetUserName, " on ", Sys.GetComputerName, ". Welcome to the Media Player!"
put "Make Sure All Song Files Are In: "
put dir
put "NOTE: Only supports up to approximately 200 songs"
put ""
put "1 - Sequential (any key for next song)"
put "2 - Selective (type in name of song)"
put "3 - Rename Files (allows you to rename any music file)"

loop
    choice := getchar
    exit when choice not= ""
end loop
put ""

for i : 1 .. streamNumber
    fileName := Dir.Get (streamNumber)
    exit when fileName = ""
    if fileName (length (fileName)) = "3" then
        cnt += 1
        songID (cnt) := fileName
        put cnt, ". ", songID (cnt)
        if (choice = '1') then
            Music.PlayFileReturn (dir + fileName)
            Input.Pause
            Music.PlayFileStop
        end if
    end if
end for

Dir.Close (streamNumber)

procedure Scrolling (value : int)
    locate (20, 50)
    put value
    GUI.Refresh
end Scrolling

procedure Locate
    if (mode = '1') then
        locate (maxrow - 2, 1)
        put ""
        locate (maxrow - 2, 1)
    else
        put ""
    end if
end Locate

if (mode = '1') then
    scrollBar := GUI.CreateVerticalScrollBarFull (maxx - 50, 50, maxy - 50, 1, cnt, 1, Scrolling, 0, 0, 0)
end if

put ""
GUI.Refresh
loop
    if (choice = '2') then
        put "Play which song? " ..
    elsif (choice = '3') then
        put "Rename which song? " ..
    end if
    get song : *
    if (choice = '2') then

        for i : 1 .. streamNumber
            if strintok (song) then
                if strint (song) = i then
                    Music.PlayFileReturn (dir + songID (i)) %switch
                end if
            end if
        end for

        Music.PlayFileReturn (dir + song + ".mp3")
        if File.Exists (dir + song + ".mp3") then
            put "Currently playing '", song, "'. " ..
        else
            put "File does not exist! " ..
        end if
        put "Press any key for next song..."
        Input.Pause
        Locate
        Music.PlayFileStop


    elsif (choice = '3') then
        if File.Exists (dir + song + ".mp3") then
            put "What do you want the song to be renamed to? " ..
            get rename : *
            File.Rename (dir + song + ".mp3", dir + rename + ".mp3")
        else
            put "File does not exist!"
        end if
        Locate
    end if
    if (mode = '1') then
        exit when GUI.ProcessEvent
    end if
end loop

Author:  Anonymous [ Fri Jun 23, 2006 10:53 pm ]
Post subject: 

PS: I need to use PlayFileReturn so you can end the song at any time. Also, Music.PlayFile works fine, except for the fact I want to be able to end the song right away. I don't know why Music.PlayFileReturn would act up like this, could it be a Turing bug?

Author:  Anonymous [ Tue Jul 04, 2006 11:07 pm ]
Post subject: 

Can anyone help me, it's been over a week and still no help.

Author:  Cervantes [ Wed Jul 05, 2006 8:59 am ]
Post subject: 

SuperFreak82 and Delos: Regardless of how locate works for either of you in text mode, the fact is that it shouldn't work in text mode. Text mode is supposed to be like running your Turing program through the command line: all you can do is output text, line by line. No fancy graphics, no fancy text relocation.

vahnx: I'm having trouble reproducing your bug. Also, you should let the user specify where his music files are. Frankly, my windows partition is on F:\, and even if I changed it to that, I still don't keep my music files in ~\My Documents\My Music.

Author:  Anonymous [ Wed Jul 05, 2006 9:18 pm ]
Post subject: 

Ok for reassurance about the bug I recieve, I'm using Turing 4.1. Look down to line 97 where all the "////////////////" stick out. Excecute my code and press '2' then '2' to get into text mode and selective. Put a few songs in the directory, and it should see them. Each song has a number that represents it. When it says "Play which song?", enter a number that corrisponds with a song. It wont recognize it! But now, change "Music.PlayFileReturn" to "Music.PlayFile", and enter the same number you entered before which corrisponded with the song! Explain that to me please... Music.PlayFileReturn won't see the file, and Music.PlayFile does...

(PS Let me use the EDIT feature in the help menu, I prieview and it looks fine, and I dont notice errors in my posts til like 10 minutes later and stuff, I deleted copied and re-put html for this post 3 times now, and it happens lots. I wont abuse it like newbies to the site, I dont feel embarassed about why I need help and wouldnt edit my original help topics, I just need it to touch up spelling errors and wrong code posted)

code:


Code:

%MEDIA PLAYER
import GUI

var window0 := Window.Open ("0,0")
var mode : char

put "1-GUI   2-TEXT"
put "Choose A Mode: " ..
loop
    exit when (mode = '1') or (mode = '2')
    mode := getchar
end loop
Window.Close (window0)

if (mode = '2') then
    setscreen ("text")
else
    var window1 := Window.Open ("graphics:1000;2000,nobuttonbar")
end if

var dir, song, rename : string
var cnt := 0
dir := "C:/Documents and Settings/" + Sys.GetUserName + "/My Documents/My Music/"
var streamNumber := Dir.Open (dir)
var scrollBar : int
var num : int
var choice : char
var fileName : string
var songID : array 1 .. 200 of string

put "Hello ", Sys.GetUserName, " on ", Sys.GetComputerName, ". Welcome to the Media Player!"
put "Make Sure All Song Files Are In: "
put dir
put "NOTE: Only supports up to approximately 200 songs"
put ""
put "1 - Sequential (any key for next song)"
put "2 - Selective (type in name of song)"
put "3 - Rename Files (allows you to rename any music file)"

loop
    choice := getchar
    exit when choice not= ""
end loop
put ""

for i : 1 .. streamNumber
    fileName := Dir.Get (streamNumber)
    exit when fileName = ""
    if fileName (length (fileName)) = "3" then
        cnt += 1
        songID (cnt) := fileName
        put cnt, ". ", songID (cnt)
        if (choice = '1') then
            Music.PlayFileReturn (dir + fileName)
            Input.Pause
            Music.PlayFileStop
        end if
    end if
end for

Dir.Close (streamNumber)

procedure Scrolling (value : int)
    locate (20, 50)
    put value
    GUI.Refresh
end Scrolling

procedure Locate
    if (mode = '1') then
        locate (maxrow - 2, 1)
        put ""
        locate (maxrow - 2, 1)
    else
        put ""
    end if
end Locate

if (mode = '1') then
    scrollBar := GUI.CreateVerticalScrollBarFull (maxx - 50, 50, maxy - 50, 1, cnt, 1, Scrolling, 0, 0, 0)
end if

put ""
GUI.Refresh
loop
    if (choice = '2') then
        put "Play which song? " ..
    elsif (choice = '3') then
        put "Rename which song? " ..
    end if
    get song : *
    if (choice = '2') then

        for i : 1 .. streamNumber
            if strintok (song) then
                if strint (song) = i then
                    Music.PlayFileReturn (dir + songID (i)) %switch////////////////////////////////////////////////////////////////
                end if
            end if
        end for

        Music.PlayFileReturn (dir + song + ".mp3")
        if File.Exists (dir + song + ".mp3") then
            put "Currently playing '", song, "'. " ..
        else
            put "File does not exist! " ..
        end if
        put "Press any key for next song..."
        Input.Pause
        Locate
        Music.PlayFileStop


    elsif (choice = '3') then
        if File.Exists (dir + song + ".mp3") then
            put "What do you want the song to be renamed to? " ..
            get rename : *
            File.Rename (dir + song + ".mp3", dir + rename + ".mp3")
        else
            put "File does not exist!"
        end if
        Locate
    end if
    if (mode = '1') then
        exit when GUI.ProcessEvent
    end if
end loop

Author:  Cervantes [ Wed Jul 05, 2006 9:32 pm ]
Post subject: 

vahnx wrote:

(PS Let me use the EDIT feature in the help menu, I prieview and it looks fine, and I dont notice errors in my posts til like 10 minutes later and stuff, I deleted copied and re-put html for this post 3 times now, and it happens lots. I wont abuse it like newbies to the site, I dont feel embarassed about why I need help and wouldnt edit my original help topics, I just need it to touch up spelling errors and wrong code posted)

Help menu?

Anyways, I don't think it would be easy to give just you access to this, without making you a mod, which would only allow you to edit your posts because you can edit everybody's posts.

As for your code, I can now get the bug. However, I get the same, "File does not exist" message regardless of whether I'm using Music.PlayFile or Music.PlayFileReturn?

Could you explain these lines of code?
code:

        for i : 1 .. streamNumber
            if strintok (song) then
                if strint (song) = i then
                    Music.PlayFile (dir + songID (i))
                end if
            end if
        end for

        Music.PlayFileReturn (dir + song + ".mp3")

Specifically, the for loop. It looks extremely inefficient.

First, why not move that if strintok outside of the for loop? song is not altered by any code in that for loop, so the conditional strintok (song) doesn't ever change, so why recalculate it over and over again?

Second, why do you have a for loop in the first place? How about something like this:
code:

if strintok (song) then
    Music.PlayFile (dir + songID (strint (song)))
end if

That should be identical, except without all the needless iteration.

Author:  Anonymous [ Thu Jul 06, 2006 11:23 pm ]
Post subject: 

I have no idea why I did that. Im losing my Turing skills I think, lol. I havent been using it for while.


: