6

I have been trying to write a simple audio ripper that i can use to learn how diffrent CODEC's work but i have got stuck on the first step, i cant get my program to read from the CD, the folowing code is what i have been trying to use

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;


public class learning 
{
    public static void main(String[] args) throws IOException 
    {   
        File cd = new File( "/dev/sr0" );
        RandomAccessFile rawAccess = new RandomAccessFile( cd, "r" );
        byte[] content = new byte[20];
        rawAccess.seek(19613);
        rawAccess.readFully(content);

        System.out.println(content);
    }

}

but it gives me the folowing error

Exception in thread "main" java.io.IOException: Input/output error
    at java.io.RandomAccessFile.readBytes(Native Method)
    at java.io.RandomAccessFile.read(RandomAccessFile.java:355)
    at java.io.RandomAccessFile.readFully(RandomAccessFile.java:414)
    at java.io.RandomAccessFile.readFully(RandomAccessFile.java:394)
    at learning.main(learning.java:21)

and i cant figure out why i get this, i though maby RandomFileAccess wasn't the right class to use but the only thing i could find said this should work

Any help on how to read CD's from java would be much appreciated.

Cheers Daniel

4 Answers4

3

First of all you should mount on a directory which the user logged in the OS Linux have access. E.g. : /mnt/cdrom or /media/cdrom

After that open your mp3 file or audio file :

File cd = new File( "/dev/sr0/track1.mp3" );

or

File cd = new File( "/dev/sr0/track1.dat" );

(don't forget the extension of the Audio or Mp3 file).

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
0

This isn't exactly the same, but I suspect it will get you going...

Is there a platform independent way (Java?) to read an audio CD's TOC?

If you don't want to use the built in stuff (for educational reasons), http://www.tritonus.org/ is open source so you might be able to look at how they did it.

Community
  • 1
  • 1
Gus
  • 6,719
  • 6
  • 37
  • 58
0

This question has a related problem and answer that is pertinent to your problem: Mount and unmount hard drives

According to the linked question and accepted answer, the answer is both "Yes" and "No". You can provide a Java API that use adapter pattern for native interface, but you will also have to do a number of things, thus making the solution not purely Java, but a hybrid:

  • create Java interfaces that support mount/unmount commands create
  • classes that implements interfaces as native methods create native
  • implementations of this commands in C or other language. One
  • implemantation for OS (Win, Mac, Linux) pack it to one jar build
  • small factory that provide implementation of interface and load native libraries
Community
  • 1
  • 1
jrd1
  • 10,358
  • 4
  • 34
  • 51
0

If /dev/sr0 is directory, then there is a problem. Please try passing one of the audio files as parameter e.g.

        File cd = new File( "/dev/sr0/track1" );

That should be working.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • When i try that it gives me an error for the line "RandomAccessFile rawAccess = new RandomAccessFile( cd, "r" );" saying that it isn't a directory – Daniel Braithwaite Oct 14 '12 at 10:23