- 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.
- 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.
- 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.
- 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.
- 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.
Play a wav file in background using parfeval and backgroundPool but there is no sound?
5 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Hassaan
2023-12-30
编辑:Hassaan
2023-12-30
Here are a few things you can check and try:
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Audio and Video Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!