1
function PlayMoviesWithoutGapDemoOSX(moviename)
3
% PlayMoviesWithoutGapDemoOSX(moviename)
5
% This demo accepts a pattern for a valid moviename, e.g.,
6
% moviename='*.mpg', then it plays all movies in the current working
7
% directory whose names match the provided pattern, e.g., the '*.mpg'
8
% pattern would play all MPEG files in the current directory.
10
% Pressing ESC ends the demo.
12
% Movies are played one after each other. We try to minimize perceptible
13
% gaps between end of movie i and start of movie i+1 by asynchronous
14
% loading: While movie i is played back, we ask Psychtoolbox to load the
15
% next movie i+1 in the background, so startup time for movie i+1 will be
16
% minimized. This feature is currently only supported on MacOS-X and it
17
% doesn't work well with movies that have sound tracks. The reason for this
18
% is currently unknown, but it leads to frequent lockups of the playback
19
% engine. How well this feature works depends strongly on the computational
20
% horsepower of your machine. Slow machines will probably stutter...
29
moviename = '*.mov' %#ok<NOPRT>
32
% Switch KbName into unified mode: It will use the names of the OS-X
33
% platform on all platforms in order to make this script portable:
34
KbName('UnifyKeyNames');
41
background=[128, 128, 128];
43
% Open onscreen window:
44
screen=max(Screen('Screens'));
45
win = Screen('OpenWindow', screen, background);
47
% Initial display and sync to timestamp:
51
% Return full list of movie files from directory+pattern:
52
moviefiles=dir(moviename);
53
for i=1:size(moviefiles,1)
54
moviefiles(i).name = [ pwd filesep moviefiles(i).name ];
57
% Playbackrate defaults to 1:
60
% Load first movie. This is a synchronous (blocking) load:
62
moviename=moviefiles(mod(iteration, size(moviefiles,1))+1).name;
63
[movie movieduration fps imgw imgh] = Screen('OpenMovie', win, moviename);
65
% Start playback of movie. This will start
66
% the realtime playback clock and playback of audio tracks, if any.
67
% Play 'movie', at a playbackrate = 1, with 1.0 == 100% audio volume.
68
Screen('PlayMovie', movie, rate, 0, 1.0);
73
% Endless loop, runs until ESC key pressed:
75
% Show basic info about movie:
76
fprintf('ITER=%i::', iteration);
77
fprintf('Movie: %s : %f seconds duration, %f fps, w x h = %i x %i...\n', moviename, movieduration, fps, imgw, imgh);
81
% Get moviename of next file:
82
iteration=iteration + 1;
83
moviename=moviefiles(mod(iteration, size(moviefiles,1))+1).name;
87
% Playback loop: Fetch video frames and display them...
91
% Return next frame in movie, in sync with current playback
93
% tex either the texture handle or zero if no new frame is
94
% ready yet. pts = Presentation timestamp in seconds.
95
[tex pts] = Screen('GetMovieImage', win, movie, 1);
97
% Valid texture returned?
99
% No. This means that the end of this movie is reached.
100
% We exit the loop and prepare next movie:
105
% Yes. Draw the new texture immediately to screen:
106
Screen('DrawTexture', win, tex);
112
Screen('Close', tex);
116
% Check for abortion by user:
118
[keyIsDown,secs,keyCode]=KbCheck;
119
if (keyIsDown==1 && keyCode(esc))
120
% Set the abort-demo flag.
125
% We start background loading of the next movie 0.5 seconds
126
% after start of playback of the current movie:
127
if prefetched==0 && pts > 0.5
128
% Initiate background async load operation:
129
% We simply set the async flag to 1 and don't query any
131
Screen('OpenMovie', win, moviename, 1);
135
% If asynchronous load of next movie has been started already
136
% and we are less than 0.5 seconds from the end of the current
137
% movie, then we try to finish the async load operation and
138
% start playback of the new movie in order to give processing a
140
if prefetched==1 && movieduration - pts < 0.5
141
% Less than 0.5 seconds until end of current movie. Try to
142
% start playback for next movie:
144
% Open the movie - this should take zero-time as the movie
145
% should have been already prefetched during playback of
146
% the current movie. In case loading did not finish, this
147
% command will pause Matlabs execution until the movie is
149
[newmovie newmovieduration newfps newimgw newimgh] = Screen('OpenMovie', win, moviename);
152
Screen('PlayMovie', newmovie, rate, 0, 1.0);
158
telapsed = GetSecs - t1 %#ok<NOPRT,NASGU>
159
finalcount=i %#ok<NOPRT,NASGU>
161
% Done with old movie. Stop its playback:
162
Screen('PlayMovie', movie, 0);
164
% Close movie object:
165
Screen('CloseMovie', movie);
167
% Ok, now our 'newmovie' becomes the current 'movie':
169
movieduration = newmovieduration;
174
% As playback of the new movie has been started already, we can
175
% simply reenter the playback loop:
178
% End of movie playback. Shut down and exit:
180
% A prefetch operation for a movie is still in progress. We need to
181
% finalize this cleanly by waiting for the movie to open and then
183
newmovie = Screen('OpenMovie', win, moviename);
188
% Playback of a new prefetched movie has been started. We need to stop and
190
Screen('PlayMovie', newmovie, 0);
191
Screen('CloseMovie', newmovie);
195
Screen('Close', win);
200
% Error handling: Close all windows and movies, release all ressources.