Pause a movie, insert a sound, and countinue playing the movie?

Hi, I am newbie on Psychtoolbox. I searched demos but couldn't find a good solution to this.


In my current experiment, I want to add a keyboard control that every time I press the key, the ongoing movie (both audio and video) will pause, and a sound (.wav) will play. After the sound, the movie(both video and audio) will continue to play.


What I have right now is, the video of the movie will freeze, and both the .wav sound and movie audio will be playing at the same time, overlapping with each other.


Anyone has any idea how I can do it?


Thanks!

Quan 

Show us the minimal (minimum number of lines) of code of what you have now, the playing pausing etc of the movie and the playing of the sound


On Tue, Aug 26, 2014 at 5:26 AM, quan0726@... [PSYCHTOOLBOX] <PSYCHTOOLBOX@yahoogroups.com> wrote:


Hi, I am newbie on Psychtoolbox. I searched demos but couldn't find a good solution to this.


In my current experiment, I want to add a keyboard control that every time I press the key, the ongoing movie (both audio and video) will pause, and a sound (.wav) will play. After the sound, the movie(both video and audio) will continue to play.


What I have right now is, the video of the movie will freeze, and both the .wav sound and movie audio will be playing at the same time, overlapping with each other.


Anyone has any idea how I can do it?


Thanks!

Quan




Hi, Here is the part of code playing movie and the insert sound (attention getter), Thanks!

 % Playback loop: Runs until end of movie or keypress:
            while 1

                % Wait for next movie frame, retrieve texture handle to it
                 [tex, pts] = Screen('GetMovieImage', win, movie,1);
           
                    % Yes. Draw the new texture immediately to screen:                  
                    if (tex>0)
                        Screen('DrawTexture', win, tex);

                        % Update display:
                        Screen('Flip', win);
                        % Release texture:
                        Screen('Close', tex);
                    end
                   
                     % Valid texture returned? A negative value means end of movie reached:
                    if tex<=0
                      
                       
                      %  Screen('PlayMovie', movie, 1);
                        Screen('CloseMovie', movie);
                        trial = trial + 1;
                                                                         
                        break;
                    end

                if (keyIsDown==1 && keyCode(up))
                    % attention getter
                    function_att_getter;
                end;

        end



function function_att_getter
           
global st_dir freqout att_getter deviceid

            wavfilename = [st_dir 'sounds\mysound.WAV'];
            % play sound file
            [audiodata, freq] = audioread(char(wavfilename));
            % Perform basic initialization of the sound driver:
            InitializePsychSound(1);
            % buffer = [];
            nrchannels = 2;
            audiodata = resample(audiodata, freqout, freq);
            [~, ninchannels] = size(audiodata);
            audiodata = repmat(transpose(audiodata), nrchannels/ninchannels, 1);

            % Open the default audio device [], with default mode [] (==Only playback),
           
            pahandle = PsychPortAudio('Open', deviceid, [], 1, freqout, nrchannels);
%            pahandle = PsychPortAudio('Open', 12, [], 1, freqout, nrchannels);
            % Fill the audio playback buffer with the audio data 'wavedata':
            PsychPortAudio('FillBuffer', pahandle, audiodata);
            PsychPortAudio('Start', pahandle, [], 0, 1);

            % Wait for end of playback, then stop:
            PsychPortAudio('Stop', pahandle, 1);

           % Close audio device, shutdown driver:
            PsychPortAudio('Close');
end
erm, your main video playing loop loads each frame and displays it. when you call your sound function, control passes to that function and it doesn't return until the sound has been played, so of course the video freezes.

firstly, you should setup sound output only once in your experiment. currently every time you play a sound you are unnecessarily initialising, opening, and closing the sound device.

secondly, when you start the sound playing you do not want to wait for it to finish. you want to start sound playback and go right back to looping and displaying your images...