Problem playing audiofile from UI.

조회 수: 2 (최근 30일)
Camilo Domínguez
Camilo Domínguez 2020년 4월 19일
답변: Temirkhan Amanzhanov 2020년 6월 1일
Hi, I´m coding a simple button to reproduce an audio file on the UI. There seems to be a problem because it will analyze the audio but does not reproduce it. Eventhough if I run the same code on a empty matlab project without UI it will play. What is missing or what is the problem with the UI?
function pushbutton1_Callback(hObject, eventdata, handles)
[a, fs]=audioread('909.wav')
p=audioplayer(a,fs)
play(p)

채택된 답변

Geoff Hayes
Geoff Hayes 2020년 4월 20일
Camilo - I suspect that the problem is that the audio player p is a local variable in your function. So as soon as the last line of code in the function is executed, the function is finished and is removed from the function call stack and so all variables declared within are "destroyed" including p...and so playback will end before it begins. Since you are using GUIDE, you can get around this by saving the player to the handles structure so that it persists outside of this function:
function pushbutton1_Callback(hObject, eventdata, handles)
[a, fs]=audioread('909.wav')
handles.p=audioplayer(a,fs)
guidata(hObject, handles); % <------ important! saves the updated structure
play(handles.p)
Now p is part of handles (and you must call guidata to save this updated structure). Try this out and see what happens!
Alternatively, you can use playblocking instead of play as playblocking does not return control until playback completes.

추가 답변 (1개)

Temirkhan Amanzhanov
Temirkhan Amanzhanov 2020년 6월 1일
I have the same problem in App Designer. Could you help me please?

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by