function varargout = music(varargin)
% MUSIC MATLAB code for music.fig
% MUSIC, by itself, creates a new MUSIC or raises the existing
% singleton*.
%
% H = MUSIC returns the handle to a new MUSIC or the handle to
% the existing singleton*.
%
% MUSIC('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MUSIC.M with the given input arguments.
%
% MUSIC('Property','Value',...) creates a new MUSIC or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before music_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to music_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 music
% Last Modified by GUIDE v2.5 04-May-2017 11:14:34
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @music_OpeningFcn, ...
'gui_OutputFcn', @music_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 music is made visible.
function music_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 music (see VARARGIN)
% Choose default command line output for music
handles.output = hObject;
global signal_stop signal_start signal_skip
signal_stop=0;
signal_start=0;
signal_skip=0;
fid=fopen('data.txt','r');
data=importdata('data.xls');
set(handles.listbox1,'String',data);
guidata(hObject, handles);
% UIWAIT makes music wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = music_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 selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (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 listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox 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
function listbox1_DeleteFcn(hObject, eventdata, handles)
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%上一首
global audio
stop(audio);
num=get(handles.listbox1,'value');
mode=get(handles.popupmenu1,'value');
max_num=length(get(handles.listbox1,'String'));
switch mode
case 1
if num==max_num
set(handles.listbox1,'value',1);
else
set(handles.listbox1,'value',num+1);
end
case 2
for i=1:5 %五次以内一定可以找到不一样的序号
rand_num=randi(max_num);
if rand_num~=num
set(handles.listbox1,'value',rand_num);
end
end
case 3
set(handles.listbox1,'value',num);
end
pushbutton3_Callback(hObject, eventdata, handles);
handles=guidata(hObject);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global audio
stop(audio)
num=get(handles.listbox1,'value');
mode=get(handles.popupmenu1,'value'); %检查当前的播放模式
max_num=length(get(handles.listbox1,'String'));%最多多少首歌
switch mode
case 1
if num==max_num
set(handles.listbox1,'value',1);
else
set(handles.listbox1,'value',num-1);
end
case 2
for i=1:5
rand_num=randi(max_num);
if rand_num~=num
set(handles.listbox1,'value',rand_num);
end
end
case 3
set(handles.listbox1,'value',num);
end
pushbutton3_Callback(hObject, eventdata, handles);
handles=guidata(hObject);
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global audio signal_start signal_stop Y FS signal_skip
musics=get(handles.listbox1,'String');
num=get(handles.listbox1,'value');
music=musics(num);
str='.mp3';
file=strcat(music,str);
file=mat2str(cell2mat(file));
file(1)=[];
file(end)=[];
[Y,FS]=audioread(file);
audio=audioplayer(Y,FS);
minutes=floor(get(audio,'TotalSamples')/FS/60); %因为Y是采集点,Fs为每秒采集多少个点
seconds=floor(get(audio,'TotalSamples')/FS-minutes*60);
% disp(['第' num2str(num) '首歌:',file '时长' num2str(minutes) '分' num2str(seconds) '秒' ])
if (signal_start~=0) && (signal_stop == 1) %如果只是因为暂停,就从该处开始
play(audio,[signal_start,get(audio,'TotalSamples')]);
disp('重新播放')
signal_start=0;
signal_stop=0;
else if (signal_start~=0) && (signal_skip == 1)
play(audio,[signal_start,get(audio,'TotalSamples')]);
disp('跳转播放')
signal_start=0;
signal_skip=0;
else
play(audio);
disp('正在播放')
end
end
%每隔0.1秒检查歌曲是否放完,如果放完就切到下一首歌
loc=get(audio,'CurrentSample');
while (loc
loc=get(audio,'CurrentSample');
minutes=floor(loc/FS/60);
seconds=floor(loc/FS-minutes*60);
set(handles.edit1,'String',[num2str(minutes) '分' num2str(seconds) '秒']);
if (loc==1)
pushbutton1_Callback(hObject, eventdata, handles);
handles=guidata(hObject);
break
end
pause(0.1);
end
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global audio signal_start signal_stop
signal_start= get(audio,'CurrentSample'); %获取当前位置
signal_stop=1;%意味着确实是暂停
pause(audio);
disp('暂停播放')
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global audio
stop(audio);
disp('停止播放');
close all;
% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject handle to slider2 (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global Y FS signal_start signal_skip
loc=get(handles.slider2,'Value');
minutes=floor(length(Y)/FS*loc/60);
seconds=floor(length(Y)/FS*loc-minutes*60);
set(handles.edit1,'String',[num2str(minutes) '分' num2str(seconds) '秒']); %这样显示的应该是时间
signal_start=round(length(Y)*loc);
signal_skip=1;
% %显示
% cla;
% h=axes('xlabel','Time');
% axis([0 length(Y) 0 0]);
% axes(h)
% plot([0,0],[0,signal_start],'LineWidth',10)
pushbutton3_Callback(handles.pushbutton1, eventdata, handles);
handles=guidata(hObject);
% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (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 popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (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
% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over listbox1.
% --- Executes during object deletion, before destroying properties.
function pushbutton1_DeleteFcn(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object deletion, before destroying properties.
function pushbutton2_DeleteFcn(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object deletion, before destroying properties.
function pushbutton3_DeleteFcn(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit 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