Audioplayer object not passed into callback function
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
John Morrell
am 30 Aug. 2019
Bearbeitet: Stephen23
am 30 Aug. 2019
I am playing around with audioplayers and wanted to have a simple callback function that would print the time of the audio file when the audio player is played. Here is the script and associated callback function:
clc;clear all;close all
[audio, fs] = audioread("mute.mp3");
player = audioplayer(audio,fs)
player.StartFcn = 'audiocallbacktest'
play(player)
function audiocallbacktest(obj,event)
cur = obj.CurrentSample;
tot = obj.TotalSamples;
fs = obj.SampleRate;
timeTot = tot/fs;
timeCur = cur/fs;
strTot = secToTime(timeTot);
strCur = secToTime(timeCur);
disp("Play from "+strCur+" of "+strTot+".")
disp(secToTime(6531653));
end
The callback function is begin called, in fact as long as I don't try to use the variable obj in the function is works just fine. For example, this:
function audiocallbacktest(obj,event)
disp("Working?")
end
works just fine (displays "working?" to the terminal). Using the debugger to pause during the middle of the callback function revealed that in the audiocallbacktest function environment, there were no saved variables, no handles to the audioplayer object. I haven't seen anyone else with this problem, any ideas?
0 Kommentare
Akzeptierte Antwort
Stephen23
am 30 Aug. 2019
Bearbeitet: Stephen23
am 30 Aug. 2019
The problem is your definition of the callback as a character vector:
player.StartFcn = 'audiocallbacktest'
Defining callbacks as a character vector is not recommended and is basically obsolete. Callbacks defined as a character vector are NOT called with the same syntax as function handles: they are NOT provided with the default object and event input arguments as you assumed. Not only that, they are evaluated in the base workspace, which is unlikely to be useful in a well-designed GUI or any kind of OOP. Altogether it is best to forget about character vector callbacks entirely.
The recommended method of defining any callback is to use a function handle:
player.StartFcn = @audiocallbacktest;
and you will find that your problem has been resolved.
3 Kommentare
Stephen23
am 30 Aug. 2019
Bearbeitet: Stephen23
am 30 Aug. 2019
"I've found several (maybe outdated?) Mathworks pages "
The documentation installed on your computer is correct for the version that you have installed.
These are the two references you should be using. Third-party websites which have copies of documentation from twenty years ago are not helpful.
"...trying to figure out how the callback functions work has been pretty confusing"
That I completely understand.
One of the biggest challenges with callbacks/GUIs/etc. is going from the "linear" code to asynchronous code, which requires thinking differently about accessing variables and workspaces. Read the documentation, try the examples, ask us anytime you have questions!
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Audio and Video Data finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!