Play 2 wav files after each other

I am trying to play 2 files after each other.
However, I tried multiple things and it either plays the two files simultaniously or the second file does not get played at all.
This is th ecode I tried:
[y1, Fs1] = audioread('file1.wav'); %this file is 5 seconds long
[y2, Fs2] = audioread('file2.wav'); %this file is 1.6 seconds long
player = audioplayer(y1, Fs1);
player2 = audioplayer(y2, Fs2);
play(player);
pause(2);
play(player2);
Like this they just play simultaniously, without a time difference of 2 seconds.
File2 does not get played 2 seconds later than file1.
Can anyone help me with this?

Answers (1)

Use the playblocking function to get them to play the way you want them.
[y1, Fs1] = audioread('file1.wav'); %this file is 5 seconds long
[y2, Fs2] = audioread('file2.wav'); %this file is 1.6 seconds long
player = audioplayer(y1, Fs1);
player2 = audioplayer(y2, Fs2);
playblocking(player);
playblocking(player2);

2 Comments

thank you so much, that helped me play them right after each other, but without the 2 second delay I wanted.
Do you know how to implement that?
The pause function does not seam to work for this (as it keeps telling me: Index exceeds matrix dimensions)
My pleasure.
You did not mention the 2 second delay. I thought you were pausing after the first one to let it finish, and not allowing enough time.
Try this:
playblocking(player);
pause(2)
playblocking(player2);
That worked when I ran it.

Sign in to comment.

Asked:

on 19 Feb 2020

Commented:

on 19 Feb 2020

Community Treasure Hunt

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

Start Hunting!