# Movie with audio after audio file

**URL:** <https://psychtoolbox.discourse.group/t/movie-with-audio-after-audio-file/1517>\
**Category:** Uncategorized\
**Created:** [December 21, 2016, 2:28pm UTC](https://psychtoolbox.discourse.group/t/movie-with-audio-after-audio-file/1517 "2016-12-21T14:28:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allen\_Ingling](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Allen\_Ingling](https://psychtoolbox.discourse.group/u/Allen_Ingling)\
**Post date:** [December 21, 2016, 2:28pm UTC](https://psychtoolbox.discourse.group/t/movie-with-audio-after-audio-file/1517/1 "2016-12-21T14:28:38Z")

</div>

&nbsp;&nbsp;&nbsp;&nbsp;

Hi all,

I'm trying to write an experiment using PTB&nbsp;in which trials where a simple audio file is played are alternated with trials where a movie (including audio track) is played.

The trials work separately, and when I first play a movie and then an audiofile, this works fine as well. The issue is when a moviefile is played after an audiofile has already been played. PTB just shows the first frame of the movie and freezes.

I've written a short demo to locate the problem. I'm using Matlab R2016a and PTB 3.0.12 on a Windows 7 64-bit OS.

Note that when I replace the "Screen('PlayMovie', movie, 1);" line with

Screen('PlayMovie', movie, 1, [], [], 2) to block the movie's audio, the movie DOES play, but of course without audio. So it seems there is a clash between the movie's audio and the previous use of PsychPortAudio (although I do close the audio object handle).

Is there any way to get around this?

Thanks!

--------

%% PTB & window  
[window, windowRect] = PsychImaging('OpenWindow', screenNumber, grey, [10, 10, 550, 550]);

%% audio  
InitializePsychSound;

audio = PsychPortAudio('Open', audio\_index-1, 1, 0, [], 1); % mono

wavedata = audioread(audfilename)';  
wavedata = resample(wavedata, 44100, 10000);

PsychPortAudio('FillBuffer', audio, wavedata);

PsychPortAudio('Stop', audio, 1);  
PsychPortAudio('Close', audio);

%video

movie = Screen('OpenMovie', window, moviename);

% Start playback engine:  
Screen('PlayMovie', movie, 1);

while true

&nbsp; % Wait for next movie frame, retrieve texture handle to it  
&nbsp; tex = Screen('GetMovieImage', window, movie, 1);

&nbsp; % Valid texture returned? A negative value means end of movie reached:  
&nbsp; if tex\<=0

&nbsp;&nbsp;&nbsp; % We're done, break out of loop:  
&nbsp;&nbsp;&nbsp; break;

&nbsp; end

&nbsp; Screen('DrawTexture', window, tex);

&nbsp; % Update display:  
&nbsp; Screen('Flip', window);

&nbsp; % Release texture:  
&nbsp; Screen('Close', tex);

end

% Close movie:  
Screen('CloseMovie', movie);

%%

Screen('CloseAll');

---

<div class="post-metadata">

**Author:** ![qx1147](https://avatars.discourse-cdn.com/v4/letter/q/b4bc9f/32.png) [@qx1147](https://psychtoolbox.discourse.group/u/qx1147)\
**Post date:** [December 21, 2016, 4:17pm UTC](https://psychtoolbox.discourse.group/t/movie-with-audio-after-audio-file/1517/2 "2016-12-21T16:17:46Z")

</div>

So apparently you already tried different audio APIs (via deviceid parameter of PsychPortAudio('Open',...), related to audio\_index in your code).  
How about removing the PsychPortAudio module altogether with "clear PsychPortAudio" after closing the audio port?

---

<div class="post-metadata">

**Author:** ![mariokleiner](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mariokleiner](https://psychtoolbox.discourse.group/u/mariokleiner)\
**Post date:** [December 22, 2016, 7:23am UTC](https://psychtoolbox.discourse.group/t/movie-with-audio-after-audio-file/1517/3 "2016-12-22T07:23:45Z")

</div>

If PsychPortAudio doesn't close down the audio device correctly, that would be a Windows operating system or audio device driver bug, nothing we could do about it. Maybe a PsychPortAudio('Close') would help, as it shuts down the whole driver?  
  
However, if you don't actually need the precise audio timing and control of PsychPortAudio, you can also use the movie playback functions to play back sound files == "movies without video". You can try just passing the name of an audio file instead of a movie file to the 'OpenMovie' function and it would play it. You couldn't use 'GetMovieImage' for obvious reasons, but would have to check progress in playback time via 'GetMovieTimeIndex' to know when the playback ends == returned time no longer progressing.  
  
-mario
