2012-04-03 81 views
0

私はMATLAB関数を使用してサウンドを開始しています。この機能は次のとおりです。MATLAB:サウンドの再生を停止する

function playTone (duration, toneFreq) 
% Generate a tone 

global player; % as a global variable, sound will continue to play after the function has ended. 
samplesPerSecond = 44100; % the bit rate of the tone 
y = sin(linspace(0, duration * toneFreq * 2 * pi, round(duration * samplesPerSecond))); % the equation of the sound wave 
player = audioplayer(y, samplesPerSecond); % create an audio object from the sound wave at the specified bit rate 
play(player); % play the audio, blocking control until the sound completes 

私は、要求に応じて音を止めることができます。私が使用することはできません。

clear playsnd; 

私はaudioplayer()関数(ない音()関数)を使って音を扇動しているため。

私はまた、使用することはできません:(「???未定義の関数や変数 『プレーヤー』」)

stop(player); 

私は親関数からの音を停止しようとしていますので、

私が持っていましたサブ関数から音色を生成する必要があるので、上記のように関数を設定することができます。サウンドウィンドウを登録できないというエラーメッセージが表示されることがあるので、sound()関数を使用できません。 'player'変数はグローバルとして設定され、関数が完了した後もサウンドが引き続き再生されるようにします。

答えて

0

プレーヤーのハンドルを返すように関数を変更できますか?

function player = playTone (duration, toneFreq) 
% Generate a tone 

global player; % as a global variable, sound will continue to play after the function has ended. 
samplesPerSecond = 44100; % the bit rate of the tone 
y = sin(linspace(0, duration * toneFreq * 2 * pi, round(duration * samplesPerSecond))); % the equation of the sound wave 
player = audioplayer(y, samplesPerSecond); % create an audio object from the sound wave at the specified bit rate 
play(player); % play the audio 

その後、あなたはstop(player)を使用して、後でそれを停止することができます。

同様の質問:How to stop sound in MATLAB?

1

あなたは、あなたがそれを使用したいところはどこでもあなたがプレーヤーを停止する場合を含む、playerはグローバル変数であることを宣言する必要があります。

global player; 
stop(player); 

グローバル変数を使用するには、しかし、ですひんしゅくを買う。だから私はGeoffの提案を使用し、ハンドルを返すことをお勧めします。

関連する問題