WDPlayerRenderVLiveView.ets
3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import componentUtils from '@ohos.arkui.componentUtils';
import { WDPlayerController } from '../controller/WDPlayerController'
import { WindowModel } from 'wdKit';
import { Logger } from '../utils/Logger';
import { enableAliPlayer } from '../utils/GlobalSetting';
import { WDAliPlayerController } from '../controller/WDAliPlayerController';
class Size {
width: Length = "100%";
height: Length = "100%";
constructor(width: Length, height: Length) {
this.width = width;
this.height = height;
}
}
let insIndex: number = 0;
const TAG = 'WDPlayerRenderView'
class MGPlayRenderViewIns {
static intCount: number = 0;
static add() {
MGPlayRenderViewIns.intCount++;
WindowModel.shared.setWindowKeepScreenOn(true);
Logger.debug(TAG, "add-- +1")
}
static del() {
Logger.debug(TAG, "del-- -1")
MGPlayRenderViewIns.intCount--;
if (MGPlayRenderViewIns.intCount <= 0) {
WindowModel.shared.setWindowKeepScreenOn(false);
}
}
}
/**
* 播放窗口组件
*/
@Component
export struct WDPlayerRenderVLiveView {
private playerController?: WDAliPlayerController;
private xComponentController: XComponentController = new XComponentController();
private insId: string = "WDPlayRenderView" + insIndex;
onLoad?: ((event?: object) => void);
@State videoWidth: number = 0
@State videoHeight: number = 0
@State selfSize: Size = new Size('100%', '100%');
aboutToAppear() {
MGPlayRenderViewIns.add();
insIndex++;
if (!this.playerController) {
return
}
this.playerController.onVideoSizeChange = (width: number, height: number) => {
// console.log(`WDPlayerRenderView onVideoSizeChange width:${width} videoTop:${height}`)
// Logger.info(TAG, ` onVideoSizeChange width:${width} videoTop:${height}`)
console.error(TAG, ` onVideoSizeChange width:${width} videoTop:${height}`)
this.videoWidth = width;
this.videoHeight = height;
this.updateLayout()
}
}
aboutToDisappear() {
Logger.info(TAG, `aboutToDisappear`)
MGPlayRenderViewIns.del();
}
build() {
Row() {
// 设置为“surface“类型时XComponent组件可以和其他组件一起进行布局和渲染。
XComponent({
id: enableAliPlayer ? this.insId : 'xComponentId',
type: XComponentType.SURFACE,
libraryname: enableAliPlayer ? "premierlibrary" : undefined,
controller: this.xComponentController
})
.onLoad(async (event) => {
Logger.info(TAG, 'onLoad')
// const surfaceId = this.xComponentController.getXComponentSurfaceId()
this.xComponentController.setXComponentSurfaceSize({
surfaceWidth: 1920,
surfaceHeight: 1080
});
if (enableAliPlayer) {
this.playerController?.setSurfaceId(this.insId)
} else {
this.playerController?.setXComponentController(this.xComponentController)
}
if (this.onLoad) {
this.onLoad(event)
}
})// .width(this.selfSize.width)
// .height(this.selfSize.height)
.height('100%')
.width('100%')
.renderFit(RenderFit.RESIZE_FILL)
}
.id(this.insId)
.onAreaChange(() => {
// this.updateLayout()
})
.backgroundColor("#000000")
.height('100%')
.width('100%')
.renderFit(RenderFit.RESIZE_FILL)
}
updateLayout() {
let info = componentUtils.getRectangleById(this.insId);
if (info.size.width > 0 && info.size.height > 0 && this.videoHeight > 0 && this.videoWidth > 0) {
if (info.size.width / info.size.height > this.videoWidth / this.videoHeight) {
let scale = info.size.height / this.videoHeight;
this.selfSize = new Size((this.videoWidth * scale / info.size.width) * 100 + "%", '100%');
} else {
let scale = info.size.width / this.videoWidth;
this.selfSize = new Size('100%', (this.videoHeight * scale / info.size.height) * 100 + "%");
}
}
}
}