⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mplay.m

📁 基于MATLAB的媒体播放器
💻 M
📖 第 1 页 / 共 4 页
字号:
    if (ud.currframe==1) || (rem(ud.currframe,10)==0),
        UpdateFrameReadout(ud);
    end
else
    UpdateFrameReadout(ud);
end

% --------------------------------------------------------
function y = GetExportFrame(ud)
switch ud.MovieFormat
    case 'I'
        y = ud.Movie(:,:,ud.currframe);
    case 'RGB'
        y = ud.Movie(:,:,:,ud.currframe);
    case 'S'
        y = ud.Movie(ud.currframe).cdata;
    otherwise
        error('Unrecognized movie format.');
end

% --------------------------------------------------------
function UpdateIntensityImage(ud)
set(ud.himage,'cdata', ud.Movie(:,:,ud.currframe));

function UpdateRGBImage(ud)
set(ud.himage,'cdata', ud.Movie(:,:,:,ud.currframe));

function UpdateStructImage(ud)
set(ud.himage,'cdata', ud.Movie(ud.currframe).cdata);

% --------------------------------------------------------
function SetupUpdateFunction(hfig)
% Register image update function

ud = get(hfig,'userdata');
switch ud.MovieFormat
    case 'I'
        ud.UpdateImageFcn = @UpdateIntensityImage;
    case 'RGB'
        ud.UpdateImageFcn = @UpdateRGBImage;
    case 'S'
        ud.UpdateImageFcn = @UpdateStructImage;
    otherwise
        error('Unrecognized movie format.');
end
set(hfig,'userdata',ud);


% --------------------------------------------------------
function SetupTimer(hfig)

% Setup timer
h = timer( ...
    'ExecutionMode','fixedRate', ...
    'TimerFcn', @TimerTickFcn, ...
    'StopFcn', @TimerStopFcn, ...
    'BusyMode', 'drop', ...
    'TasksToExecute', inf);

% Store fig handle in timer
udtimer.hfig = hfig;
set(h,'userdata',udtimer);

% Store timer handle in figure
ud = get(hfig,'userdata');
ud.htimer = h;
set(hfig,'userdata', ud);

SetFPS(hfig);

% --------------------------------------------------------
function y = ext_getFPS(hfig)
ud = get(hfig,'userdata');
y = ud.fps;

% --------------------------------------------------------
function ext_setFPS(hfig, fps)

ud = get(hfig,'userdata');
ud.fps = fps;
set(hfig,'userdata',ud);
SetFPS(hfig);


% --------------------------------------------------------
function SetFPS(hfig)
% Set frames per second for playback

% Cannot change period while movie is running
% Must stop timer, change frame rate, then restart timer

ud = get(hfig,'userdata');
isRunning = strcmp(get(ud.htimer,'Running'),'on');
% isPaused = ud.paused;
if isRunning, 
    cb_play([],[],ud.hfig);  % pause
end
set(ud.htimer,'Period', 1./ud.fps);
if isRunning, 
    cb_play([],[],ud.hfig);
%     ud = get(hfig,'userdata');
%     ud.paused = isPaused;
%     set(hfig,'userdata',ud);
end
UpdateFPSReadout(hfig);

% -------------------------------------------------------------------------
function UpdateFrameSizeReadout(hfig)
% Update current frame size readout in status bar

ud         = get(hfig,'UserData');
sizeStr    = sprintf('%dx%d', ud.frameRows, ud.frameCols);
readoutStr = sprintf('%s:%s', ud.MovieFormat, sizeStr);

% Build tooltip for frame format/size status bar readout
switch ud.MovieFormat
    case 'I',   s='Intensity';
    case 'S',   s='Structure';
    case 'RGB', s='RGB';
end
CR = sprintf('\n');
tipStr = [s ' format' CR sprintf('%d rows x %d cols', ud.frameRows, ud.frameCols)];

set(ud.hStatusBar.FrameSize,'string',readoutStr,'tooltip',tipStr);

% -------------------------------------------------------------------------
function UpdateFrameReadout(ud)
% Update current frame number readout in status bar
% If fwd/bkwd mode not on,
%    show "current frame : total frames"
% If on,
%    show "+/- current frame : total frames"
%  where + means fwd play, - means bkwd play

str = sprintf('%d:%d',ud.currframe,ud.numFrames);
if ud.fbmode,
    if ud.fbrev,
        dir='-';
    else
        dir='+';
    end
    str = [dir str];
end
set(ud.hStatusBar.FrameNum, 'string', str);

% -------------------------------------------------------------------------
function UpdateFrameReadoutTooltip(ud)

tStr = 'Current Frame : Total Frames';
if ud.fbmode,
    tStr = [tStr sprintf('\n') '+: Fwd dir, -:Bkwd dir'];
end
set(ud.hStatusBar.FrameNum, 'tooltip', tStr);

% -------------------------------------------------------------------------
function UpdateFPSReadout(hfig)
% Update frames per second readout in status bar

ud = get(hfig,'UserData');
str = sprintf('FPS: %d',ud.fps);
set(ud.hStatusBar.FPS,'string',str);

% --------------------------------------------------------
function EditProperties(hfig)
% Get frames per second in dialog box

ud = get(hfig,'userdata');

while 1,  % infinite loop in case user enters invalid information
    % Get dialog
    prompt={'Desired playback rate (fps):', ...
            'Colormap expression:'};
    def={num2str(ud.fps), ud.cmapexpr};
    dlgTitle='Movie Player Properties';
    lineNo=1;
    AddOpts.Resize='off';
    AddOpts.WindowStyle='modal';
    AddOpts.Interpreter='none';
    answer=inputdlg(prompt,dlgTitle,lineNo,def,AddOpts);
    
    if ~isempty(answer),  % cancel pressed?
        % Error check
        
        % Frame rate:
        %
        new_fps = str2num(answer{1});
        if new_fps > 0,
            if ~isequal(new_fps, ud.fps),  % change made?
                % Grab userdata now in case changes were made
                %   while dialog was open
                ext_setFPS(hfig,new_fps);
            end
        else
            hwarn = warndlg({'Invalid frames/second entered.','Must be > 0.'}, 'Movie Player Error','modal');
            waitfor(hwarn);
            continue;
        end
        
        % Colormap:
        %
        new_cmapexpr = deblank(answer{2});
        new_cmap = eval(new_cmapexpr, '[]');
        if isempty(new_cmap),
            hwarn = warndlg('Invalid colormap expression.','Movie Player Error','modal');
            waitfor(hwarn);
            continue;
        else
            if ~isequal(new_cmapexpr, ud.cmapexpr),  % change made?
                % Grab userdata now in case changes were made
                %   while dialog was open
                ud = get(hfig,'userdata');
                ud.cmapexpr = new_cmapexpr;
                ud.cmap     = new_cmap;
                set(hfig, 'userdata',ud, 'colormap',new_cmap);
            end
        end
    end
    break  % stop infinite loop
end

% --------------------------------------------------------
function EditFrameNum(hfig)
% Get manually specified frame number in dialog box
% "Jump to" operation

% Initialize to last "jump to" value, NOT to the current frame
% This way, the edit operation becomes useful for continually
% jumping to a particular frame

ud = get(hfig,'userdata');

while 1,  % infinite loop in case user enters invalid information
    % Show dialog
    prompt   = {'Jump to frame:'};
    def      = {num2str(ud.jumpto_frame)};
    dlgTitle = 'Movie Player Properties';
    lineNo   = 1;
    AddOpts.Resize      = 'off';
    AddOpts.WindowStyle = 'normal';
    AddOpts.Interpreter = 'none';
    answer = inputdlg(prompt,dlgTitle,lineNo,def,AddOpts);
    
    if ~isempty(answer),  % cancel pressed?
        % Error check
        
        % Currently Displayed Frame:
        %
        new_jumpto = str2num(answer{1});
        if (new_jumpto > 0) && (new_jumpto <= ud.numFrames),
            % Note: always perform update, even if value remains the same
            % That's because the currently displayed frame may have
            % changed behind our backs.  Even if we re-grab userdata,
            % it's changing all the time.
                
            % Get properties again, in case something has
            % changed behind our backs while dialog was open
            ud = get(hfig,'userdata');
            ud.currframe = new_jumpto;
            ud.nextframe = new_jumpto;
            ud.jumpto_frame = new_jumpto;
            
            % jump puts player in pause mode if currently stopped
            isRunning = strcmp(get(ud.htimer,'Running'),'on');
            isStopped = ~isRunning && ~ud.paused;
            ud.paused = 1;
            
            set(hfig,'userdata',ud);
            ShowMovieFrame(hfig);
            if isStopped, UpdateGUIControls(hfig); end
        else
            hwarn = warndlg({'Invalid frame number.'}, 'Movie Player Error','modal');
            waitfor(hwarn);
            continue
        end
    end
    break  % stop infinite loop
end

% --------------------------------------------------------
function cb_properties(hco, eventStruct)
EditProperties(gcbf);

function cb_jumpto(hco, eventStruct)
EditFrameNum(gcbf);

% --------------------------------------------------------
function cb_export_imview(hco, eventStruct)
% Export current image frame to IMVIEW or base workspace.

hfig = gcbf;
ud = get(hfig,'userdata');
v = GetExportFrame(ud);
if exist('imview','file'),
    try
        imview(v);
    catch
        warndlg(sprintf('Failed when calling IMVIEW:\n\n%s', lasterr), ...
            'MPLAY Export Error', 'modal');
    end
else
    % Export to workspace
    varname = 'mplay_export';
    fprintf(['Exported current image frame to base workspace\n' ...
            'Variable name: %s\n\n'], varname);
    assignin('base',varname,v);
end

% --------------------------------------------------------
function TimerTickFcn(hco, user)

ud = get(hco,'userdata');     % hco = timer object
hfig = ud.hfig;
ud = get(hfig,'userdata');
ud.currframe = ud.nextframe;
ShowMovieFrame(hfig, 1);  % "fast" display

% Increment frame or stop playback if finished
if (ud.currframe == ud.numFrames),
    % Last frame of movie just displayed
    
    if ud.fbmode % && ~ud.fbrev,  % unnecessary qualifier --- always will be false here
        ud.fbrev = 1;  % going into reverse playback mode
        ud.nextframe = ud.currframe-1;
        shouldStop = 0;
        
%     elseif ud.fbmode && ud.fbrev,
%         error('how did this happen? ==end');
%         shouldStop = 1;
        
    else
        ud.nextframe = 1;  % loop back to beginning
        shouldStop = 1;
    end
    
    % If we hit this point, the timer ran us to the end of
    % the movie.  In particular, the user did not hit stop.
    % Now, if pause is on, it's due to manual interaction with
    % the fwd/back buttons, and not due to actual pausing,
    % obvious since we're still running and is why we're here.
    % Turn off pause:
    ud.paused = 0;
    
elseif (ud.currframe == 1),
    % First frame of movie just displayed
    if ud.fbmode && ud.fbrev,
        ud.fbrev = 0;  % going into fwd playback mode
        ud.nextframe = ud.currframe+1;
        shouldStop = 1;
    else
        ud.nextframe = 2;  % ud.currframe+1
        shouldStop = 0;
    end
    
else
    if ud.fbmode && ud.fbrev,
        ud.nextframe = ud.nextframe - 1;  % next frame, reverse play
    else
        ud.nextframe = ud.nextframe + 1;  % next frame, fwd play
    end
    shouldStop = 0;
end
set(hfig,'userdata',ud);

if shouldStop && ~ud.loopmode, % no looping - stop now
    stop(ud.htimer); % stop playback
end

% --------------------------------------------------------
function TimerStopFcn(hco, user)
% Keep this here, not in cb_stop
% Could have stopped from stop button (eg, gone thru cb_stop)
% but also could have stopped here due to end of movie

ud = get(hco,'userdata');
do_timer_stop(ud.hfig);

% --------------------------------------------------------
function do_timer_stop(hfig)

ud = get(hfig,'userdata');
% ShowMovieFrame(hfig);
UpdateFrameReadout(ud);

% Set play icon, brighter
icons = get_icons_from_fig(hfig);
hPlay = findobj(ud.htoolbar, 'tag','Play/Pause');
set(hPlay, ...
    'tooltip', 'Play', ...
    'cdata', icons.play_on);
UpdateGUIControls(hfig);

% --------------------------------------------------------
function z = mergefields(varargin)
%MERGEFIELDS Merge fields into one structure
%   Z = MERGEFIELDS(A,B,C,...) merges all fields of input structures
%   into one structure Z.  If common field names exist across input
%   structures, values from later input arguments prevail.
%
%   Example:
%     x.one=1;  x.two=2;    % Define structures
%     y.two=-2; y.three=3;  % containing a common field (.two)
%     z=mergefields(x,y)  % => .one=1, .two=-2, .three=3
%     z=mergefields(y,x)  % => .one=1, .two=2,  .three=3
%
%   See also SETFIELD, GETFIELD, RMFIELD, ISFIELD, FIELDNAMES.

% Copyright 1984-2003 The MathWorks, Inc.
% $Revision: $ $Date: $

z=varargin{1};
for i=2:nargin,
    f=fieldnames(varargin{i});
    for j=1:length(f),
        z.(f{j}) = varargin{i}.(f{j});
    end
end

% [EOF] mplay.m

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -