audioCodec, channelCount, sampleRate));
int sampleCount = stream.getStreamCoder().getDefaultAudioFrameSize();
// create a place for audio samples and video pictures
IAudioSamples samples = IAudioSamples.make(sampleCount, channelCount);
IVideoPicture picture = IVideoPicture.make(IPixelFormat.Type.YUV420P, w, h);
// create the tone generator
TestAudioSamplesGenerator generator = new TestAudioSamplesGenerator();
generator.prepare(channelCount, sampleRate);
// make some media
long videoTime = 0;
long audioTime = 0;
long totalSamples = 0;
long totalSeconds = 6;
// the goal is to get 6 seconds of audio and video, in this case
// driven by audio, but kicking out a video frame at about the right
// time
while (totalSamples < sampleRate * totalSeconds)
{
// comput the time based on the number of samples
audioTime = (totalSamples * 1000 * 1000) / sampleRate;
// if the audioTime i>= videoTime then it's time for a video frame
if (audioTime >= videoTime)
{
BufferedImage image = new BufferedImage(w, h,
BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = image.createGraphics();
g.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
double theta = videoTime / 1000000d;
g.setColor(Color.RED);
g.rotate(theta, w / 2, h / 2);
g.fillRect(50, 50, 100, 100);
picture.setPts(videoTime);
writer.encodeVideo(videoStreamIndex, image, videoTime,
Global.DEFAULT_TIME_UNIT);
videoTime += deltaTime;
}
// generate audio
generator.fillNextSamples(samples, sampleCount);
writer.encodeAudio(audioStreamIndex, samples);
totalSamples += samples.getNumSamples();
}
// close the writer
writer.close();