private void handleSegment(byte[] mediaSegment, CachedSegment cachedSegment) throws IOException {
        File segmentFile = getTempFile(mContext, "seg" + cachedSegment.representation.id + "-" + cachedSegment.segment.range + "");
        long segmentPTSOffsetUs = 0;

        if(mMp4Mode) {
            /* The MP4 iso format needs special treatment because the Android MediaExtractor/MediaCodec
             * does not support the fragmented MP4 container format. Each segment therefore needs
             * to be joined with the init fragment and converted to a "conventional" unfragmented MP4
             * container file. */
            IsoFile baseIsoFile = new IsoFile(new MemoryDataSourceImpl(mInitSegments.get(cachedSegment.representation).asByteBuffer()));
            IsoFile fragment = new IsoFile(new MemoryDataSourceImpl(mediaSegment));

            /* The PTS in a converted MP4 always start at 0, so we read the offset from the segment
             * index box and work with it at the necessary places to adjust the local PTS to global
             * PTS concerning the whole stream. */
            List<SegmentIndexBox> segmentIndexBoxes = fragment.getBoxes(SegmentIndexBox.class);
            if(segmentIndexBoxes.size() > 0) {
                SegmentIndexBox sidx = segmentIndexBoxes.get(0);
                segmentPTSOffsetUs = (long) ((double) sidx.getEarliestPresentationTime() / sidx.getTimeScale() * 1000000);
            }
            /* If there is no segment index box to read the PTS from, we calculate the PTS offset
             * from the info given in the MPD. */
            else {
                segmentPTSOffsetUs = cachedSegment.number * cachedSegment.representation.segmentDurationUs;
            }

            Movie mp4Segment = new Movie();
            for(TrackBox trackBox : baseIsoFile.getMovieBox().getBoxes(TrackBox.class)) {
                mp4Segment.addTrack(new Mp4TrackImpl(null, trackBox, fragment));
            }
            Container mp4SegmentContainer = new DefaultMp4Builder().build(mp4Segment); // always create new instance to avoid memory leaks!
            FileOutputStream fos = new FileOutputStream(segmentFile, false);
            mp4SegmentContainer.writeContainer(fos.getChannel());
            fos.close();
        } else {
            // merge init and media segments into file
            BufferedSink segmentFileSink = Okio.buffer(Okio.sink(segmentFile));
            segmentFileSink.write(mInitSegments.get(cachedSegment.representation));
            segmentFileSink.write(mediaSegment);
            segmentFileSink.close();
        }

        cachedSegment.file = segmentFile;
        cachedSegment.ptsOffsetUs = segmentPTSOffsetUs;
    }
Handles a segment by merging it with the init segment into a temporary file.
MediaPlayer-DASH/src/main/java/net/protyposis/android/mediaplayer/dash/DashMediaExtractor.java