04-10-2011 10:58 PM
Hi, I'm a beginning developer and I'm attempting to create an app in which the user selects an audio file on the first screen, which then takes them to a second screen where they will select how long they want the file to play.
I can't figure out how to make the app remember which audio file the user has selected on the first screen so that a timer can be set for the playback. I found a thread with a similar issue on this board but it didn't really solve my problem. Below, I've tried to call the same player on each screen, which of course did not work. If I leave the player out, it does not recognize the player p for the stop() I created on the timer screen.
If someone knows how to fix this, or where I can find the fix, I'd appreciate it.
Here's my code so far.
Screen 1:
public class TestSoundFileMainScreen extends MainScreen implements FieldChangeListener {
CustomButtonField sound1Button;
CustomButtonField sound2Button;
CustomButtonField sound3Button;
CustomButtonField sound4Button;
CustomButtonField sound5Button;
CustomButtonField sound6Button;
int field;
public void updateScreen(int x) {
field = x;
}
private void timer() {
TimerScreen timerScreen = new TimerScreen(this);
UiApplication.getUiApplication().pushScreen(timerS creen);
}
public TestSoundFileMainScreen() {
//code here for the appearance of the buttons, left out for brevity
Background headerBackground = BackgroundFactory.createSolidBackground(Color.BLAC K);
HorizontalFieldManager header = new HorizontalFieldManager(Field.FIELD_HCENTER);
add(new CustomLabelField ("Choose Your Sound", Color.WHITE, 0x000000, 0));
HorizontalFieldManager buttonManager = new HorizontalFieldManager(Field.FIELD_HCENTER);
Background blackBackground = BackgroundFactory.createSolidBackground(Color.GRAY );
buttonManager.setBackground(blackBackground);
buttonManager.add(sound1Button);
sound1Button.setChangeListener(this);
add(buttonManager);
add(new SeparatorField());
//and so on in this manner for the rest of the buttons...
}
public void fieldChanged(Field field, int context) {
if(field == sound1Button){
try {
InputStream inptstrm = getClass().getResourceAsStream("/sound1.wav");
final Player p = javax.microedition.media.Manager.createPlayer(inpt strm, "audio/x-wav");
p.realize();
VolumeControl volume = (VolumeControl)p.getControl("VolumeControl");
volume.setLevel(100);
p.prefetch();
p.setLoopCount(200);
p.start();
}catch(final Exception me){
UiApplication.getUiApplication().invokeAndWait(new Runnable() {
public void run() {
Dialog.inform(me.toString());
}
});
}
timer();}
if(field == sound2Button) {timer();}
if(field == sound3Button){timer();}
if(field == sound4Button){timer();}
if(field == sound5Button){timer();}
if(field == sound6Button){timer();}
invalidate();
}
}
Screen 2:
public class TimerScreen extends MainScreen {
CustomButtonField fiveMinutes;
CustomButtonField tenMinutes;
CustomButtonField fifteenMinutes;
CustomButtonField twentyMinutes;
CustomButtonField thirtyMinutes;
CustomButtonField fortyfiveMinutes;
CustomButtonField oneHour;
CustomButtonField twoHours;
TestSoundFileMainScreen _parent;
public TimerScreen(TestSoundFileMainScreen parent) {
_parent = parent;
//again, code creating button fields and their appearance
Background timerscreenBackground = BackgroundFactory.createSolidBackground(Color.BLAC K);
HorizontalFieldManager timerheader = new HorizontalFieldManager(Field.FIELD_HCENTER);
add(new CustomLabelField ("How Long Should It Play?", Color.WHITE, 0x000000, 0));
HorizontalFieldManager fiveManager = new HorizontalFieldManager(Field.USE_ALL_WIDTH);
Background fiveBackground = BackgroundFactory.createSolidBackground(Color.GRAY );
fiveManager.setBackground(fiveBackground);
fiveManager.add(fiveMinutes);
add(fiveManager);
add(new SeparatorField());
//code here in this same manner for the rest of the time period buttons
}
public void fieldChanged(Field fields, int context) {
if (fields == fiveMinutes) {
try {
InputStream inptstrm = getClass().getResourceAsStream("/thunder.wav");
final Player p = javax.microedition.media.Manager.createPlayer(inpt strm, "audio/x-wav");
p.realize();
VolumeControl volume = (VolumeControl)p.getControl("VolumeControl");
volume.setLevel(100);
p.prefetch();
p.setLoopCount(200);
p.start();
new Timer().schedule(new TimerTask() {
public void run() {
try { p.stop();
} catch (Exception e) {
Dialog.alert(e.toString());
}
}
}, 300000);
}catch(final Exception me){
UiApplication.getUiApplication().invokeAndWait(new Runnable() {
public void run() {
Dialog.inform(me.toString());
}
});
}
}
}
}
04-11-2011 04:04 AM
if you use a reference to the screen you can execute all public methods.
or you can use a common class, like a dataservice for example, which both screens can access.
04-12-2011 12:05 AM
that seems to be working in terms of recognizing the player... thanks for the help. I'll mark solved as soon as I get the timer working.
04-15-2011 07:07 PM - edited 04-15-2011 07:10 PM
Well this is what I've tried:
I added this reference to the class declaration:
TestSoundFileMainScreen _parent = new TestSoundFileMainScreen();
And I removed the second player instance from TimerScreen:
public void fieldChanges(Field fields, int context) {
_parent.updateScreen(field);
if (fields == fiveMinutes) {
new Timer().schedule(new TimerTask() {
public void run() {
try { p.stop();
} catch (Exception e) {
Dialog.alert(e.toString());
}
}
}, 300000);
}
}
But I am still getting the following errors:
public class TimerScreen extends MainScreen implements TestSoundFileMainScreen {
^
C:\dev\bin\com\apps\testsoundfile\TimerScreen.java :145: cannot find symbol
symbol : variable field
location: class com.apps.testsoundfile.TimerScreen
_parent.updateScreen(field);
^
C:\dev\bin\com\apps\testsoundfile\TimerScreen.java :153: cannot find symbol
symbol: variable p
try { p.stop();
^
3 errorsCan someone provide some examples of code that I can try? Thanks.