Results 1 to 6 of 6
  1. #1
    ad54me is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Exclamation How to return audio in a method?

    So I am doing a "Snowman" project for school and I want to output sound/music (whatever's possible) for my program.

    I made a method, playAudio(), but I don't know what to classify it as...

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    
    public class Audio extends Applet
    {
    
    
    Image Buffer;
        Graphics gBuffer;
        boolean pressedLeft=true, pressedRight=false, pressedUp=false, pressedDown=false;
        AudioClip FightingDreamer;
        AudioClip Sign;
        AudioClip SadnessSorrow;
        
        
        
        public void init()
        {
           Buffer=createImage(size().width,size().height);
           gBuffer=Buffer.getGraphics();
    
           try
           {
                FightingDreamer=getAudioClip(getCodeBase(),"Fighting_Dreamer.wav");
                Sign=getAudioClip(getCodeBase(),"Sign.wav");
                SadnessSorrow=getAudioClip(getCodeBase(),"Sadness_and_Sorrow.wav");
           }//try
           catch (Exception e)
           {
           }//catch
        }//init
    	
    	public boolean keyDown(Event e, int key)
        {
           if(key==Event.LEFT)
           pressedLeft=true;
    
           if(key==Event.RIGHT)
           pressedRight=true;
    
           if(key==Event.UP)
           pressedUp=true;
           if(key==Event.DOWN)
           pressedDown=true;
    
           if(key=='s'||key=='S')
               Sign.play();
    
           repaint();
    
           return true;
        }
    	
    	public boolean keyUp(Event e, int key)
        {
           pressedLeft=pressedRight=pressedUp=pressedDown=false;
    
           repaint();
    
           return true;
        }
        
        public void playAudio(){
    	if(pressedLeft)
              return Sign.loop();
          else
              return Sign.stop();
        
        if(pressedRight)
              return SadnessSorrow.loop();
          else
              return SadnessSorrow.stop();
              
        if(pressedUp)
              return FightingDreamer.loop();
          else
              return FightingDreamer.stop();
        
        if(pressedDown)
              return;
          else 
              return;
        }
            
    }
    I know that for a void method, you can't return anything, so what would I replace "void" with?
    Also, there seems to be no error for

    Java Code:
    if(pressedDown)
              return;
          else 
              return;
    So could anyone explain why please?

    Thanks in advance! :)

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: How to return audio in a method?

    Replace "void" with the type of Object or primitive you want to pass back.

    In this case, its an AudioClip object. So your method signature should look something like:

    public AudioClip getAudioClip();


    As for the second question, the following code:
    Java Code:
    return;
    only exits the encapsulating method (without returning anything).

    It's useful in some cases, consider the following code for example:

    Java Code:
    public double calculateSomething(MyObject myObject) {
      if (myObject==null) return;
      //do some heavy processing
      ...
    }
    that would save doing any unnecessary processing when there is none to be done for certain values/non-values of myObject.

  3. #3
    ad54me is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: How to return audio in a method?

    I changed it to

    Java Code:
    public AudioClip getAudio(){
    	if(pressedLeft)
              return Sign.loop();
          else
              return Sign.stop();
    }
    But I get an error for incompatible types. What am I doing wrong now?

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: How to return audio in a method?

    Incompatible type means you are returning the wrong Object.

    If I have a class like this:

    Java Code:
    class Sign {
    
      private static AnotherObject anotherObject;
    
      public static AnotherObject loop() {
        ...
        anotherObject = ...;
        return anotherObject;
      }
    
    }

    And then I create a method like this:

    Java Code:
    class SignTest {
    
        public AnObject getMyObject() { 
           return Sign.loop();
        }
    
    }
    Because I told the compiler that I would return AnObject, it expects me to return AnObject.

    Instead I returned the whatever the result of Sign.loop() is.

    In this case, Sign.loop() returned AnotherObject.

    Therefore, the compiler expected AnObject but I gave it AnotherObject.

    AnotherObject is not a subclass of AnObject, neither is it an instance of AnObject so it is incompatible with AnObject and there is an error thrown.
    Last edited by ozzyman; 12-27-2011 at 05:42 AM.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: How to return audio in a method?

    Quote Originally Posted by ozzyman View Post
    Replace "void" with the type of Object or primitive you want to pass back.

    In this case, its an AudioClip object. So your method signature should look something like:

    public AudioClip getAudioClip();


    As for the second question, the following code:
    Java Code:
    return;
    only exits the encapsulating method (without returning anything).

    It's useful in some cases, consider the following code for example:

    Java Code:
    public double calculateSomething(MyObject myObject) {
      if (myObject==null) return;
      //do some heavy processing
      ...
    }
    that would save doing any unnecessary processing when there is none to be done for certain values/non-values of myObject.
    You can't return; from a method that has a return type other than void. You have to return a value.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: How to return audio in a method?

    Oh yes of course thanks for spotting that db.

Similar Threads

  1. Why is this method return 0
    By africanhacker in forum New To Java
    Replies: 7
    Last Post: 06-30-2011, 06:21 PM
  2. cant return value from a method
    By gedas in forum New To Java
    Replies: 2
    Last Post: 03-23-2011, 07:37 AM
  3. Not able to return the method value
    By dmakshay2002 in forum Advanced Java
    Replies: 11
    Last Post: 05-28-2010, 02:07 PM
  4. Method won't return value
    By footyvino in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 10:49 AM
  5. Return value of method
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 08:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •