필터 지우기
필터 지우기

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

조회 수: 4 (최근 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

답변 (1개)

Hassaan
Hassaan 2023년 12월 30일
편집: Hassaan 2023년 12월 30일
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
Wei Sun
Wei Sun 2023년 12월 31일
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

댓글을 달려면 로그인하십시오.

카테고리

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

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by