How can I play video in MATLAB GUI after browsing the video?
6 views (last 30 days)
Show older comments
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/156592/image.png)
0 Comments
Answers (7)
Walter Roberson
on 25 Sep 2016
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 Comments
Walter Roberson
on 25 Sep 2016
Sorry, it should have been
set(handles.edit1, 'String', filename);
and
filename = get(handles.edit1, 'String');
Image Analyst
on 25 Sep 2016
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 Comment
Walter Roberson
on 25 Sep 2016
implay() opens its own figure to play in; you cannot use implay() to play within a specific area of your GUI.
Pavel Chatterjee
on 15 Apr 2018
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 Comment
Walter Roberson
on 16 Apr 2018
You do not have any graphic object with tag 'edit1'. You should change the edit1 to the tag of your edit box.
Sevthia Nugraha
on 22 Jun 2018
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 Comments
Image Analyst
on 23 Jun 2018
"so what should i do??" My answer is "upgrade to the latest MATLAB version."
Salma Fatia
on 16 Nov 2019
The code is also working with me, thank u so much Walter Roberson
0 Comments
Mazhar ali Khan
on 4 Jan 2020
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 Comment
Walter Roberson
on 4 Jan 2020
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.
See Also
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!