幾乎所有的播放器都是基於ffmpeg,所以這就用ffmpeg 來說吧.
大家都只是stop其實做了幾個處理,一個是暫停播放,另外一個就是將播放位置seek到0點上.
除了這些,其實內部在seek的時候將播放隊列的buffer 全部都清空了.
為了不紙上談兵,這裡就貼一段ffplay的代碼
[cpp]
if (is->seek_req) {
int64_t seek_target= is->seek_pos;
int64_t seek_min= is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
int64_t seek_max= is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
//FIXME the +-2 is due to rounding being not done in the correct direction in generation
// of the seek_pos/seek_rel variables
ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
if (ret < 0) {
fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
}else{
if (is->audio_stream >= 0) {
packet_queue_flush(&is->audioq);
packet_queue_put(&is->audioq, &flush_pkt);
}
if (is->subtitle_stream >= 0) {
packet_queue_flush(&is->subtitleq);
packet_queue_put(&is->subtitleq, &flush_pkt);
}
if (is->video_stream >= 0) {
packet_queue_flush(&is->videoq);
packet_queue_put(&is->videoq, &flush_pkt);
}
} www.2cto.com
is->seek_req = 0;
eof= 0;
}
seek完之後調用flush把緩沖隊列的所有內容清空,包括音頻,視頻,字幕等數據.