Search code examples
c4

audio playback in c4 framework (c4iOS


new user to c4iOS framework. Working my way thru the tutorials/examples - wondering how one goes about playing back audio (as opposed to video, which is covered in the example texts).

thanks in advance for answering my less-than-advance 'n00b' question

-jf


Solution

  • Audio samples are fairly similar to movie objects, albeit they don't have an option like shouldAutoplay that will get them running as soon as the application loads.

    The easiest way to construct a sample is like this:

    @implementation C4WorkSpace {
        C4Sample *audioSample;
    }
    
    -(void)setup {
        audioSample = [C4Sample sampleNamed:@"C4Loop.aif"];
    }
    

    Which builds the audio sample object as a variable that you can then reference in other methods. For instance, if you want to play a sound clip when you first touch the screen you would do the following:

    -(void)touchesBegan {
        [audioSample play];
    }
    

    To toggle the playback for each touch, you would do something like:

    -(void)touchesBegan {
        if(audioSample.isPlaying) {
            [audioSample stop];
        } else {
            [audioSample play];
        }
    }
    

    A working copy of a C4 app that toggles playback can be found HERE.

    There are also a lot of properties for audio samples that let you control things like playback rate, volume, panning and so on.

    An example of changing the volume is like this:

    audioSample.volume = 0.5; //0 = mute, 1 = full volume
    

    An example of skipping to a specific time in a sample would be like:

    audioSample.currentTime = 1.0f; //this will put the "playhead" to 1.0 second
    

    You can have a look at the C4Sample documentation to see more properties and other aspects of the class. The documentation is also available via the Xcode organizer.