1. 读取
首先需要使用到Screen(‘OpenMovie’)函数,该函数可以读取mp4、gif格式的数据,具体方式如下:
clear;
clc;
Screen('Preference', 'SkipSyncTests', 1);
screens = Screen('Screens');
screenNum = max(screens);
[window, screenRect] = Screen('OpenWindow',screenNum);
%HideCursor;
%设置图片
[leftPtr, dl, fpsl, wl, hl, zhenshul]=Screen('OpenMovie', window, 'F:\Matlab\混淆刺激范式\left.mp4');
其中leftPtr为函数读取视频的指针,dl表示视频时长,fps1表示视频频率,wl表示视频宽度,hl表示视频高度,zhenshul表示帧数。
2. 设置开始时间
需要使用到Screen(‘SetMovieTimeIndex’)函数:
Screen('SetMovieTimeIndex', leftPtr, 0)
这里leftPtr表示视频读取后的指针,0表示设置视频从0秒开始播放。
3.设置视频播放区域和位置,循环次数
首先读取屏幕大小即屏幕长宽高
[screenXpixels, screenYpixels] = Screen('WindowSize', window);
screenXpixels表示屏幕宽度,screenYpixels表示屏幕高度
接着设置绘制区域,使用CenterRectOnPoint函数:
leftRect = CenterRectOnPoint([0, 0, wl, hl], screenXpixels / 4, screenYpixels / 2);
第一个[]表示绘制图的长宽以及从相对当前位置的哪里开始绘制,00表示相对当前位置的起始点开始绘制,screenXpixels / 4, screenYpixels / 2表示CenterRectOnPoint相对屏幕的位置,即从屏幕宽1/4、高1/2处绘制。大致是这个意思,具体如下图:
循环次数设置代码如下:
Screen('PlayMovie', leftPtr, 1, 1, 1);
4. 设置循环播放,代码如下:
while truelefttex = Screen('GetMovieImage', window, leftPtr, [], [], 2);if lefttex > 0Screen('DrawTexture', window, lefttex, [], leftRect);Screen('Close', lefttex);endScreen('Flip', window);% 检查键盘输入以退出循环[keyIsDown, ~, keyCode] = KbCheck;if keyIsDownif keyCode(KbName('Q'))break;endendend
Screen('PlayMovie', leftPtr, 0);
Screen('CloseMovie', leftPtr);
Screen('CloseAll');
5.整体代码如下,代码给出三个视频left、mid、right运行:
clear;
clc;
Screen('Preference', 'SkipSyncTests', 1);
screens = Screen('Screens');
screenNum = max(screens);
[window, screenRect] = Screen('OpenWindow',screenNum);
%HideCursor;
%设置图片[leftPtr, dl, fpsl, wl, hl, zhenshul]=Screen('OpenMovie', window, 'F:\Matlab\混淆刺激范式\left.mp4');
[midPtr, dm, fpsm, wm, hm, zhenshum]=Screen('OpenMovie', window, 'F:\Matlab\混淆刺激范式\mid.mp4');
[rightPtr, dr, fpsr, wr, hr, zhenshur]=Screen('OpenMovie', window, 'F:\Matlab\混淆刺激范式\right.mp4');
Screen('SetMovieTimeIndex', leftPtr, 0)
Screen('SetMovieTimeIndex', midPtr, 0)
Screen('SetMovieTimeIndex', rightPtr, 0)
% 设置视频播放区域
[screenXpixels, screenYpixels] = Screen('WindowSize', window);leftRect = CenterRectOnPoint([0, 0, wl, hl], screenXpixels / 4, screenYpixels / 2);
midRect = CenterRectOnPoint([0, 0, wm, hm], screenXpixels / 2, screenYpixels / 2); % 中间视频区域
rightRect = CenterRectOnPoint([0, 0, wr, hr], 3*screenXpixels / 4, screenYpixels / 2); % 右侧视频区域Screen('PlayMovie', leftPtr, 1, 1, 1);
Screen('PlayMovie', midPtr, 1, 1, 1);
Screen('PlayMovie', rightPtr, 1, 1, 1);while truelefttex = Screen('GetMovieImage', window, leftPtr, [], [], 2);midTex = Screen('GetMovieImage', window, midPtr, [], [], 2);rightTex = Screen('GetMovieImage', window, rightPtr, [], [], 2);if lefttex > 0Screen('DrawTexture', window, lefttex, [], leftRect);Screen('Close', lefttex);endif midTex > 0Screen('DrawTexture', window, midTex, [], midRect);Screen('Close', midTex);endif rightTex > 0Screen('DrawTexture', window, rightTex, [], rightRect);Screen('Close', rightTex);endScreen('Flip', window);% 检查键盘输入以退出循环[keyIsDown, ~, keyCode] = KbCheck;if keyIsDownif keyCode(KbName('Q'))break;endendend
Screen('PlayMovie', leftPtr, 0);
Screen('PlayMovie', midPtr, 0);
Screen('PlayMovie', rightPtr, 0);
Screen('CloseMovie', leftPtr);
Screen('CloseMovie', midPtr);
Screen('CloseMovie', rightPtr);
Screen('CloseAll');
绘制结果: