audioplayer

14 ビュー (過去 30 日間)
Mr Smart
Mr Smart 2011 年 10 月 17 日
コメント済み: Caleb Heide 2022 年 7 月 20 日
I made GUI in matlab to play .wav. But I have problem, at callback fcn. I put that code
[y,Fs] = wavread('signalExp01-22KHz.wav');
player=audioplayer(y,Fs);
play(player)
but that`s not work.
But I change like this
[y,Fs] = wavread('signalExp01-22KHz.wav');
sound(y,Fs);
that`s work. But I can`t stop music during playing.
  3 件のコメント
Mr Smart
Mr Smart 2011 年 10 月 18 日
when I used audio player in GUI.. at callback ... it`s not work.. didn`t hear music.
Daniel Shub
Daniel Shub 2011 年 10 月 18 日
How are your planning on stopping the audioplayer? Have you implemented this yet? My guess is that you haven't. I think when you save the handle player so that your can stop the music, you will find that your current function will begin to work. See my answer about variable scope.

サインインしてコメントする。

採用された回答

Daniel Shub
Daniel Shub 2011 年 10 月 17 日
The audioplayer object is "scoped". Once no more references to the handle exist, it stops playing and cleans up itself. See Loren's blog:
You need to save a copy of player someplace, but only long enough to let the sound play. You can use a timer with a callback to keep player in scope, and then have the timer callback delete player when it is done playing.
EDIT
Basically your variable player points to an audioplayer object. When there are no variables in memory that point to that audioplayer object, MATLAB deletes the object. MATLAB automatically takes care of the memory management. Your player variable gets deleted when your callback function exits (again MATLAB is taking care of the memory management). The key then is to either prevent your callback function from exiting until after the playing has finished or make sure a copy of player is "saved" someplace else.
The easiest is probably to replace
play(player)
with
playblocking(player)
A slightly better solution might be to do
play(player);
pause(max(size(y))/Fs);
The playblocking method will potentially block everything including callbacks. The pause method will allow callbacks to be processed. Both these methods, however, will halt the linear flow of the program until after the playback has finished. There are ways around this, but it depends on your program (see my comment to your question).
  3 件のコメント
Daniel Shub
Daniel Shub 2011 年 10 月 18 日
See my edit.
Mr Smart
Mr Smart 2011 年 10 月 22 日
Thanks , that`s work>> play(player);
pause(max(size(y))/Fs);<<
But I can`t stop and pause song during play,when I click Close Button and Toogle Button.For toogle button>> function togglebutton1_Callback(hObject, eventdata, handles)
[s,Fs] = wavread('signalExp01-22KHz.wav');
player=audioplayer(s,Fs);
play (player);
state=get(handles.togglebutton1,'Value');
if state == 1
set(handles.togglebutton1,'String','Pause');
resume(player);
else
pause(player);
set(handles.togglebutton1,'String','Play');
end <<

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2011 年 10 月 22 日
You can store the AudioPlayer object persistently:
function MyBeep
persistent AP
Play_Data = [sin(1:.6:400), sin(1:.7:400), sin(1:.4:400)]);
AP = audioplayer(Play_Data(:), 22050);
play(AP);
This does not block the execution and allows to stop the sound remotely - with implementing further logic in form of inputs. Drawback: This produces a crashdump when the Matlab session is closed in Matlab 6.5 and 2011b, although there is no problem in e.g. 2009a.
I've tried to use the callback of the AudioPlayer's StopFcn to delete the object after playing, but this let MATLAB crash also. I've reported this to the technical support already.
  2 件のコメント
Mr Smart
Mr Smart 2011 年 10 月 22 日
Thanks for helping Mr.Jan Simon
Caleb Heide
Caleb Heide 2022 年 7 月 20 日
Thanks for posting this.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAudio and Video Data についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by