2 * Copyright 2017, Dario Casalinuovo. All rights reserved.
3 * Copyright 2005, Marcus Overhagen, marcus@overhagen.de. All rights reserved.
4 * Copyright 2005, Jérôme Duval. All rights reserved.
5 * Distributed under the terms of the MIT License.
11 #include <MediaFile.h>
12 #include <MediaTrack.h>
14 #include <SoundPlayer.h>
22 thread_id reader
= -1;
24 BMediaTrack
* playTrack
;
25 media_format playFormat
;
26 BSoundPlayer
* player
= 0;
27 volatile bool interrupt
= false;
31 play_buffer(void *cookie
, void * buffer
, size_t size
, const media_raw_audio_format
& format
)
35 // Use your feeling, Obi-Wan, and find him you will.
36 playTrack
->ReadFrames(buffer
, &frames
);
39 player
->SetHasData(false);
40 release_sem(finished
);
48 // Are you threatening me, Master Jedi?
50 release_sem(finished
);
54 int media_play(const char* uri
)
60 if (get_ref_for_path(uri
, &ref
) != B_OK
) {
61 url
.SetUrlString(uri
);
63 playFile
= new BMediaFile(url
);
67 playFile
= new BMediaFile(&ref
);
69 if (playFile
->InitCheck() != B_OK
) {
74 for (int i
= 0; i
< playFile
->CountTracks(); i
++) {
75 BMediaTrack
* track
= playFile
->TrackAt(i
);
76 playFormat
.type
= B_MEDIA_RAW_AUDIO
;
77 if ((track
->DecodedFormat(&playFormat
) == B_OK
)
78 && (playFormat
.type
== B_MEDIA_RAW_AUDIO
)) {
83 playFile
->ReleaseTrack(track
);
86 // Good relations with the Wookiees, I have.
87 signal(SIGINT
, keyb_int
);
89 finished
= create_sem(0, "finish wait");
91 printf("Playing file...\n");
94 player
= new BSoundPlayer(&playFormat
.u
.raw_audio
, "playfile", play_buffer
);
95 player
->SetVolume(1.0f
);
97 // Join me, Padmé and together we can rule this galaxy.
98 player
->SetHasData(true);
101 acquire_sem(finished
);
103 if (interrupt
== true) {
104 // Once more, the Sith will rule the galaxy.
105 printf("Interrupted\n");
110 printf("Playback finished.\n");