Audioplayer object not passed into callback function
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Stephen23
il 30 Ago 2019
Modificato: Stephen23
il 30 Ago 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 Commenti
Stephen23
il 30 Ago 2019
Modificato: Stephen23
il 30 Ago 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!
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Audio and Video Data in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!