Skip to content

Commit

Permalink
fix: 修改视频宽高计算时的除数为0时的判断 (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
longgegege authored Oct 18, 2024
1 parent eead2a0 commit de975a4
Showing 1 changed file with 46 additions and 39 deletions.
85 changes: 46 additions & 39 deletions harmony/rn_video/src/main/ets/controller/VideoController.ets
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import media from '@ohos.multimedia.media';
import audio from '@ohos.multimedia.audio';
import Logger from '../common/util/Logger';
import { AvplayerStatus, CommonConstants, Events, ResizeMode,CurrentTime } from '../common/constants/CommonConstants';
import { AvplayerStatus, CommonConstants, Events, ResizeMode, CurrentTime } from '../common/constants/CommonConstants';
import { PlayConstants } from '../common/constants/PlayConstants';
import { AudioTracksBean, VideoTracksBean } from '../common/bean/AudioTracksBean';
import { IPlayPlayer } from '../view/PlayPlayer';
Expand All @@ -51,9 +51,10 @@ export class VideoController {
private preMutedVolume: number = 1.0;
private mMuted: boolean = false;
private flagtd: boolean = false;
private mVideoBufferedDuration: number = 0; // 对标安卓,获取缓冲时间, mVideoBufferedDuration = (int) Math.round((double) (mVideoDuration * percent) / 100.0); playableDuration = mVideoBufferedDuration / 1000.0
public audioTracksBeanArr?: AudioTracksBean[] ;
public videoTracksBeanArr?: VideoTracksBean[] ;
private mVideoBufferedDuration: number =
0; // 对标安卓,获取缓冲时间, mVideoBufferedDuration = (int) Math.round((double) (mVideoDuration * percent) / 100.0); playableDuration = mVideoBufferedDuration / 1000.0
public audioTracksBeanArr?: AudioTracksBean[];
public videoTracksBeanArr?: VideoTracksBean[];
private mPreventsDisplaySleepDuringVideoPlayback: boolean = true; /* 默认true*/

constructor() {
Expand All @@ -63,16 +64,16 @@ export class VideoController {
/**
* Creates a videoPlayer object.
*/
createAVPlayer(): void {
media.createAVPlayer().then((avPlayer) => {
createAVPlayer(): void {
media.createAVPlayer().then((avPlayer) => {
if (avPlayer) {
Logger.debug(TAG, `createAVPlayer isNull: ${avPlayer == null}`);
this.avPlayer = avPlayer;
this.bindState();
Logger.debug(TAG, `createAVPlayer after bind`);
if (this.flagtd) {
this.flagtd = false;
if(typeof this.url === "string"){
if (typeof this.url === "string") {
this.avPlayer.url = this.url;
} else {
this.avPlayer.fdSrc = this.url;
Expand All @@ -92,7 +93,7 @@ export class VideoController {
*/
bindState(): void {
Logger.debug(TAG, `bindState`);
if (this.avPlayer){
if (this.avPlayer) {

this.onStateChange();

Expand Down Expand Up @@ -128,7 +129,7 @@ export class VideoController {
this.onVideoBuffer(false);
break;
case media.BufferingInfoType.BUFFERING_PERCENT: // 表示缓存百分比
/* 此时的value有值,表示缓存百分比*/
/* 此时的value有值,表示缓存百分比*/
break;
case media.BufferingInfoType.CACHED_DURATION: // 表示缓存时长,毫秒(ms)。
Logger.debug(TAG, `onVideoProgress CACHED_DURATION value = ${value}`);
Expand All @@ -140,7 +141,7 @@ export class VideoController {
})

this.avPlayer.on(Events.SEEK_DONE, (seekDoneTime: number) => {
Logger.debug(TAG, `seekDone success,and seek time is: ${seekDoneTime} `);
Logger.debug(TAG, `seekDone success,and seek time is: ${seekDoneTime} `);
})
}
Logger.debug(TAG, `bindState end`);
Expand All @@ -156,15 +157,15 @@ export class VideoController {
this.avPlayer.on(Events.STATE_CHANGE, async (state) => {
switch (state) {
case AvplayerStatus.IDLE:
if(typeof this.url === "string"){
if (typeof this.url === "string") {
this.avPlayer.url = this.url;
}else{
} else {
this.avPlayer.fdSrc = this.url;
}
this.onVideoLoadStart(JSON.stringify(this.url));
break;
case AvplayerStatus.INITIALIZED:
if(this.playerThis.disableFocus){
if (this.playerThis.disableFocus) {
let audioRendererInfo = {
usage: audio.StreamUsage.STREAM_USAGE_GAME,
rendererFlags: 0
Expand All @@ -184,10 +185,12 @@ export class VideoController {
this.watchStatus();
if (this.playerThis.paused) {
this.avPlayer.pause();
}else {
} else {
this.avPlayer.play();
}
let startSecond = (this.avPlayer.currentTime == CurrentTime.CURRENTTIME_MINUS_ONE ? CurrentTime.CURRENTTIME_ZERO : this.avPlayer.currentTime);
let startSecond =
(this.avPlayer.currentTime == CurrentTime.CURRENTTIME_MINUS_ONE ? CurrentTime.CURRENTTIME_ZERO :
this.avPlayer.currentTime);
this.onVideoLoad(startSecond);
break;
case AvplayerStatus.PLAYING:
Expand Down Expand Up @@ -231,7 +234,7 @@ export class VideoController {
this.url = url;
this.surfaceId = surfaceId;
if (this.avPlayer && this.avPlayer.state == AvplayerStatus.IDLE) {
if(typeof this.url === "string"){
if (typeof this.url === "string") {
this.avPlayer.url = this.url;
} else {
this.avPlayer.fdSrc = this.url;
Expand Down Expand Up @@ -286,7 +289,7 @@ export class VideoController {
return;
}
if (AvplayerStatus.IDLE == this.avPlayer.state) {
if(typeof this.url === "string"){
if (typeof this.url === "string") {
this.avPlayer.url = this.url;
} else {
this.avPlayer.fdSrc = this.url;
Expand All @@ -305,7 +308,7 @@ export class VideoController {
if (paused) {
this.avPlayer?.pause();
} else {
this.avPlayer?.play();``
this.avPlayer?.play();
}
}

Expand Down Expand Up @@ -367,7 +370,7 @@ export class VideoController {
*/
setSeekTime(value: number): void {
Logger.debug(TAG, `setSeekTime== ${JSON.stringify(value)}`);
this.seekTime = value*1000;
this.seekTime = value * 1000;
Logger.debug(TAG, `setSeekTime== ${this.seekTime}`);
/* seek 只能在prepared/playing/paused/complete状态调用 毫秒(ms)*/
if (this.status === CommonConstants.STATUS_PREPARED ||
Expand Down Expand Up @@ -399,7 +402,7 @@ export class VideoController {
this.avPlayer.setVolume(this.playerThis.volume); /* 更新音量 */
Logger.debug(TAG, `updateProp resizeMode, ${JSON.stringify(this.playerThis.resizeMode)}`);
/* 更新 resizeMode */
this.convertResizeMode(this.playerThis.resizeMode);
this.convertResizeMode(this.playerThis.resizeMode);
/* 更新音频流类型 */
this.watchStatus();
} else {
Expand All @@ -420,42 +423,46 @@ export class VideoController {
this.playerThis.videoWidth = `${px2vp(this.avPlayer.width).toFixed(2)}`;
this.playerThis.videoHeight = `${px2vp(this.avPlayer.height).toFixed(2)}`;
break;
case ResizeMode.CONTAIN:
case ResizeMode.CONTAIN: {
this.playerThis.videoJustifyContent = FlexAlign.Center;
this.playerThis.videoAlignItems = HorizontalAlign.Center;
this.avPlayer.videoScaleType = media.VideoScaleType.VIDEO_SCALE_TYPE_FIT_CROP;

const viewWidth = this.playPageThis.viewWidth
const viewHeight = this.playPageThis.viewHeight
const playerWidth = this.avPlayer.width
const playerHeight = this.avPlayer.height
const videoRatio = playerWidth / playerHeight
const viewRatio = viewWidth / viewHeight
if (viewRatio > videoRatio) {
this.playerThis.videoHeight = `${viewHeight}`;
this.playerThis.videoWidth = `${viewHeight * videoRatio}`;
if (playerWidth != 0 && playerHeight != 0 && viewHeight != 0) {
const videoRatio = playerWidth / playerHeight
const viewRatio = viewWidth / viewHeight
if (viewRatio > videoRatio) {
this.playerThis.videoHeight = `${viewHeight}`;
this.playerThis.videoWidth = `${viewHeight * videoRatio}`;
} else {
this.playerThis.videoWidth = `${viewWidth}`;
this.playerThis.videoHeight = `${viewWidth / videoRatio}`;
}
} else {
this.playerThis.videoWidth = `${viewWidth}`;
this.playerThis.videoHeight = `${viewWidth / videoRatio}`;
Logger.error(TAG, `ResizeMode contain playerWidth or playerHeight or viewHeight = 0 `);
}
break;
}
case ResizeMode.COVER: // 保持视频宽高比拉伸至填满窗口,内容可能会有裁剪。
this.playerThis.videoJustifyContent = FlexAlign.Center;
this.playerThis.videoAlignItems = HorizontalAlign.Center;
/* 这种模式,取视频最小的边同窗口比进行缩放,最小边填满窗口最小边 */
/* 这种模式,取视频最小的边同窗口比进行缩放,最小边填满窗口最小边 */
this.avPlayer.videoScaleType = media.VideoScaleType.VIDEO_SCALE_TYPE_FIT_CROP;
if (this.avPlayer.height > this.avPlayer.width) { // 视频为竖屏
this.playerThis.videoWidth = CommonConstants.FULL_PERCENT; // 竖屏则宽度满屏,进行高度比例放大
let height = (CommonConstants.ONE_HUNDRED * this.avPlayer.height / this.avPlayer.width).toFixed(2);
this.playerThis.videoHeight = CommonConstants.FULL_PERCENT;
this.playerThis.matrix = Matrix4.identity()
.scale({x:1, y:Number.parseFloat(height)/100});
.scale({ x: 1, y: Number.parseFloat(height) / 100 });
} else {
this.playerThis.videoHeight = CommonConstants.FULL_PERCENT; // 横屏则高度满屏,进行宽度比例放大
let width = (CommonConstants.ONE_HUNDRED * this.avPlayer.width / this.avPlayer.height).toFixed(2);
this.playerThis.videoWidth = CommonConstants.FULL_PERCENT;
this.playerThis.matrix = Matrix4.identity()
.scale({x:Number.parseFloat(width)/100, y:1});
.scale({ x: Number.parseFloat(width) / 100, y: 1 });
}
break;
case ResizeMode.STRETCH:
Expand All @@ -464,8 +471,8 @@ export class VideoController {
this.playerThis.videoWidth = CommonConstants.FULL_PERCENT;
this.playerThis.videoHeight = CommonConstants.FULL_PERCENT;
break;
default :
break;
default:
break;
}
}

Expand All @@ -482,7 +489,7 @@ export class VideoController {
* @param playerThis PlayPlayer this object.
*
*/
initPlayerThis(playerThis:IPlayPlayer): void {
initPlayerThis(playerThis: IPlayPlayer): void {
if (playerThis == null) {
Logger.debug(TAG, `initPlayerThis is null .`);
}
Expand Down Expand Up @@ -547,7 +554,8 @@ export class VideoController {
if ((arrList) != null) {

/* 获取到音视频轨道信息后 回传*/
this.playPageThis.onVideoLoad(startSecond, this.duration, this.avPlayer.width, this.avPlayer.height, orientationIn);
this.playPageThis.onVideoLoad(startSecond, this.duration, this.avPlayer.width, this.avPlayer.height,
orientationIn);

} else {
Logger.debug(TAG, `getTrackDescription fail, error:${error}`);
Expand Down Expand Up @@ -603,7 +611,7 @@ export class VideoController {
/* 设置音频渲染信息,默认值contentType为CONTENT_TYPE_MUSIC,streamUsage为STREAM_USAGE_MEDIA。
只允许在initialized状态下设置 */
if (this.status === CommonConstants.STATUS_INITIALIZED) {
if (this.avPlayer){
if (this.avPlayer) {
if (this.playerThis.disableFocus) {
/* 设置音频流类型为视频,获取视频 音视频打断*/
let audioRendererInfo = {
Expand All @@ -618,7 +626,7 @@ export class VideoController {
this.status === CommonConstants.STATUS_START ||
this.status === CommonConstants.STATUS_PAUSE ||
this.status === CommonConstants.STATUS_COMPLETED
){
) {
/* setVolume 只能在prepared/playing/paused/complete状态调用 */
/* 20231227 - 不重复调用静音设置,在点击变更时再设置,switchMuted */
// this.switchMuted(this.mMuted);
Expand All @@ -644,5 +652,4 @@ export class VideoController {
this.playPageThis.onVideoError(errMsg);
}
}

}

0 comments on commit de975a4

Please sign in to comment.