我是Matlab的新手,我正在尝试制作一个有3个按钮的程序。
问题1:
按钮1将链接到音频文件(.wav) 因此,当我按下它时,按钮1将选择"第一个音频文件。
按钮2将链接到另一个音频文件(.wav) 因此,当我按下它时,按钮2将选择"第二个音频文件。
然后,我需要按下按钮3播放所选的声音文件,根据我先按下的按钮(按钮1 /按钮2)
所以我现在的代码是这样的:
按钮1和2:
[FileName,PathName] = uigetfile({'*.wav'},'Load Wav File');
[x,Fs] = wavread([PathName '/' FileName]);
按钮3:
player = audioplayer(x,Fs);
play(player);
好的,所以我的第一个问题是,当我运行此代码时,我必须手动选择该文件。我不太确定如何使它已经链接到文件,所以我需要帮助...
我的第二个问题实际上是关于音频播放器......
对于按钮3,它实际上不起作用......并且没有播放声音。
我之前试过sound (x,Fs)
但我在网上搜索他们说我必须手动给命令清除声音以阻止.wav文件继续。
如何解决这个问题?因为我认为如果我使用带代码的第四个按钮:
stop(player)
它给了我一个错误。
答案 0 :(得分:2)
- 首先你必须知道你在Callback函数中定义的变量是不存储的,这就是你的Button 3无效的原因。
- 您是否尝试直接运行音频文件以在编程GUI之前检查它是否有效?
- 您要做的事情的几个提示:
1)如果您不想手动选择文件,只需设置:
filename ='.wav文件的完整路径';
2)如果你有FileName和PathName,要构造包含完整路径的字符串,你可以这样做:
FullPath=strcat(PathName,FileName);
3)按钮1和按钮2是什么类型的按钮?如果只是选择一个文件,为什么还需要按钮?我建议:
4)关于你的上一个问题,这又是因为变量不是在回调之间传递的,所以在你的Button4_Callback中,Matlab不知道变量'player'。
当你回答我的问题时,我会发布更多细节^^
编辑 - 关于您的评论:
a)你不应该使用`函数uipanel1_SelectionChangeFcn(hObject,eventdata,handles)'来调用你的代码,因为你可以点击Button3来做所有事情。 (功能Button3_Callback(hObject,eventdata,handle ))
b)我建议您为GUI元素提供自定义名称(在属性编辑器中更改Tag属性),例如Button3将是“play”或类似的东西。 (想象一下GUI有35个按钮,25个编辑字段等等,你会很快迷路)
c)Button3_Callback看起来像:
%Know which radiobutton is selected
Value_radiobutton1=get(handles.radiobutton1,'value'); %Returns 1 if radiobutton1 is checked, 0 if not
Value_radiobutton2=get(handles.radiobutton2,'value'); %Same as above but for radiobutton2
%Now that you have the information :
if(Value_radiobutton1)
% play first .wav
FullPath='C:\Users\Y400\Documents\MATLAB\test1';
[x,Fs]=wavread(FullPath);
handles.myPlayer = audioplayer(x,Fs);
play(handles.myPlayer);
else
% play second.wav
end
%store your handles again to keep track of your audioplayer.
guiData(hObject,handles);
请注意,只要回调停止执行,audiofile就会停止播放,所以你必须在那里使用一点技巧,例如:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/328757
另请注意,执行回调后,您的播放器变量将被删除,因此您将无法再访问它。我建议你在 handle 对象中存储一个句柄。这可以通过例如在guidata(hObject,handles)
之前的GUI的打开功能中添加类似的内容来完成:
handles.myPlayer=[];
然后在Button3_Callback函数中重写它
而不是写作:player=audioplayer[x,Fs]
您选择:handles.myPlayer=audioplayer[x,Fs]
然后,您可以访问它以暂停/停止音频。
现在你仍然需要:
答案 1 :(得分:1)
My first answer is starting to get a bit messy, so i'll post here a fully working simple example. In this GUI, there are 3 pushButtons (Play, Pause and Stop), and one .wav file played (piano2.wav).
function varargout = AudioTest(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @AudioTest_OpeningFcn, ...
'gui_OutputFcn', @AudioTest_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% Opening function %%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function AudioTest_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.myPlayer=[];
% Update handles structure
guidata(hObject, handles);
function varargout = AudioTest_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Executed on press of Play button %%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Play_Callback(hObject, eventdata, handles)
[x,Fs]=wavread('piano2.wav');
handles.myPlayer=audioplayer(x,Fs);
play(handles.myPlayer);
guidata(hObject,handles);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Executed on press of Pause button %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Pause_Callback(hObject, eventdata, handles)
pause(handles.myPlayer);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Executed on press of Stop button %%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Stop_Callback(hObject, eventdata, handles)
stop(handles.myPlayer);