Results 1 to 6 of 6
Thread: How to return audio in a method?
- 12-27-2011, 04:28 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
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...
I know that for a void method, you can't return anything, so what would I replace "void" with?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; } }
Also, there seems to be no error for
So could anyone explain why please?Java Code:if(pressedDown) return; else return;
Thanks in advance! :)
-
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:
only exits the encapsulating method (without returning anything).Java Code:return;
It's useful in some cases, consider the following code for example:
that would save doing any unnecessary processing when there is none to be done for certain values/non-values of myObject.Java Code:public double calculateSomething(MyObject myObject) { if (myObject==null) return; //do some heavy processing ... }
- 12-27-2011, 05:01 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
Re: How to return audio in a method?
I changed it to
But I get an error for incompatible types. What am I doing wrong now?Java Code:public AudioClip getAudio(){ if(pressedLeft) return Sign.loop(); else return Sign.stop(); }
-
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:
Because I told the compiler that I would return AnObject, it expects me to return AnObject.Java Code:class SignTest { public AnObject getMyObject() { return Sign.loop(); } }
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.
- 12-27-2011, 03:52 PM #5
-
Similar Threads
-
Why is this method return 0
By africanhacker in forum New To JavaReplies: 7Last Post: 06-30-2011, 06:21 PM -
cant return value from a method
By gedas in forum New To JavaReplies: 2Last Post: 03-23-2011, 07:37 AM -
Not able to return the method value
By dmakshay2002 in forum Advanced JavaReplies: 11Last Post: 05-28-2010, 02:07 PM -
Method won't return value
By footyvino in forum New To JavaReplies: 2Last Post: 03-26-2010, 10:49 AM -
Return value of method
By cachi in forum New To JavaReplies: 1Last Post: 08-01-2007, 08:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks