运行 光标 Pause/Play 音频 Matlab UI

Running Cursor Pause/Play Audio Matlab UI

我目前正在研究垂直移动光标,而不是在播放 wav 信号时随着声音移动。它单独运行良好,但是当我尝试 link 它到我的 Play/Pause 按钮时,它失败了。我发现使用 'uiwait' 和 'uiresume' 可能会有用,但尽管我很努力,但我无法让那条线在我想要的时候暂停和播放。

我对 Play/Pause 切换按钮的回调在这里:

`% --- Executes on button press in play_pauseTogglebutton.
function play_pauseTogglebutton_Callback(hObject, eventdata, handles)
% hObject    handle to play_pauseTogglebutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
linePlaying(handles.current_data);
if isplaying(handles.sound)
    pause(handles.sound)
    %c_sample = get(handles.sound,'CurrentSample');
    %disp(c_sample)
    handles.pause=1;
else
    if(handles.pause==1)
    resume(handles.sound);
    handles.pause=0;
    else
    play(handles.sound);
    handles.pause=0;
    end
end
% Hint: get(hObject,'Value') returns toggle state of play_pauseTogglebutton
% Save the handles structure.
guidata(hObject,handles)`

我的台词播放方法是:

`function linePlaying(current_data)
[y,fs]=wavread(current_data);
h=line([0,0],       [min(min(y)),max(max(y))],'color','b','Marker','*','MarkerEdgeColor','b','LineStyle','-','linewidth',2);
end_time=length(y)/fs;
tic
t=toc;
while (t<end_time)
    set(h,'xdata',t*[1,1])
    drawnow
    t=toc;
end
end`

感谢您的阅读,希望有人能帮助我:)

PS:我是Matlab的新手,而不是英语的新手。

编辑

GUI 的屏幕截图:

谢谢你:)

暂停按钮使线路停止。但我仍然有一个问题:这条线走得很快,改变 set 函数中的单位步骤没有任何改变...... 这是我的代码和您绘制线条的系统:

% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
AxisLim = axis;
ispushed=get(hObject,'Value');
if ispushed
    set(hObject,'String','pause')
    if(handles.pause==0)
        play(handles.sound);
    else
        resume(handles.sound);
    end
    while true

                LinePos = ceil(get(handles.L,'XData'));

                %// If the line reaches the axes limit, go back to start.
                if LinePos(1) == AxisLim(2)

                    set(handles.L,'XData',[1 1]);

                    %// drawnow is important!!
                    drawnow
                else

                %// Update position of th eline
                set(handles.L,'XData',[LinePos(1)+1 LinePos(2)+1])
                drawnow
                end

                %// If stop button was pressed, stop.
                if strcmp(get(hObject,'String'),'play')
                    break
                end

            end
    handles.pause=0;
else
    set(hObject,'String','play')
    pause(handles.sound);
    handles.pause=1;
end

% Update handles structure
guidata(hObject, handles);

这是我的做法。基本上我使用 Play/Stop 按钮的 String 属性 来切换移动线的显示。在此示例中,我使用静态图像,但这与您的数据相同。

代码有注释;请尝试一下,如果有什么不明白的地方就问!

使用以下代码创建一个新的 .m 文件:

function MoveSliderGUI
clc
clear all

global PlayStrings L
hFig = figure('Position',[100 100 400 400],'Units','normalized');

handles.axes1 = axes('Units','normalized','Position',[.2 .2 .6 .6]);

%// Create cell array with 2 strings.
PlayStrings = {'Play';'Stop'};

handles.PlayStopButton= uicontrol('Style','push','String',PlayStrings{1},'Position',[40 350 50 30],'Callback',@(s,e) PlayStopCallback);

handles.img = imread('peppers.png');

[~,NumCol,~] = size(handles.img);

imshow(handles.img,'parent',handles.axes1)

%// Get axis limits
AxisLim = axis;

%// Draw line
L = line([AxisLim(1) AxisLim(1)],[AxisLim(3) AxisLim(4)],'color','b','Marker','*','MarkerEdgeColor','b','LineStyle','-','linewidth',2);

guidata(hFig,handles);


    function PlayStopCallback(~,~)

        handles = guidata(hFig);

        %// Check current string in pushbutton
        if strcmp(get(handles.PlayStopButton,'String'),'Play')
            set(handles.PlayStopButton,'String','Stop')

            while true

                LinePos = ceil(get(L,'XData'));

                %// If the line reaches the axes limit, go back to start.
                if LinePos(1) == NumCol

                    set(L,'XData',[1 1]);

                    %// drawnow is important!!
                    drawnow
                else

                %// Update position of th eline
                set(L,'XData',[LinePos(1)+1 LinePos(2)+1])
                drawnow
                end

                %// If stop button was pressed, stop the movie.
                if strcmp(get(handles.PlayStopButton,'String'),'Play')
                    break
                end

            end

        else
            set(handles.PlayStopButton,'String','Play')
        end

        guidata(hFig,handles);
    end

end

这是它的屏幕录像:

然后您可以将声音文件的 "playing" 与线路移动同步。祝你好运!

编辑

我使用 [~,c,~] = size(handles.im) 获取显示图像的轴的尺寸。在您的情况下,这可能是 ~12,即 x 轴的上限。根据轴的单位,它可能是像素或标准化单位。

命令

AxisLimits = axis

将为您提供一个 4 元素向量,告诉您 [x_min x_max y_min y_max] 当前轴。