Problems with java and reading data from a CD in Linux

I am trying to write a simple sound ripper that I can use to find out how excellent CODEC works, but I got stuck in the first step, I can not get my program to read from the CD, the following code is what I tried 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 he gives me the following 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 can’t understand why I understood it, although maby RandomFileAccess was not a suitable class to use, but the only thing I could find was to say that this should work

Any help on reading a CD from java would be greatly appreciated.

Cheers Daniel

+6
source share
4 answers

First of all, you must connect to the directory to which the user registered in Linux has access. For example.: / 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" ); 

(do not forget to expand the audio or Mp3 file).

+3
source

This is not exactly the same, but I suspect it will make you go ...

Is there a platform independent way (Java?) For reading TOC audio CDs?

If you do not want to use the embedded material (for educational reasons), http://www.tritonus.org/ is open source so you can be able to see how they did it.

0
source

This question has a related problem and an answer that is relevant to your problem: Connect and disconnect hard drives

According to the related question and the accepted answer, the answer answers both β€œYes” and β€œNo”. . You can provide a Java API that uses an adapter template for your own interface, but you will also have to do a few things , which makes the solution not pure Java, but hybrid:

  • create Java interfaces supporting mount / unmount commands
  • classes that implement interfaces as native methods create their own
  • implementation of these commands in C or another language. One
  • for OS (Win, Mac, Linux) package for one jar assembly
  • small factory that provide the implementation of the interface and load the native libraries
0
source

If /dev/sr0 is a directory, then a problem arises. Try passing one of the audio files as a parameter, for example.

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

That should work.

0
source

Source: https://habr.com/ru/post/927662/


All Articles