Practice Exercises Java - Learn to program performing exercises with Java

Practice Exercises Java

Learn to program performing exercises with Java

12 Lessons Java with the Solutions - 228 Exercises Java with the solutions
For Beginners, Intermediates and Advanceds


App Practice Exercises Java

The human knowledge belongs to the world
¡The infomation should be free!



MP3 reader - Practice Exercises Java


Lesson 8:

File management


Exercise 8.26:

MP3 reader


Objetive:

ID3 specifications apply to any file or audiovisual container. However, it is usually applied primarily audio containers. There are three versions of the specification that are compatible. For example, a file may contain labels simultaneously version 1.1 and version 2.0. In this case, the media player must decide which are relevant.

ID3 version 1
This first specification is very simple. It consists of attaching a fixed block size of 128 bytes at the end of the file in question. This block contains the following tags: A header that identifies the presence of block ID3 and version. Specifically, said header comprises TAG characters.

Title: 30 characters.
Artist: 30 characters.
Album: 30 characters.
Year: 4 characters.
Comment: 30 characters.
Genre (music): a character.

All tags using ASCII characters except gender, an integer stored within a single byte. The musical genre associated with each byte is predefined in the standard definitions and includes 80 genera, numbered 0 to 79. Some breeding programs have expanded their own defined genres (from 80th).


Source Code:


import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		final int SIZE = 128;

		byte[] data;

		System.out.print("Enter name: ");
		String name = new Scanner(System.in).nextLine();

		if (!(new java.io.File(name)).isFile())
		{
			System.out.println("Not exists");
			return;
		}

		try
		{
			java.io.FileInputStream file = File.OpenRead(name);

			data = new byte[SIZE];

			file.Seek(-128, SeekOrigin.End);
			file.read(data, 0, SIZE);

			file.close();

			byte b1 = data[0];
			byte b2 = data[1];
			byte b3 = data[2];

			if ((char)b1 != 'T' || (char)b2 != 'A' || (char)b3 != 'G')
			{
				System.out.println("not mp3 valid");
				return;
			}

			int i = 3;

			String title = "";
			for (; i < 33; i++)
			{
				if (data[i] != 0)
				{
					title += (char)data[i];
				}
			}

			String author = "";
			for (i = 33; i < 63; i++)
			{
				if (data[i] != 0)
				{
					author += (char)data[i];
				}
			}

			String album = "";
			for (i = 63; i < 93; i++)
			{
				if (data[i] != 0)
				{
					album += (char)data[i];
				}
			}

			String year = "";
			for (i = 93; i < 97; i++)
			{
				if (data[i] != 0)
				{
					year += (char)data[i];
				}
			}

			String comments = "";
			for (i = 97; i < 127; i++)
			{
				if (data[i] != 0)
				{
					comments += (char)data[i];
				}
			}

			System.out.println("Data of MP3:");
			System.out.println("----------------------------");
			System.out.println();

			System.out.println("Title: " + title);
			System.out.println("Author: " + author);
			System.out.println("Album: " + album);
			System.out.println("Year: " + year);
			System.out.println("Comments: " + comments);
			System.out.println("Genre: " + data[127]);
		}
		catch (RuntimeException e)
		{
			System.out.println("Error");
		}
	}
}
Exercisey 8.26




Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.