Skip to content

Instantly share code, notes, and snippets.

@beauchamplab
Created May 4, 2022 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beauchamplab/cae6af3a259161e8ba22ae25ec4a30a9 to your computer and use it in GitHub Desktop.
Save beauchamplab/cae6af3a259161e8ba22ae25ec4a30a9 to your computer and use it in GitHub Desktop.
Psychtoolbox Video Player
%% Simple movie player
% Michael Beauchamp 4 May 2022
function play_movies()
movie_dir = [ pwd '/']; % directory movies are in; default is current working directory
movienames = {'thoughts_danielle.mp4','jet_heidi_a.mp4'}; % filenames of movies to play
trials = length(movienames); % number of total movies
num_videos = 0; % counter that keeps track of which movie we are currently playing
try
% Abort if we don't have OpenGL
AssertOpenGL;
% Open onscreen window. use the display with the highest number on
% multi-display setups:
screen=max(Screen('Screens'));
% This will open a screen with default settings, aka black
% background, fullscreen, double buffered with 32 bits color depth:
win = Screen('OpenWindow', screen);
% Setup movie size and position
screen_size = Screen('Rect', win);
w = (screen_size(3) - screen_size(1));
cx = .5 * w;
h = (screen_size(4) - screen_size(2));
cy = .5 * h;
mag_ratio = 1.0; % clip magnification ratio
mw = 1920*mag_ratio; mh = 1080*mag_ratio;
x1 = cx - mw/2;
x2 = cx + mw/2;
y1 = cy - mh/2;
y2 = cy + mh/2;
% Main video loop:
for current_trial = 1:trials
% increment counter that keeps track of which movie we are currently playing
num_videos = num_videos + 1;
% Open the moviefile and get details
[movie, tmp, tmp2] = Screen('OpenMovie', win, [movie_dir movienames{current_trial}], 0);
% Start playback of the movie: Play 'movie', at a playbackrate = 1 (normal speed forward), play it once,
% aka with loopflag = 0, % play audio track at volume 1.0 = 100% audio volume.
Screen('PlayMovie', movie, 1, 0, 1);
% to speed things up, open next movie
if current_trial<length(movienames) && (~isempty(strfind(movienames{current_trial+1}, 'mov')) || ~isempty(strfind(movienames{current_trial+1}, 'mp4')))
Screen('OpenMovie', win, [movie_dir movienames{current_trial+1}], 1);
end
% Video playback, one frame at a time
movietexture=0; % Texture handle for the current movie frame.
while(movietexture>=0)
% The 0 - flag means: Don't wait for arrival of new frame,
% just return a zero or -1 'movietexture' if we are at the
% end of the movie
[movietexture, tmp] = Screen('GetMovieImage', win, movie, 0);
% Is it a valid texture?
if (movietexture > 0)
%Draw the texture into backbuffer:
Screen('DrawTexture', win, movietexture, [], [x1, y1, x2, y2]);
% Flip the display to show the image at next retrace:
Screen('Flip', win);
% Delete the texture. We don't need it anymore:
Screen('Close', movietexture);
movietexture=0;
end;
end; % end with movie loop
% Trial done. Next trial...
end
% Done with the experiment. Close onscreen window and finish.
Screen('CloseAll');
return;
catch
% Error handling: Close all windows and movies, release all resources.
ShowCursor;
Screen('CloseAll');
psychrethrow(psychlasterror);
end;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment