圖片播放器模仿視頻播放器

arm設(shè)備蝶念,安裝的瀏覽器無法播放視頻〉5校快速播放圖片苦酱,模仿播放視頻

第一版:直接快速播放網(wǎng)絡(luò)圖片,效果非常差颂跨,1秒5幀都無法滿足

第二版:使用雙緩沖策略扯饶,網(wǎng)頁(yè)上使用兩個(gè)image標(biāo)簽輪流顯示,顯示效果有提升钓丰,但是不大

第三版:加入預(yù)加載策略每币,增加一個(gè)長(zhǎng)度為5的預(yù)加載隊(duì)列兰怠,用于預(yù)先去網(wǎng)絡(luò)請(qǐng)求圖片,每顯示一張圖片的時(shí)候肥橙,都會(huì)去更新預(yù)加載隊(duì)列秸侣,刪除最早加入的圖片,并開始預(yù)加載新的圖片方篮。效果提升明顯励负,內(nèi)網(wǎng)環(huán)境下可滿足每秒20幀的要求

優(yōu)化點(diǎn):雙緩沖+預(yù)加載

<template>
  <div class="image-player">
    <!-- 播放器主體區(qū)域 -->
    <div class="player-container">
      <img
        :src="bufferA.src"
        alt="播放圖片"
        v-show="bufferA.isActive"
        @load="handleImageLoad('A')"
        :class="{ 'fade-in': bufferA.isActive }"
      />
      <img
        :src="bufferB.src"
        alt="播放圖片"
        v-show="bufferB.isActive"
        @load="handleImageLoad('B')"
        :class="{ 'fade-in': bufferB.isActive }"
      />
      <div v-if="isLoading" class="loading-indicator">加載中...</div>
    </div>


    <!-- 控制按鈕區(qū)域 -->
    <div class="controls">
      <button @click="togglePlay">
        {{ isPlaying ? "暫停" : "播放" }}
      </button>
      <span class="progress">
        {{ currentIndex + 1 }} / {{ images.length }}
      </span>
      <!-- 添加幀率選擇器 -->
      <div class="fps-selector">
        <span>幀率:</span>
        <select v-model="selectedFps">
          <option v-for="fps in fpsOptions" :key="fps" :value="fps">
            {{ fps }} FPS
          </option>
        </select>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "ImagePlayer",


  data() {
    return {
      // 圖片列表數(shù)組
      images: [
        // 添加更多圖片URL
      ],
      currentIndex: 0, // 當(dāng)前播放的圖片索引
      isPlaying: false, // 播放狀態(tài)
      playIntervalTimer: null, // 播放定時(shí)器
      // 添加雙緩沖相關(guān)的數(shù)據(jù)
      bufferA: {
        src: "",
        isActive: true,
        isLoaded: false,
      },
      bufferB: {
        src: "",
        isActive: false,
        isLoaded: false,
      },
      // 添加幀率相關(guān)數(shù)據(jù)
      fpsOptions: [1, 2, 5, 10, 15, 20, 24],
      selectedFps: 5, // 默認(rèn)5幀每秒
      preloadQueue: [], // 預(yù)加載隊(duì)列
      maxPreloadCount: 5, // 最大預(yù)加載數(shù)量
      isLoading: false,
    };
  },

  computed: {
    currentImage() {
      return this.images[this.currentIndex];
    },
    // 根據(jù)選擇的幀率計(jì)算播放間隔(毫秒)
    playInterval() {
      // 向下取整
      return Math.floor(1000 / this.selectedFps);
    },
  },
  created() {
    // 生成100張圖片
    for (let i = 0; i < 1000; i++) {
      this.images.push(`https://picsum.photos/200?t=${i}`);
    }
    // 初始化第一張圖片
    this.bufferA.src = this.images[0];
    // 初始化預(yù)加載隊(duì)列
    this.initPreloadQueue();
  },
  methods: {
    // 初始化預(yù)加載隊(duì)列
    initPreloadQueue() {
      const startIdx = (this.currentIndex + 1) % this.images.length;
      for (let i = 0; i < this.maxPreloadCount; i++) {
        const idx = (startIdx + i) % this.images.length;
        this.preloadImage(this.images[idx]);
      }
    },

    // 預(yù)加載圖片
    preloadImage(src) {
      if (!this.preloadQueue.includes(src)) {
        const img = new Image();
        img.src = src;
        this.preloadQueue.push(src);
        if (this.preloadQueue.length > this.maxPreloadCount) {
          this.preloadQueue.shift(); // 移除最早的預(yù)加載圖片
        }
      }
    },

    // 處理圖片加載
    handleImageLoad(buffer) {
      if (buffer === "A") {
        this.bufferA.isLoaded = true;
      } else {
        this.bufferB.isLoaded = true;
      }
      this.isLoading = false;
    },

    // 優(yōu)化后的播放下一張圖片方法
    async nextImage() {
      this.isLoading = true;
      const nextIndex = (this.currentIndex + 1) % this.images.length;

      // 切換緩沖區(qū)
      this.bufferA.isActive = !this.bufferA.isActive;
      this.bufferB.isActive = !this.bufferB.isActive;

      // 更新當(dāng)前索引
      this.currentIndex = nextIndex;

      // 預(yù)加載下一張圖片
      const nextBuffer = this.bufferA.isActive ? this.bufferB : this.bufferA;
      nextBuffer.isLoaded = false;
      nextBuffer.src = this.images[nextIndex];

      // 更新預(yù)加載隊(duì)列
      const preloadIndex =
        (nextIndex + this.maxPreloadCount) % this.images.length;
      this.preloadImage(this.images[preloadIndex]);
    },

    // 優(yōu)化后的播放控制
    play() {
      if (!this.isPlaying) {
        this.isPlaying = true;
        this.playIntervalTimer = setInterval(() => {
          if (!this.isLoading) {
            this.nextImage();
          }
        }, this.playInterval);
      }
    },

    // 切換播放/暫停狀態(tài)
    togglePlay() {
      if (this.isPlaying) {
        this.pause();
      } else {
        this.play();
      }
    },

    // 暫停播放
    pause() {
      this.isPlaying = false;
      if (this.playIntervalTimer) {
        clearInterval(this.playIntervalTimer);
        this.playIntervalTimer = null;
      }
    },
  },

  // 組件銷毀時(shí)清理定時(shí)器
  beforeDestroy() {
    // 清理預(yù)加載隊(duì)列
    this.preloadQueue = [];
    this.pause();
  },

  // 監(jiān)聽?zhēng)首兓?  watch: {
    selectedFps() {
      // 如果正在播放略吨,重新開始播放以應(yīng)用新的幀率
      if (this.isPlaying) {
        this.pause();
        this.$nextTick(() => {
          this.play();
        });
      }
    },
  },
};
</script>

<style scoped>
.image-player {
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
}


.player-container {
  width: 100%;
  aspect-ratio: 16/9;
  background: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.player-container img {
  position: absolute;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  will-change: transform; /* 優(yōu)化渲染性能 */
  transition: opacity 0.2s ease-in-out;
}

.controls {
  margin-top: 10px;
  display: flex;
  align-items: center;
  gap: 20px; /* 增加間距 */
}

button {
  padding: 8px 16px;
  cursor: pointer;
}

.progress {
  font-size: 14px;
}

/* 添加幀率選擇器樣式 */
.fps-selector {
  display: flex;
  align-items: center;
  gap: 8px;
}

.fps-selector select {
  padding: 4px 8px;
  border-radius: 4px;
  border: 1px solid #ccc;
  cursor: pointer;
}

.loading-indicator {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0, 0, 0, 0.5);
  color: white;
  padding: 10px;
  border-radius: 4px;
}

.fade-in {
  /* animation: fadeIn 0.2s ease-in-out; */
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
</style>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末鞠苟,一起剝皮案震驚了整個(gè)濱河市秽之,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌跨细,老刑警劉巖河质,帶你破解...
    沈念sama閱讀 218,122評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件掀鹅,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡戚丸,警方通過查閱死者的電腦和手機(jī)科吭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門对人,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人牺弄,你說我怎么就攤上這事势告。” “怎么了咱台?”我有些...
    開封第一講書人閱讀 164,491評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵回溺,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我萍恕,道長(zhǎng),這世上最難降的妖魔是什么崭倘? 我笑而不...
    開封第一講書人閱讀 58,636評(píng)論 1 293
  • 正文 為了忘掉前任司光,我火速辦了婚禮阔挠,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘跪削。我一直安慰自己迂求,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評(píng)論 6 392
  • 文/花漫 我一把揭開白布毫玖。 她就那樣靜靜地躺著凌盯,像睡著了一般驰怎。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上县忌,一...
    開封第一講書人閱讀 51,541評(píng)論 1 305
  • 那天症杏,我揣著相機(jī)與錄音,去河邊找鬼穴豫。 笑死逼友,一個(gè)胖子當(dāng)著我的面吹牛潘鲫,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播挖函,決...
    沈念sama閱讀 40,292評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼怨喘,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了肉拓?” 一聲冷哼從身側(cè)響起梳庆,我...
    開封第一講書人閱讀 39,211評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤膏执,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后欺栗,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體征峦,經(jīng)...
    沈念sama閱讀 45,655評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡栏笆,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了存哲。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片七婴。...
    茶點(diǎn)故事閱讀 39,965評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡打厘,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出户盯,到底是詐尸還是另有隱情饲化,我是刑警寧澤吃靠,帶...
    沈念sama閱讀 35,684評(píng)論 5 347
  • 正文 年R本政府宣布巢块,位于F島的核電站巧号,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏丹鸿。R本人自食惡果不足惜靠欢,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望庭敦。 院中可真熱鬧薪缆,春花似錦、人聲如沸拣帽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽减拭。三九已至蔽豺,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間拧粪,已是汗流浹背修陡。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留可霎,地道東北人魄鸦。 一個(gè)月前我還...
    沈念sama閱讀 48,126評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像癣朗,于是被迫代替她去往敵國(guó)和親拾因。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容