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

2 Ansichten (letzte 30 Tage)
Isha Pandya
Isha Pandya am 25 Sep. 2016
Kommentiert: Walter Roberson am 4 Jan. 2020
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:

Antworten (7)

Walter Roberson
Walter Roberson am 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 Kommentare
Isha Pandya
Isha Pandya am 25 Sep. 2016
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 am 25 Sep. 2016
Sorry, it should have been
set(handles.edit1, 'String', filename);
and
filename = get(handles.edit1, 'String');

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 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 Kommentar
Walter Roberson
Walter Roberson am 25 Sep. 2016
implay() opens its own figure to play in; you cannot use implay() to play within a specific area of your GUI.

Melden Sie sich an, um zu kommentieren.


Pavel Chatterjee
Pavel Chatterjee am 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 Kommentar
Walter Roberson
Walter Roberson am 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.

Melden Sie sich an, um zu kommentieren.


Sevthia Nugraha
Sevthia Nugraha am 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 Kommentare
Image Analyst
Image Analyst am 23 Jun. 2018
"so what should i do??" My answer is "upgrade to the latest MATLAB version."
Sevthia Nugraha
Sevthia Nugraha am 15 Jul. 2018
that's helping me a lot, thank you

Melden Sie sich an, um zu kommentieren.


prasetya zulfikar
prasetya zulfikar am 18 Apr. 2019
thx walter robenson its work !!!!!!!!!!

Salma Fatia
Salma Fatia am 16 Nov. 2019
The code is also working with me, thank u so much Walter Roberson

Mazhar ali Khan
Mazhar ali Khan am 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 Kommentar
Walter Roberson
Walter Roberson am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by