Filter löschen
Filter löschen

Play a wav file in background using parfeval and backgroundPool but there is no sound?

4 Ansichten (letzte 30 Tage)
Just want to play a wav file in background using parfeval and backgroundPool but there is no sound?
"a=1" is showing up correctly.
Any clue? Thanks.
sound_file='sound/output.wav';
[y,Fs] = audioread(sound_file);
parfeval(backgroundPool,@sound,1,y,Fs);
a=1

Antworten (1)

Hassaan
Hassaan am 30 Dez. 2023
Bearbeitet: Hassaan am 30 Dez. 2023
Here are a few things you can check and try:
  1. Audio Hardware Access: Make sure that the background workers have access to the audio hardware. Sometimes, background workers may not have the same access to system resources as the main MATLAB process.
  2. Scope of Variables: The variables y and Fs are passed to the sound function in the background. Ensure that they are not modified or cleared in the main MATLAB workspace before the background execution starts.
  3. Function Handle: Verify that the function handle @sound is correctly pointing to MATLAB's sound function and not to any other function with the same name.
  4. Return Value: The third argument of parfeval is the number of outputs the function returns. Since sound doesn't return any output, it should be 0 instead of 1.
  5. Error Checking: It's a good idea to check for errors in the background execution. You can do this by using the fetchOutputs function on the future object returned by parfeval.
sound_file = 'sound/output.wav';
[y, Fs] = audioread(sound_file);
future = parfeval(backgroundPool, @sound, 0, y, Fs); % No output argument
a = 1;
% Now check for errors.
try
fetchOutputs(future);
catch futureError
warning(futureError.ID,"Error playing sound: %s", futureError.message);
end
After the parfeval call, a = 1 is executed immediately because parfeval is non-blocking. The sound should play in the background, but if it's not, the error checking code may provide more insights into what's going wrong.
If you continue to have issues, it might be worth considering if playing sound asynchronously is necessary for your application, or if other methods of playing sound could be used that are more straightforward, such as using the audioplayer object and its play method, which is designed for more complex audio playback tasks.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
  1 Kommentar
Wei Sun
Wei Sun am 31 Dez. 2023
Thanks for your anser. I have figured out that sound itself can play wav in the background.
sound_file='sound/output.wav';
[y,Fs] = audioread(sound_file);
sound(y,Fs);
a=1

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by