How can I play video in MATLAB GUI after browsing the video?
2 次查看(过去 30 天)
显示 更早的评论
I have created the following GUI in MATLAB. I want to browse the video and display the path of video in the edit textbox besides the browse button. And after clicking the "Play" button, it should play the browsed video in the static text box. Please help me with the code. Following is the GUI:
0 个评论
回答(7 个)
Walter Roberson
2016-9-25
function browse_Callback(hObject, event, handles)
[chosenfile, chosenpath] = uigetfile('*.avi', 'Select a video');
if ~ischar(chosenfile)
return; %user canceled dialog
end
filename = fullfile(chosenpath, chosenfile);
set(handles.edit1, filename);
function play_Callback(hobject, event, handles)
filename = get(handles.edit1, filename);
if ~exist(filename, 'file')
warndlg( 'Text in edit box is not the name of a file');
return
end
try
obj = VideoReader(filename);
catch
warndlg( 'File named in edit box does not appear to be a usable movie file');
return
end
ax = handles.ax1;
while hasFrame(obj)
vidFrame = readFrame(obj);
image(vidFrame, 'Parent', ax);
set(ax, 'Visible', 'off');
pause(1/obj.FrameRate);
end
clear obj
Notice, though, that this code displays in an axes, not in a static text. A static text box cannot hold graphics. If there was some deep reason why you needed the video displayed on a uicontrol then you would use a uicontrol('style', 'push', 'enable', 'off') and update its Cdata property with the contents of the frame -- but that would seldom be better than displaying it in an axes.
I would also suggest to you that it would be better if considered making that edit box into a static text box. When you have it as an edit box then the user might edit the string, which is why it is necessary to validate that the string still designates a file. If you make it a static text box then the only way for the user to put something in there is by the Browse button, so you could be sure that it was a file named.
You should be careful, though: when you allow the user to browse to arbitrary locations, the complete pathname to the file (which is what you write in the edit box) might not fit within the space you allocate. Edit boxes do at least allow the user to position within the text if they want to see it all, but the default is at the beginning, which is the directory part rather than the file name part. Static text boxes attempt to "wrap" the text to multiple lines if the box is not wide enough for all if it; that is not going to be very successful if the name is too long for the area you give.
2 个评论
Walter Roberson
2016-9-25
Sorry, it should have been
set(handles.edit1, 'String', filename);
and
filename = get(handles.edit1, 'String');
Image Analyst
2016-9-25
Description
implay opens the Video Viewer app. You can use Video Viewer to show MATLAB® movies, videos, or image sequences (also called image stacks). To select the movie or image sequence that you want to play,................
1 个评论
Walter Roberson
2016-9-25
implay() opens its own figure to play in; you cannot use implay() to play within a specific area of your GUI.
Pavel Chatterjee
2018-4-15
I am getting the following error
Reference to non-existent field 'edit1'. Error in untitled10>pushbutton1_Callback (line 85) filename = get(handles.edit1, 'String'); Error in gui_mainfcn (line 95) feval(varargin{:}); Error in untitled10 (line 42) gui_mainfcn(gui_State, varargin{:}); Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)untitled10('pushbutton1_Callback',hObject,eventdata,guidata(hObject)) Error while evaluating UIControl Callback.
1 个评论
Walter Roberson
2018-4-16
You do not have any graphic object with tag 'edit1'. You should change the edit1 to the tag of your edit box.
Sevthia Nugraha
2018-6-22
Undefined function 'hasFrame' for input arguments of type 'VideoReader'.
Error in SIMULASI_CROSSTALK>pushbutton7_Callback (line 983) while hasFrame(obj)
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in SIMULASI_CROSSTALK (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)SIMULASI_CROSSTALK('pushbutton7_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
i'm still getting these error, so what should i do??
3 个评论
Image Analyst
2018-6-23
"so what should i do??" My answer is "upgrade to the latest MATLAB version."
Mazhar ali Khan
2020-1-4
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)GUI('browse_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
i m facing this error
1 个评论
Walter Roberson
2020-1-4
At the command line give the command
dbstop if error
And then run the code. When it stops on the error, give the command
dbstack
And show us the complete results. Also show us size() and class() of every variable mentioned on the line that it stopped on. And tell us which MATLAB version you are using.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!