How can I play video in MATLAB GUI after browsing the video?

5 次查看(过去 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:

回答(7 个)

Walter Roberson
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 个评论
Isha Pandya
Isha Pandya 2016-9-25
I have made slight changes in GUI as suggested by you. Instead of static text I am using axes. But I am getting the following errors:
Error using hg.uicontrol/set The name 'D:\Video3.wmv' is not an accessible property for an instance of class 'uicontrol'.
Error in play2>pushbutton1_Callback (line 85) set(handles.edit1, filename);
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in play2 (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)play2('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
I am new to MATLAB and need some explanation regarding these errors. How to solve these errors?
Walter Roberson
Walter Roberson 2016-9-25
Sorry, it should have been
set(handles.edit1, 'String', filename);
and
filename = get(handles.edit1, 'String');

请先登录,再进行评论。


Image Analyst
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
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
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
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
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
Image Analyst 2018-6-23
"so what should i do??" My answer is "upgrade to the latest MATLAB version."

请先登录,再进行评论。


prasetya zulfikar
prasetya zulfikar 2019-4-18
thx walter robenson its work !!!!!!!!!!

Salma Fatia
Salma Fatia 2019-11-16
The code is also working with me, thank u so much Walter Roberson

Mazhar ali Khan
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
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.

请先登录,再进行评论。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by