Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Media Player Help
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Prince Pwn




PostPosted: Wed Feb 21, 2007 11:26 pm   Post subject: Media Player Help

My teacher found a way to play a variety of audio formats (including .wav, .mp3) in Java, and I wanna make a media player. I wish to "scan" a directory for mp3's and add all of them into an array. I'm not very firmilliar with Java, but in Turing it's similar to:
Turing:

const dir := "C:/Users/" + Sys.GetUserName + "/Music/"
var stream := Dir.Open (dir)
for i : 0 .. stream
    var file := Dir.Get (stream)
    if file (length (file)) = '3' then
        put file
    end if
end for


How would I be able to do the same for Java? My teacher doesn't know and is still teaching the other students for loops.
Sponsor
Sponsor
Sponsor
sponsor
klopyrev




PostPosted: Thu Feb 22, 2007 1:59 am   Post subject: Re: Media Player Help

After a couple minutes of researching, I came up with this code. It finds every file with the extension .wav in the directory and plays it. I found out about sun.audio.AudioPlayer on the internet. This code may not even work in JAVA 1.5, since I only have 1.4, but my point is: do some research. There is a lot of information on the internet. Also, if you haven't learned it already, your best friend in JAVA is the documentation. I found out that the File class can actually list all the files in a directory after looking in the documentation. The java library is vast - there are many very useful classes there. I guarantee you that there are at least 5 ways to play an audio file in java. This is just one I found (probably not the best):

import java.io.*;
import sun.audio.*;
public class Test
{
public static void main (String[] args) throws IOException
{
File currentDirectory = new File (".");
class TheFilter implements FilenameFilter
{
public boolean accept (File dir, String name)
{
int index = name.lastIndexOf ('.');
if (name.substring (index + 1).toLowerCase ().equals ("wav"))
return true;
return false;
}
}
File[] list = currentDirectory.listFiles (new TheFilter ());
if (list.length != 0)
{
InputStream in = new FileInputStream (list [0].getName ());
AudioStream as = new AudioStream (in);
AudioPlayer.player.start (as);
}
}
}
Prince Pwn




PostPosted: Tue Mar 06, 2007 7:58 pm   Post subject: RE:Media Player Help

Ok that's awesome. You know how you have the '.' for current directory, it wont work for eg: "C:\\Users\\USERNAME\\Music\\" in Vista for me. It just executes and ends. I don't want the Java program to be with all my music files.

Also, is there a command to print the user name so I can automate it, and possibly detect operating system?

eg:
Turing:


And for the OS,
"if OS = "xp" then
directory = c:\\documents and settings\\USERNAME\\my documents\\my music\\"
or if OS = "Vista" then
directory = c:\\users\\USERNAME\\music\\".

Thanks.
ericfourfour




PostPosted: Fri Mar 09, 2007 1:02 am   Post subject: Re: Media Player Help

Google search string = "java windows user name"

First web page = http://forum.java.sun.com/thread.jspa?threadID=440883&messageID=1991088

4 posts down the page =
DeadAir wrote:
If you want the user who is logged on to the server that is running your JSP this will do it.

System.getProperty("user.name");


I tried running a test program with the following code:
Java:
System.out.println(System.getProperty("user.name"));


The output was the following:
Eric


Gee, a few minutes of research wouldn't kill you.

Now you try it. Find a way to detect the operating system in Java (I found it. I had the same results as the former search except the answer was 3 posts down instead of 4).
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: