如何使用 Matlab 在特定时间间隔播放音频文件?

How to play an audio file at a specific interval using Matlab?

正常:

player=audioplayer(snd1,FS1);
play(player);

%let as suppose that sound duration is 10 seconds
% and I wanted to play the sound from second 5 or 7... 
%   .. depending on the input of user(and using audioplayer libraries)

您可以使用 play(playerObj,[start,stop]) 函数指定 startstop

load handel;
playerObj = audioplayer(y,Fs);
start = 1;
stop = playerObj.SampleRate * 3;

play(playerObj,[start,stop]);

或者,您可以计算总数据的子样本并将其提供给 audioplayer:

load handel;
% y = data
% Fs = frequency = number of datapoints per second
% so calculate begin and end time using the sampling frequency:
totalTime = size(y,1)/Fs;  % all data divided by sampling frequency
beginTime = round(5*Fs);   % 5 seconds
endTime = round(7*Fs);     % 7 seconds

% playing full audio:
% player = audioplayer(y, Fs);
% play(player);

% playing only part of audio:
player = audioplayer(y(beginTime:endTime,:), Fs);
play(player);

音频播放器的 PLAY 方法支持名为 [startSample stopSample] 的附加输入参数。所以如果你想播放 5 到 7 秒之间的音频,你必须指定的范围是 [player.SampleRate*5 player.SampleRate*7].