【游戏】基于matlab GUI音乐闹钟设计【含Matlab源码 1105期】
## 一、简介
基于matlab GUI音乐闹钟设计
## 二、源代码
```c
function varargout = wying(varargin)
% WYING M-file for wying.fig
% WYING, by itself, creates a new WYING or raises the existing
% singleton*.
%
% H = WYING returns the handle to a new WYING or the handle to
% the existing singleton*.
%
% WYING('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in WYING.M with the given input arguments.
%
% WYING('Property','Value',...) creates a new WYING or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before wying_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to wying_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help wying
% Last Modified by GUIDE v2.5 02-Jun-2021 10:01:54
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @wying_OpeningFcn, ...
'gui_OutputFcn', @wying_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
% End initialization code - DO NOT EDIT
% --- Executes just before wying is made visible.
function wying_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to wying (see VARARGIN)
%设置定时器
if ~isempty(timerfindall), stop(timerfindall);delete(timerfindall);end
%在这里设置radiobutton,是创建时设置的属性不起作用?
%set(handles.radiobutton_music,'value',handles.clockSaveData(3));
set(handles.radiobutton_music,'Value',handles.clockSaveData(3));
%设置图标*******************************************************************
filename='d:\我的文档\My Pictures\picture\pink素材.jpg';
javaFrame=get(hObject,'javaFrame');
set(javaFrame,'FigureIcon',javax.swing.ImageIcon(filename));
%**************************************************************************
%设置com对象媒体播放器
temp=get(handles.uipanel_outerFrame,'Units');set(handles.uipanel_outerFrame,'Units','pixels');
position=get(handles.uipanel_outerFrame,'position');%【左下X,左下Y,宽,高】
position(4)=position(2)-20;position(2)=8;position(3)=position(3)+position(1)-13;
handles.sound_player=actxcontrol('wmplayer.ocx.7',position,handles.figure_bkground);
handles.sound_player.settings.volume=100;%音量【0,,100】
set(handles.uipanel_outerFrame,'Units',temp);%uipanellll_outerFrame还原回原来的单位
handles.timer=timer;%这句要放set之前,否则handles里无timer域
set(handles.timer,'TimerFcn',{@timer_action,handles});%计时器
start_timer(handles);%启动定时器
start_timer2(handles);
%*************************************************************************
%set(handles.pushbutton_browse,'visible','off');%默认为隐藏浏览框
%set(handles.edit_musicFile,'visible','off');
%*************************************************************************
% Choose default command line output for wying
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes wying wait for user response (see UIRESUME)
% uiwait(handles.figure_bkground);
% --- Outputs from this function are returned to the command line.
function varargout = wying_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton_browse.
function pushbutton_browse_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_browse (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%set(handles.activex1,'URL','d:\我的文档\我的音乐\music\阿桑\阿桑.mp3');
[filename,pathname]=uigetfile(...%调用windows的打开文件窗口
{'*.mp3;*.wav;*.asf;*.wma;*.wmv;*.rm;*.avi;...*.mpg;*.mp4;*.rmvb;*.mkv',...
'可以播放的文件';},'选定是播放的音乐','MultiSelect','off');
%形成完整的“路径名+文件名”字符串
handles.musicSaveData=fullfile(pathname,filename);
%将上部的字符串写入edit_musicFile空间的string域内
set(handles.edit_musicFile,'String',handles.musicSaveData);
musicSaveData=handles.musicSaveData;%供下部save使用musicSaveData变量
%每次都sava是为了提供记录功能,是每次打开闹钟者都有上次记录的文件名
save('musicSaveData.txt','-ascii','musicSaveData');%写入一个txt文件
guidata(hObject,handles);%因为增加了handles的域,所以要更新数据供其他函数用
% --- Executes on selection change in popupmenu_hour.
function popupmenu_hour_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu_hour (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_hour contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu_hour
%提取当前的小时数的位置编号(不是具体小时数)
handles.clockSaveData(1)=get(hObject,'Value');
clockSaveData=handles.clockSaveData;
save('clockSaveData.txt','-ascii','clockSaveData');
guidata(hObject,handles);%更新数据
%下面启动定时器
start_timer(handles);
% --- Executes during object creation, after setting all properties.
function popupmenu_hour_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu_hour (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
%载入以前的定时
set(hObject,'Value',handles.clockSaveData(1));
%控件对象的句柄写入handles中
handles.popupmenu_hour=hObject;
guidata(hObject,handles);
% --- Executes on selection change in popupmenu_minute.
function popupmenu_minute_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu_minute (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_minute contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu_minute
handles.clockSaveData(2)=get(hObject,'Value');
clockSaveData=handles.clockSaveData;
save('clockSaveData.txt','-ascii','clockSaveData');
guidata(hObject,handles);%更新数据
%下面启动定时器
start_timer(handles);
% --- Executes during object creation, after setting all properties.
function popupmenu_minute_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu_minute (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
%载入以前的定时
set(hObject,'Value',handles.clockSaveData(2));
%控件对象的句柄写入handles中
handles.popupmenu_minute=hObject;
guidata(hObject,handles);
function edit_musicFile_Callback(hObject, eventdata, handles)
% hObject handle to edit_musicFile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_musicFile as text
% str2double(get(hObject,'String')) returns contents of edit_musicFile as a double
handles.musicSaveData=get(hObject,'String');%从编辑框控件中获得文件名(string)
musicSaveData=handles.musicSaveData;%文件名临时存到musicSaveData*记录
%将文件名musicSaveData写入musicSaveData.txt文本文件中,保存到硬盘上
save('musicSaveData.txt','-ascii','musicSaveData');
guidata(hObject,handles);%更新handles数据
```
## 三、运行结果
![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210706183747657.JPG?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RJUUNtYXRsYWI=,size_16,color_FFFFFF,t_70#pic_center)
## 四、备注
版本:2014a