[ Exercice ] Semi command line audio player With a Stop Button

Hi,

I have done this this afternoon mixing some tips and tutorials:

#include <Application.h>
#include <Window.h>
#include <FileGameSound.h>

#include <Button.h>
#include <iostream>
const int32 STOP_PLAYING ='STOP';
char * file2Play=NULL;
class MainWin:public BWindow{
	public:
		BFileGameSound *player= new BFileGameSound(file2Play,false,NULL);
		
		MainWin():BWindow(BRect(100,100,250,150),"Player",B_TITLED_WINDOW,0)
		{
			
			BButton *button = new BButton(Bounds(),NULL,"Stop",new BMessage(STOP_PLAYING));
			AddChild(button);
			play(1);
			if(player->IsPaused()==2)
				play(0);
		}
		
		
		bool QuitRequested(){
			MessageReceived(new BMessage(STOP_PLAYING));
			be_app_messenger.SendMessage(B_QUIT_REQUESTED);
			return BWindow::QuitRequested();
		}
		
		int play(int play){
			if(player->InitCheck() != B_OK ){
				return 1;
			}
			if(play==1){
				player->Preload();
				if(player->StartPlaying() !=B_OK)
					return 1;
			}
			if(play==0)			
				player->StopPlaying();
				
			return 0;
		}
		
		void MessageReceived(BMessage *msg)
		{

			switch(msg->what){
				case STOP_PLAYING:
					play(0);
					std::cout << "Stop: " << file2Play << std::endl;
					break;
				default:
					BWindow::MessageReceived(msg);
			}
		}
};

class MainApp: public BApplication
{
	public:
	MainApp():BApplication("application/test")
	{
	}
	void ReadyToRun()
	{
		BWindow *win = new MainWin();
		
		win->Show();
		
	}
};

int main(int argc,char *argv[]){
	if(argv[1]!=NULL){
	file2Play=argv[1];
	MainApp app;
		app.Run();
	}
	else
		std::cout << "Usage: player nameOfAudioFile.mp3" << std::endl;
	return 0;
}

It’s a semi command line audio player.
For now you must use it like a command line player to have a music in playback and it show a “Stop” button.

it use libbe and libgame

compile:
g++ main.cpp -o player -lbe -lbgame
Usage:
player NameOfMP3.mp3

I have a trouble about buffer I think, if you have some advice about it?

Apparently this trouble is about Virtual Box emulation of the Sound Card. In my native Haiku Installation this code work without trouble. But some others Applications don’t have this issue on Virtual Box.

Thanks.

4 Likes