瀏覽代碼

播放页增加AB循环功能

onecold 10 月之前
父節點
當前提交
84e50de778

+ 25 - 0
entry/src/main/ets/common/util/CommUtils.ets

@@ -76,4 +76,29 @@ export function getTransverterText(message: string, transverterType:number):stri
     return message;
   }
 
+}
+
+export function  stringForTime(timeMs: number): string {
+  let totalSeconds: number | string = (timeMs / 1000);
+  let newSeconds: number | string = totalSeconds % 60;
+  let minutes: number | string = (totalSeconds / 60) % 60;
+  let hours: number | string = totalSeconds / 3600;
+  //LogUtils.getInstance().LOGI("stringForTime hours:" + hours + ",minutes:" + minutes + ",seconds:" + newSeconds);
+  hours = completionNum(Math.floor(Math.floor(hours * 100) / 100));
+  minutes = completionNum(Math.floor(Math.floor(minutes * 100) / 100));
+  newSeconds = Math.floor(Math.floor(newSeconds * 100) / 100)
+  newSeconds = completionNum(newSeconds);
+  if (hours > 0) {
+    return hours + ":" + minutes + ":" + newSeconds;
+  } else {
+    return minutes + ":" + newSeconds;
+  }
+}
+
+export function  completionNum(num: number): string | number {
+  if (num < 10) {
+    return '0' + num;
+  } else {
+    return num;
+  }
 }

+ 121 - 0
entry/src/main/ets/view/ABLoopComptent.ets

@@ -0,0 +1,121 @@
+import { common } from '@kit.AbilityKit';
+import { CommonConstants } from '../common/constants/CommonConstants';
+import { stringForTime } from '../common/util/CommUtils';
+
+// AB循环组件
+@Component
+export struct ABLoopComptent {
+  @Prop jumpATime: number
+  @Prop jumpBTime: number
+  onClickAItem =async () => {
+  }
+  onClickBItem =async () => {
+  }
+  onCancleButtonClick = () => {
+  }
+  onStartButtonClick = () => {
+  }
+  @StorageProp('themeColor') themeColor: string = CommonConstants.DEFAULT_THEME_COLOR;
+  context = this.getUIContext().getHostContext() as common.UIAbilityContext
+
+
+
+  async aboutToAppear() {
+
+
+  }
+
+  build() {
+    Scroll(){
+
+
+      Column() {
+
+      // 滑动控制栏
+        Column() {
+          Button({ type: ButtonType.Capsule, stateEffect: true }) {
+            Row() {
+              Text('设置A点:')
+                .fontSize(16)
+                .fontColor(Color.White)
+              Text(stringForTime(this.jumpATime))
+                .fontSize(16)
+                .fontColor(Color.White)
+            }
+            .margin({  bottom: 5 })
+
+          }
+          .height(50)
+          .width(180)
+          .backgroundColor($r('app.color.title_bar_bg'))
+          .onClick(async () => {
+
+            this.onClickAItem()
+
+          })
+          .margin({ top: 20, bottom: 15 })
+
+          Button({ type: ButtonType.Capsule, stateEffect: true }) {
+            Row() {
+              Text('设置B点:')
+                .fontSize(16)
+                .fontColor(Color.White)
+              Text(stringForTime(this.jumpBTime))
+                .fontSize(16)
+                .fontColor(Color.White)
+            }
+            .margin({  bottom: 5 })
+
+          }
+          .height(50)
+          .width(180)
+          .margin({ top: 15, bottom: 20 })
+          .backgroundColor($r('app.color.title_bar_bg'))
+          .onClick(async () => {
+
+            this.onClickBItem()
+          })
+
+          Row() {
+            Button('取消')
+              .fontColor(Color.White)
+              .layoutWeight(1)
+              .height(50)
+              .width(90)
+              .backgroundColor($r('app.color.title_bar_bg'))
+              .stateEffect(true)
+              .margin({ right: 10 ,bottom: 20})
+              .onClick(async () => {
+                this.onCancleButtonClick()
+
+              })
+
+            Button('启用循环')
+              .fontColor(Color.White)
+              .layoutWeight(1)
+              .height(50)
+              .width(90)
+              .backgroundColor($r('app.color.title_bar_bg'))
+              .stateEffect(true)
+              .margin({ left: 10 ,bottom: 20})
+              .onClick(async () => {
+                this.onStartButtonClick()
+
+              })
+          }
+          .margin({ left: 38, right: 38, top: 10, bottom: 25 })
+          .width(260)
+          .height(50)
+
+        }
+
+      }
+    }
+    .width('100%')
+    .height('100%')
+  }
+
+
+
+}
+

+ 74 - 1
entry/src/main/ets/view/LocalMusic.ets

@@ -80,7 +80,7 @@ import { IndexerView } from './IndexerView';
 import { KeyCode } from '@kit.InputKit';
 import { KnockController } from '../controller/KnockController';
 import { DeleteComptent } from '../view/DeleteComptent'
-// import { FFmpeg, FFProgressMessageParser } from '@sj/ffmpeg';
+import { ABLoopComptent } from '../view/ABLoopComptent';
 const TAG = 'LocalMusic';
 
 const DEFAULT_INDEX =
@@ -2028,6 +2028,15 @@ export struct LocalMusic {
           .visibility(Visibility.Hidden)
           .height(CommonConstants.FULL_PERCENT)
           .width(CommonConstants.FULL_PERCENT)
+          .bindSheet($$this.isABSheet, this.ABLoopSheet(), {
+            height:this.isLandscape?'80%': '43%',
+            dragBar: true,
+            showClose: true,
+            preferType: this.currentBreakpoint !== BreakpointTypeEnum.SM ? SheetType.CENTER : SheetType.BOTTOM,
+            blurStyle:BlurStyle.Thin,
+            backgroundColor:Color.Transparent,
+            title: { title: 'AB循环' }
+          })
 
           Stack() {
             Row() {
@@ -10254,6 +10263,8 @@ export struct LocalMusic {
 
     { id: 11, image: $r('app.media.time_close'), title: '跳过头尾' },
 
+    { id:20,image: $r("app.media.a_b"), title: 'AB循环' },
+
     { id: 12, image: $r('app.media.toubo'), title: '投播' },
 
     { id: 3, image: $r('app.media.hua_zhong_hua'), title: '详情' },
@@ -10352,10 +10363,63 @@ export struct LocalMusic {
         }
         this.isShowMoreView = false
         break;
+      case 20://AB循环
+        this.isABSheet = !this.isABSheet;
+        this.isShowMoreView = false
+        break;
     }
 
   }
 
+
+  //AB循环
+  @State isOpenAB:boolean = false
+  @State jumpATime:number = 0
+  @State jumpBTime:number  = 0
+  @State isABSheet:boolean = false
+
+  @Builder
+  ABLoopSheet() {
+
+    ABLoopComptent({
+      jumpATime:this.jumpATime,
+      jumpBTime:this.jumpBTime,
+      onClickAItem: async ()=>{
+        if(this.mIjkMediaPlayer){
+          this.jumpATime = await this.mIjkMediaPlayer.getCurrentPosition()
+        }
+      },
+      onClickBItem: async ()=>{
+        if(this.mIjkMediaPlayer){
+          this.jumpBTime = await this.mIjkMediaPlayer.getCurrentPosition()
+          if(this.jumpBTime<=this.jumpATime){
+            ToastUtil.showToast('设置B点时间不能小于A点时间')
+            this.jumpBTime = 0
+          }
+        }
+      },
+      onCancleButtonClick: async ()=>{
+        this.isOpenAB = false
+        this.isABSheet = false
+      },
+      onStartButtonClick: async ()=>{
+        this.isABSheet = false
+        this.startABLoop()
+      },
+
+    })
+
+  }
+
+
+  //开始AB循环
+  startABLoop(){
+    if(this.mIjkMediaPlayer){
+      this.seekTo(this.jumpATime+'')
+      this.isOpenAB = true
+    }
+  }
+
   /**
    * 切换横竖屏
    *
@@ -10903,6 +10967,14 @@ export struct LocalMusic {
       this.randomColor =
         `rgb(${Math.floor(Math.random() * 256)},${Math.floor(Math.random() * 256)},${Math.floor(Math.random() * 256)})`
 
+      //如果是开启AB循环
+      if(this.isOpenAB){
+        if(position >=this.jumpBTime){
+          this.seekTo(this.jumpATime+'')
+          return
+        }
+      }
+
       // 判断是否播放到结束时间
       if (this.isOpenJump && duration > this.jumpEndTime * 1000) {
         if (position >= duration - this.jumpEndTime * 1000) {
@@ -10965,6 +11037,7 @@ export struct LocalMusic {
   private async play(url: string) {
     let that = this;
     that.showLoadIng();
+    this.isOpenAB = false
     //设置XComponent回调的context
     if (!!this.mContext) {
       this.mIjkMediaPlayer.setContext(this.mContext, 'xcomponentId');

+ 1 - 0
entry/src/main/resources/base/media/a_b.svg

@@ -0,0 +1 @@
+<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1759031474377" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5839" xmlns:xlink="http://www.w3.org/1999/xlink" width="64" height="64"><path d="M213.333333 640v85.333333c0 44.970667 40.533333 85.333333 85.333334 85.333334h128v85.333333H298.666667a170.666667 170.666667 0 0 1-170.666667-170.666667v-85.333333h85.333333z m554.666667-213.333333l187.733333 469.333333h-91.946666l-51.242667-128h-174.506667l-51.157333 128h-91.904L682.666667 426.666667h85.333333z m-42.666667 123.093333L672.128 682.666667h106.325333L725.333333 549.76zM128 128h256a128 128 0 0 1 95.36 213.333333A128 128 0 0 1 384 554.666667H128V128z m256 256H213.333333v85.333333h170.666667a42.666667 42.666667 0 0 0 0-85.333333z m341.333333-256a170.666667 170.666667 0 0 1 170.666667 170.666667v85.333333h-85.333333V298.666667a85.333333 85.333333 0 0 0-85.333334-85.333334h-128V128h128zM384 213.333333H213.333333v85.333334h170.666667a42.666667 42.666667 0 1 0 0-85.333334z" p-id="5840" fill="#ffffff"></path></svg>