Home Can audio be implemented in a static nested class?
Reply: 1

Can audio be implemented in a static nested class?

Nicki
1#
Nicki Published in 2018-01-13 04:40:58Z

So I've been trying to implement this audio into my main class. My main class contains other methods which I will not include in order to prevent my post from being too long. I think the code is going wrong because of out.audio audio = new out.audio (); because I either placed it in the wrong method or I'm not using the right variables. I'm still very much a beginner coder so I'm already kinda confused with what's going on in my code. What I do know though is that running just the audio class alone (not nested) works.

Am I formatting my nested class correctly and is it possible to nest audio?

import java.awt.*;
import hsa.Console;
import java.awt.image.*;
import java.util.Random;
import sun.audio.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
class out implements ImageObserver
{
//lots of code and other methods here so I'm going to leave it out
public static class audio extends Thread
{
    out.audio audio = new out.audio ();
    public void run ()
    {
        while (true)
        {
            AudioPlayer MGP = AudioPlayer.player;
            AudioStream BGM;
            AudioData MD;

            ContinuousAudioDataStream loop = null;

            try
            {
                InputStream test = new FileInputStream ("Music long.au");
                BGM = new AudioStream (test);
                AudioPlayer.player.start (BGM);
                //MD = BGM.getData();
                //loop = new ContinuousAudioDataStream(MD);

            }
            catch (FileNotFoundException e)
            {
                System.out.print (e.toString ());
            }
            catch (IOException error)
            {
                System.out.print (error.toString ());
            }
            MGP.start (loop);
            try
            {
                Thread.sleep (200000);
            }
            catch (InterruptedException ie)
            {
            }
            try
            {
                Thread.sleep (1500);
            }
            catch (InterruptedException ie)
            {
            }
        }
    }
    public static void main (String[] args)
    {
        c = new Console ();
        audio t1 = new audio ();
        t1.start ();
        for (;;)
        {
            c.println ("");
            break;
        }

    } // main method
} // audio class
} //out class
Ravi
2#
Ravi Reply to 2018-01-14 05:45:58Z

If you are asking when to use nested class, then answer is as per your requirement and design. You are free to use nested class, but sometimes it will make you hard to understand the design and changes to it, if it was not properly done.

I'm quoting Oracle Official Document

Compelling reasons for using nested classes include the following:

  1. It is a way of logically grouping classes that are only used in one place
  2. It increases encapsulation
  3. It can lead to more readable and maintainable code

==Updated==

As I mentioned, for me it doesn't make sense to have audio as inner class, but again its upto your design/requirement, also I'm not sure why did you declared your main method inside inner class ?? So, I was suggesting you

class out
{
    public static void main (String[] args)
    {
     //lots of code
    }
}

class ImageImp implements ImageObserver
{
//lots of code and other methods here so I'm going to leave it out

}

Then you should take out inner class

class audio extends Thread
{

    public void run ()
    {
        //code here
    }
}

Your design should be simple and should make some sense. Don't try to put everything inside one class until it is really needed.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.425336 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO