1 /**App window for the FileSoundWindow test 2 @author Tim de Jong 3 @date 21/09/2002 4 @version 1.0 5 */ 6 7 8 #include "FileSoundWindow.h" 9 10 #include <Alert.h> 11 #include <Application.h> 12 #include <Box.h> 13 #include <Button.h> 14 #include <CheckBox.h> 15 #include <Entry.h> 16 #include <Path.h> 17 #include <TextControl.h> 18 19 #include <stdio.h> 20 #include <stdlib.h> 21 22 23 FileSoundWindow::FileSoundWindow(BRect windowBounds) 24 :BWindow(windowBounds,"BFileGameSound test",B_TITLED_WINDOW,B_NOT_ZOOMABLE|B_NOT_RESIZABLE) 25 { 26 //init private vars 27 loop = false; 28 preload = false; 29 playing = false; 30 paused = false; 31 rampTime = 250000; // 250 ms default 32 fileSound = 0; 33 //make openPanel and let it send its messages to this window 34 openPanel = new BFilePanel(); 35 openPanel -> SetTarget(this); 36 //get appBounds 37 BRect appBounds = Bounds(); 38 //make a cosmetic box 39 BBox *box = new BBox(appBounds); 40 //textcontrol to display the chosen file 41 BRect textBounds(appBounds.left + 10, appBounds.top + 10, appBounds.right - 70, appBounds.top + 20); 42 textControl = new BTextControl(textBounds,"filechosen","Play file:","No file chosen yet.", NULL); 43 textControl -> SetEnabled(false); 44 45 float x1 = textControl -> Bounds().left; 46 float x2 = textControl -> Bounds().right; 47 float dividerX = 0.20 * (x2 - x1); 48 textControl -> SetDivider(dividerX); 49 50 box -> AddChild(textControl); 51 //button to choose file 52 BRect browseBounds(textBounds.right + 5,textBounds.top, appBounds.right - 5, textBounds.bottom); 53 BMessage *browseMessage = new BMessage(BROWSE_MSG); 54 BButton *browseButton = new BButton(browseBounds,"browseButton","Browse" B_UTF8_ELLIPSIS,browseMessage); 55 box -> AddChild(browseButton); 56 //make play button 57 BRect playBounds(textBounds.left,textBounds.bottom + 15, textBounds.left + 50, textBounds.bottom + 25); 58 BMessage *playMessage = new BMessage(PLAY_MSG); 59 playButton = new BButton(playBounds,"playbutton","Play", playMessage); 60 box -> AddChild(playButton); 61 //make pause button 62 BRect pauseBounds(playBounds.right + 10,playBounds.top, playBounds.right + 60, playBounds.bottom); 63 BMessage *pauseMessage = new BMessage(PAUSE_MSG); 64 pauseButton = new BButton(pauseBounds,"pausebutton","Pause",pauseMessage); 65 box -> AddChild(pauseButton); 66 //make textcontrol to enter delay for pausing/resuming 67 BRect delayBounds(pauseBounds.right + 10, pauseBounds.top,pauseBounds.right + 150, pauseBounds.bottom); 68 delayControl = new BTextControl(delayBounds,"delay","Ramp time (ms)","250", new BMessage(DELAY_MSG)); 69 delayControl -> SetDivider(90); 70 delayControl -> SetModificationMessage(new BMessage(DELAY_MSG)); 71 box -> AddChild(delayControl); 72 //make loop checkbox 73 BRect loopBounds(playBounds.left,playBounds.bottom + 20, playBounds.left + 150, playBounds.bottom + 30); 74 BMessage *loopMessage = new BMessage(LOOP_MSG); 75 loopCheck = new BCheckBox(loopBounds,"loopcheckbox","Loop the sound file",loopMessage); 76 box -> AddChild(loopCheck); 77 //make preload checkbox 78 BRect preloadBounds(loopBounds.right + 10,loopBounds.top, loopBounds.right + 150, loopBounds.bottom); 79 BMessage *preloadMessage = new BMessage(PRELOAD_MSG); 80 preloadCheck = new BCheckBox(preloadBounds,"loopcheckbox","Preload the sound file",preloadMessage); 81 box -> AddChild(preloadCheck); 82 //add background box to window 83 AddChild(box); 84 } 85 86 FileSoundWindow::~FileSoundWindow() 87 { 88 } 89 90 void FileSoundWindow::MessageReceived(BMessage *message) 91 { 92 switch (message -> what) 93 { 94 case BROWSE_MSG: 95 openPanel -> Show(); 96 break; 97 98 case B_REFS_RECEIVED: 99 { 100 message->FindRef("refs", 0, &fileref); 101 BPath path(new BEntry(&fileref)); 102 textControl -> SetText(path.Path()); 103 } 104 break; 105 106 case PLAY_MSG: 107 { 108 if (!playing) 109 { 110 fileSound = new BFileGameSound(&fileref,loop); 111 status_t error = fileSound -> InitCheck(); 112 if (error != B_OK) 113 { 114 delete fileSound; 115 fileSound = 0; 116 if (error == B_NO_MEMORY) 117 { 118 BAlert *alert = new BAlert("alert","Not enough memory.","OK"); 119 alert -> Go(); 120 } 121 else if (error == B_ERROR) 122 { 123 BAlert *alert = new BAlert("alert","Unable to create a sound player.","OK"); 124 alert -> Go(); 125 } 126 else 127 { 128 BAlert *alert = new BAlert("alert","Other kind of error!","OK"); 129 alert -> Go(); 130 } 131 break; 132 } 133 paused = false; 134 pauseButton -> SetLabel("Pause"); 135 //preload sound file? 136 if (preload) 137 { 138 status_t preloadError = fileSound -> Preload(); 139 if (preloadError != B_OK) 140 { 141 BAlert *alert = new BAlert("alert","Port errors. Unable to communicate with the streaming sound port.","OK"); 142 alert -> Go(); 143 } 144 } 145 //play it! 146 status_t playerror = fileSound -> StartPlaying(); 147 if (playerror != B_OK) 148 { 149 if (playerror == EALREADY) 150 { 151 BAlert *alert = new BAlert("alert","Sound is already playing","OK"); 152 alert -> Go(); 153 } 154 else 155 { 156 BAlert *alert = new BAlert("alert","Error playing sound","OK"); 157 alert -> Go(); 158 } 159 } 160 else 161 { 162 playButton -> SetLabel("Stop"); 163 playing = true; 164 } 165 } 166 else 167 { 168 //stop it! 169 status_t stoperror = fileSound -> StopPlaying(); 170 if (stoperror != B_OK) 171 { 172 if (stoperror == EALREADY) 173 { 174 BAlert *alert = new BAlert("alert","Sound is already stopped","OK"); 175 alert -> Go(); 176 } 177 else 178 { 179 BAlert *alert = new BAlert("alert","Error stopping sound","OK"); 180 alert -> Go(); 181 } 182 } 183 else 184 { 185 playButton -> SetLabel("Play"); 186 playing = false; 187 } 188 delete fileSound; 189 fileSound = 0; 190 } 191 } 192 break; 193 194 case PAUSE_MSG: 195 { 196 if (!fileSound) 197 break; 198 199 int32 pausedValue = fileSound -> IsPaused(); 200 if (pausedValue != BFileGameSound::B_PAUSE_IN_PROGRESS) 201 { 202 status_t error; 203 const char *label; 204 if (paused) 205 { 206 paused = false; 207 label = "Pause"; 208 error = fileSound ->SetPaused(paused, rampTime); 209 } 210 else 211 { 212 paused = true; 213 label = "Resume"; 214 error = fileSound ->SetPaused(paused, rampTime); 215 } 216 217 if (error != B_OK) 218 { 219 if (error == EALREADY) 220 { 221 BAlert *alert = new BAlert("alert","Already in requested state","OK"); 222 alert -> Go(); 223 } 224 else 225 { 226 BAlert *alert = new BAlert("alert","Error!","OK"); 227 alert -> Go(); 228 } 229 } 230 else 231 { 232 pauseButton -> SetLabel(label); 233 } 234 } 235 } 236 break; 237 238 case LOOP_MSG: 239 { 240 int32 checkValue = loopCheck -> Value(); 241 if (checkValue == B_CONTROL_ON) 242 loop = true; 243 else loop = false; 244 } 245 break; 246 247 case PRELOAD_MSG: 248 { 249 int32 checkValue = preloadCheck -> Value(); 250 if (checkValue == B_CONTROL_ON) 251 preload = true; 252 else preload = false; 253 } 254 break; 255 256 case DELAY_MSG: 257 { 258 //set delay time for resuming/pausing 259 rampTime = atol(delayControl -> Text()) * 1000; 260 } 261 break; 262 } 263 264 } 265 266 bool FileSoundWindow::QuitRequested() 267 { 268 269 delete openPanel; 270 if (fileSound != 0 && fileSound -> IsPlaying()) 271 fileSound -> StopPlaying(); 272 delete fileSound; 273 be_app->PostMessage(B_QUIT_REQUESTED); 274 return(true); 275 } 276