Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How to play video in allegro 5

This thread is locked; no one can reply to it. rss feed Print
How to play video in allegro 5
Crisbe
Member #16,579
October 2016

Hello I have problem with streaming video in the newest allegro 5.

I have:

#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>
#include <allegro5/allegro_video.h>

int main(){

al_install_audio();
al_init_acodec_addon();
al_init_video_addon();

ALLEGRO_VIDEO *splash_vid = al_open_video("vid.ogv");
ALLEGRO_MIXER *mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);

al_start_video(splash_vid, mixer);


al_close_video(splash_vid);
al_destroy_mixer(mixer);
return 0;
}

but after launch I have only black screen which can't be closed.
Can u help me solve this problem?

Edgar Reynaldo
Member #8,592
May 2007
avatar

You have to monitor video events and then display a frame when you get an ALLEGRO_EVENT_VIDEO_FRAME_SHOW event. You then get the bitmap to draw with al_get_video_frame. You need to register the video event source with an ALLEGRO_EVENT_QUEUE to receive video events.

Read the video section of the manual for details :
http://liballeg.org/a5docs/trunk/video.html

You have a black screen that can't be closed because you didn't create a display.

Crisbe
Member #16,579
October 2016

Now I have something like that and I created display earlier, in main fucntion:

void splash_screen(void)
{
ALLEGRO_VIDEO *splash_vid = al_open_video("vid.ogv");
ALLEGRO_MIXER *mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);

al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
ALLEGRO_BITMAP *bitmap = al_create_bitmap(al_get_video_scaled_width(splash_vid), al_get_video_scaled_height(splash_vid));

al_start_video(splash_vid, mixer);

std::cout << al_is_video_playing(splash_vid);

ALLEGRO_EVENT_QUEUE *vid_event_queue = al_create_event_queue();

al_register_event_source(vid_event_queue, al_get_video_event_source(splash_vid));

bool done = false;
while (!done)
{
ALLEGRO_EVENT vid_ev;
al_wait_for_event(vid_event_queue, &vid_ev);
switch (vid_ev.type)
{
case ALLEGRO_EVENT_VIDEO_FRAME_SHOW:
bitmap = al_get_video_frame(splash_vid);
al_draw_bitmap(bitmap, 0, 0, 0);
al_flip_display();
//std::cout << _LINE_ << ' ' << _FILE_ << std::endl;
al_clear_to_color(al_map_rgb(0, 0, 0));
break;
case ALLEGRO_EVENT_VIDEO_FINISHED:
done = true;
break;
}
}

al_destroy_event_queue(vid_event_queue);
al_close_video(splash_vid);
al_destroy_mixer(mixer);
}

and still I have black screen.

Edgar Reynaldo
Member #8,592
May 2007
avatar

Crisbe
Member #16,579
October 2016

Still it doesn't work :/
I can't event gets this events because this lines not executes

Edgar Reynaldo
Member #8,592
May 2007
avatar

Crisbe said:

Still it doesn't work :/
I can't event gets this events because this lines not executes

That's pretty vague. We need more details.

Post full code of a Minimum, Complete, Verifiable Example (MCVE) that fails to play video or fails to compile correctly and then we can help you.

If you video is under 10MB, post it as an attachment as well. Or upload it somewhere else so we can test it.

Crisbe
Member #16,579
October 2016

Ok, so this is my main function:

#include "All_headers.h"

ALLEGRO_TIMER *fps_timer = NULL;
ALLEGRO_DISPLAY *display = NULL;
int width = 640;
int height = 480;
int fps = 60;

int main(void)
{
//allegro variables
if (!al_init()) // test allegro
al_show_native_message_box(display, "Arcade Jump", "Error", "Error", "Error with allegro", ALLEGRO_MESSAGEBOX_WARN);

al_set_new_display_flags(ALLEGRO_WINDOWED | ALLEGRO_RESIZABLE);
display = al_create_display(width, height);
al_set_window_title(display, "Arcade Jump");
al_hide_mouse_cursor(display);

if (!display) //test display
al_show_native_message_box(display, "Arcade Jump", "Error", "Error", "Error with display", ALLEGRO_MESSAGEBOX_WARN);

fps_timer = al_create_timer(1.0 / fps);
al_start_timer(fps_timer);

//addon init
al_install_audio();
al_init_acodec_addon();
al_init_video_addon();
al_install_keyboard();
al_init_primitives_addon();
al_init_image_addon();

splash_screen();
menu();

// destroying globall variables
al_destroy_display(display);
al_destroy_timer(fps_timer);
return EXIT_SUCCESS;
}

Splash function:

#include "All_headers.h"

void splash_screen(void)
{
ALLEGRO_VIDEO *splash_vid = al_open_video("vid.ogv");
ALLEGRO_MIXER *mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);

al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
ALLEGRO_BITMAP *bitmap;

std::cout << al_is_video_playing(splash_vid);

ALLEGRO_EVENT_QUEUE *vid_event_queue = al_create_event_queue();

al_register_event_source(vid_event_queue, al_get_video_event_source(splash_vid));

al_start_video(splash_vid, mixer);

bool done = false;
while (!done)
{
ALLEGRO_EVENT vid_ev;
al_wait_for_event(vid_event_queue, &vid_ev);
if (vid_ev.type == ALLEGRO_EVENT_VIDEO_FRAME_SHOW)
{
bitmap = al_get_video_frame(splash_vid);
al_draw_bitmap(bitmap, 0, 0, 0);
al_flip_display();
std::cout << _LINE_ << ' ' << _FILE_ << std::endl;
al_clear_to_color(al_map_rgb(0, 0, 0));
}
else if (vid_ev.type == ALLEGRO_EVENT_VIDEO_FINISHED)
{
done = true;
break;
}
std::cout << _LINE_ << ' ' << _FILE_ << std::endl;
}

al_destroy_event_queue(vid_event_queue);
al_close_video(splash_vid);
al_destroy_mixer(mixer);
}

and headers:

#pragma once
// allegro extensions
#include <allegro5\allegro.h>
#include <allegro5\allegro_native_dialog.h>
#include <allegro5\allegro_image.h>
#include <allegro5\allegro_audio.h>
#include <allegro5\allegro_acodec.h>
#include <allegro5\allegro_video.h>
#include <iostream>

// splash screen
void splash_screen(void);

// global variables
extern ALLEGRO_TIMER *fps_timer;
extern ALLEGRO_DISPLAY *display;
extern int width;
extern int height;
extern int fps;

video file:
http://www79.zippyshare.com/v/d1HOqKmJ/file.html

Edgar Reynaldo
Member #8,592
May 2007
avatar

You can include code in <code>code goes here...</code> tags for easier reading and formatting.

Your video is only 1.41 MB. Please upload it as an attachment. I will not be using ZippyShare to install AdWare. Thanks.

EDIT
You still haven't set the drawing target to the backbuffer before drawing the video frame and flipping the display.

Crisbe
Member #16,579
October 2016

#SelectExpand
1void splash_screen(void) 2{ 3 ALLEGRO_VIDEO *splash_vid = al_open_video("vid.ogv"); 4 ALLEGRO_MIXER *mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); 5 6 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP); 7 ALLEGRO_BITMAP *bitmap; 8 9 std::cout << al_is_video_playing(splash_vid); 10 11 ALLEGRO_EVENT_QUEUE *vid_event_queue = al_create_event_queue(); 12 13 al_register_event_source(vid_event_queue, al_get_video_event_source(splash_vid)); 14 15 al_start_video(splash_vid, mixer); 16 17 bool done = false; 18 while (!done) 19 { 20 ALLEGRO_EVENT vid_ev; 21 al_wait_for_event(vid_event_queue, &vid_ev); 22 if (vid_ev.type == ALLEGRO_EVENT_VIDEO_FRAME_SHOW) 23 { 24 bitmap = al_get_video_frame(splash_vid); 25 al_set_target_backbuffer(display); 26 al_draw_bitmap(bitmap, 0, 0, 0); 27 al_flip_display(); 28 std::cout << __LINE__ << ' ' << __FILE__ << std::endl; 29 al_clear_to_color(al_map_rgb(0, 0, 0)); 30 } 31 else if (vid_ev.type == ALLEGRO_EVENT_VIDEO_FINISHED) 32 { 33 done = true; 34 break; 35 } 36 std::cout << __LINE__ << ' ' << __FILE__ << std::endl; 37 } 38 39 al_destroy_event_queue(vid_event_queue); 40 al_close_video(splash_vid); 41 al_destroy_mixer(mixer); 42}

Still no working, I can't figure why.
My event queue is all the time empty.

Edgar Reynaldo
Member #8,592
May 2007
avatar

It's not playing because you're never receiving an ALLEGRO_EVENT_VIDEO_FRAME_SHOWN event. Looks like a bug in Allegro. Could be due to an unsupported video format. .ogv is just a container for different video files.

EDIT
I've been having problems with DirectX and when I try to close my example program to play your .ogv file it deadlocks in al_close_video.

However, try ex_video to play your .ogv file. There doesn't appear to be anything wrong with the video file. VLC Media Player can play it just fine, and when I run "ex_video.exe vid.ogv" it plays fine. There may be something wrong with my build. I've been having other problems with DirectX.

What version of Allegro are you using? Are you using binaries? If so, what version?

Crisbe
Member #16,579
October 2016

So if you know, could you tell me which video format I can use ?

Edgar Reynaldo
Member #8,592
May 2007
avatar

Crisbe
Member #16,579
October 2016

I have Allegro 5.2.1.1 and I downloaded it from NuGet packages using Visual Studio 2015 Enterprise

EDIT

How can I use ex_video?

Edgar Reynaldo
Member #8,592
May 2007
avatar

Crisbe
Member #16,579
October 2016

Still don't know how to play video but I solved my problem. I rendered my video frame by frame in images and I am drawing them sequentially. Thanks for help and have a nice day :)

Edgar Reynaldo
Member #8,592
May 2007
avatar

Crisbe
Member #16,579
October 2016

Yes.

#SelectExpand
1void intro(void) 2{ 3 ALLEGRO_SAMPLE *intro_music = al_load_sample("Intro/Intro_music.ogg"); 4 ALLEGRO_BITMAP *image = NULL; 5 std::string count = "000000"; 6 std::string name = "Intro/Intro_"; 7 std::string png = ".png"; 8 std::string all_name; 9 std::string buffer; 10 bool done = false; 11 int c = 0, i = 0; 12 13 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); 14 al_register_event_source(event_queue, al_get_timer_event_source(fps_timer)); 15 al_register_event_source(event_queue, al_get_keyboard_event_source()); 16 17 al_reserve_samples(1); 18 al_play_sample(intro_music, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, 0); 19 20 while (!done) 21 { 22 ALLEGRO_EVENT ev; 23 al_wait_for_event(event_queue, &ev); 24 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) 25 { 26 if (ev.keyboard.keycode == ALLEGRO_KEY_SPACE) 27 done = true; 28 } 29 else if (ev.type == ALLEGRO_EVENT_TIMER) 30 { 31 all_name = name + count + png; 32 image = al_load_bitmap(all_name.c_str()); 33 al_draw_bitmap(image, 0, 0, 0); 34 al_flip_display(); 35 al_clear_to_color(al_map_rgb(0, 0, 0)); 36 al_destroy_bitmap(image); 37 c++; 38 count.clear(); 39 count = std::to_string(c); 40 while (buffer.length() + count.length() < 6) 41 buffer += '0'; 42 count = buffer + count; 43 while (count.length() > 6) 44 count.erase(count.begin()); 45 ++i; 46 } 47 if (i == 390) 48 done = true; 49 } 50 al_destroy_sample(intro_music); 51}

Edgar Reynaldo
Member #8,592
May 2007
avatar

Like I said, ex_video works, where our example programs do not. They must be doing something different. Look at the source of ex_video.c for clues. You can compile it yourself like any other normal allegro program now that you have Allegro from Nuget installed.

Go to: