diff --git a/example/ohos/.gitignore b/example/ohos/.gitignore new file mode 100644 index 00000000..6ca13b31 --- /dev/null +++ b/example/ohos/.gitignore @@ -0,0 +1,19 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +*.har +**/BuildProfile.ets +**/oh-package-lock.json5 + +**/src/main/resources/rawfile/flutter_assets/ +**/libs/arm64-v8a/libapp.so +**/libs/arm64-v8a/libflutter.so +**/libs/arm64-v8a/libvmservice_snapshot.so diff --git a/example/ohos/AppScope/app.json5 b/example/ohos/AppScope/app.json5 new file mode 100644 index 00000000..473ae6e2 --- /dev/null +++ b/example/ohos/AppScope/app.json5 @@ -0,0 +1,10 @@ +{ + "app": { + "bundleName": "com.befovy.fijkplayer.fijkplayer_example", + "vendor": "example", + "versionCode": 1000000, + "versionName": "0.8.5", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} \ No newline at end of file diff --git a/example/ohos/AppScope/resources/base/element/string.json b/example/ohos/AppScope/resources/base/element/string.json new file mode 100644 index 00000000..34d3ca6f --- /dev/null +++ b/example/ohos/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "fijkplayer_example" + } + ] +} diff --git a/example/ohos/AppScope/resources/base/media/app_icon.png b/example/ohos/AppScope/resources/base/media/app_icon.png new file mode 100644 index 00000000..ce307a88 Binary files /dev/null and b/example/ohos/AppScope/resources/base/media/app_icon.png differ diff --git a/example/ohos/build-profile.json5 b/example/ohos/build-profile.json5 new file mode 100644 index 00000000..53bab2fa --- /dev/null +++ b/example/ohos/build-profile.json5 @@ -0,0 +1,27 @@ +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/example/ohos/entry/.gitignore b/example/ohos/entry/.gitignore new file mode 100644 index 00000000..2795a1c5 --- /dev/null +++ b/example/ohos/entry/.gitignore @@ -0,0 +1,7 @@ + +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/example/ohos/entry/build-profile.json5 b/example/ohos/entry/build-profile.json5 new file mode 100644 index 00000000..6de31ee2 --- /dev/null +++ b/example/ohos/entry/build-profile.json5 @@ -0,0 +1,15 @@ + +{ + "apiType": 'stageMode', + "buildOption": { + }, + "targets": [ + { + "name": "default", + "runtimeOS": "HarmonyOS" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/example/ohos/entry/hvigorfile.ts b/example/ohos/entry/hvigorfile.ts new file mode 100644 index 00000000..98b5db41 --- /dev/null +++ b/example/ohos/entry/hvigorfile.ts @@ -0,0 +1,3 @@ + +// Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently. +export { hapTasks } from '@ohos/hvigor-ohos-plugin'; diff --git a/example/ohos/entry/oh-package.json5 b/example/ohos/entry/oh-package.json5 new file mode 100644 index 00000000..79c7d017 --- /dev/null +++ b/example/ohos/entry/oh-package.json5 @@ -0,0 +1,11 @@ +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "fijkplayer": "file:../har/fijkplayer.har" + } +} \ No newline at end of file diff --git a/example/ohos/entry/src/main/ets/entryability/EntryAbility.ets b/example/ohos/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 00000000..f85a6550 --- /dev/null +++ b/example/ohos/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,10 @@ + +import { FlutterAbility, FlutterEngine } from '@ohos/flutter_ohos'; +import { GeneratedPluginRegistrant } from '../plugins/GeneratedPluginRegistrant'; + +export default class EntryAbility extends FlutterAbility { + configureFlutterEngine(flutterEngine: FlutterEngine) { + super.configureFlutterEngine(flutterEngine) + GeneratedPluginRegistrant.registerWith(flutterEngine) + } +} diff --git a/example/ohos/entry/src/main/ets/pages/Index.ets b/example/ohos/entry/src/main/ets/pages/Index.ets new file mode 100644 index 00000000..7bb6543f --- /dev/null +++ b/example/ohos/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,24 @@ + +import common from '@ohos.app.ability.common'; +import { FlutterPage } from '@ohos/flutter_ohos' + +let storage = LocalStorage.getShared() +const EVENT_BACK_PRESS = 'EVENT_BACK_PRESS' + +@Entry(storage) +@Component +struct Index { + private context = getContext(this) as common.UIAbilityContext + @LocalStorageLink('viewId') viewId: string = ""; + + build() { + Column() { + FlutterPage({ viewId: this.viewId }) + } + } + + onBackPress(): boolean { + this.context.eventHub.emit(EVENT_BACK_PRESS) + return true + } +} \ No newline at end of file diff --git a/example/ohos/entry/src/main/ets/plugins/GeneratedPluginRegistrant.ets b/example/ohos/entry/src/main/ets/plugins/GeneratedPluginRegistrant.ets new file mode 100644 index 00000000..74bace19 --- /dev/null +++ b/example/ohos/entry/src/main/ets/plugins/GeneratedPluginRegistrant.ets @@ -0,0 +1,26 @@ +import { FlutterEngine, Log } from '@ohos/flutter_ohos'; +import FijkPlugin from 'fijkplayer'; + +/** + * Generated file. Do not edit. + * This file is generated by the Flutter tool based on the + * plugins that support the Ohos platform. + */ + +const TAG = "GeneratedPluginRegistrant"; + +export class GeneratedPluginRegistrant { + + static registerWith(flutterEngine: FlutterEngine) { + try { + flutterEngine.getPlugins()?.add(new FijkPlugin()); + } catch (e) { + Log.e( + TAG, + "Tried to register plugins with FlutterEngine (" + + flutterEngine + + ") failed."); + Log.e(TAG, "Received exception while registering", e); + } + } +} diff --git a/example/ohos/entry/src/main/module.json5 b/example/ohos/entry/src/main/module.json5 new file mode 100644 index 00000000..bc840cba --- /dev/null +++ b/example/ohos/entry/src/main/module.json5 @@ -0,0 +1,49 @@ + +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "phone" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:icon", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:icon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "requestPermissions": [ + {"name" : "ohos.permission.INTERNET"}, + { + "name": "ohos.permission.READ_PASTEBOARD", + "reason": "$string:permission_READ_PASTEBOARD", + "usedScene": { + "abilities": [ + "EntryAbility" + ] + } + } + ] + } +} \ No newline at end of file diff --git a/example/ohos/entry/src/main/resources/base/element/color.json b/example/ohos/entry/src/main/resources/base/element/color.json new file mode 100644 index 00000000..3c712962 --- /dev/null +++ b/example/ohos/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/example/ohos/entry/src/main/resources/base/element/string.json b/example/ohos/entry/src/main/resources/base/element/string.json new file mode 100644 index 00000000..2a1132ab --- /dev/null +++ b/example/ohos/entry/src/main/resources/base/element/string.json @@ -0,0 +1,20 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "fijkplayer_example" + }, + { + "name": "permission_READ_PASTEBOARD", + "value": "Need to read clipboard content to support paste functionality" + } + ] +} \ No newline at end of file diff --git a/example/ohos/entry/src/main/resources/base/media/icon.png b/example/ohos/entry/src/main/resources/base/media/icon.png new file mode 100644 index 00000000..ce307a88 Binary files /dev/null and b/example/ohos/entry/src/main/resources/base/media/icon.png differ diff --git a/example/ohos/entry/src/main/resources/base/profile/main_pages.json b/example/ohos/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 00000000..1898d94f --- /dev/null +++ b/example/ohos/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/example/ohos/entry/src/main/resources/en_US/element/string.json b/example/ohos/entry/src/main/resources/en_US/element/string.json new file mode 100644 index 00000000..2a1132ab --- /dev/null +++ b/example/ohos/entry/src/main/resources/en_US/element/string.json @@ -0,0 +1,20 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "fijkplayer_example" + }, + { + "name": "permission_READ_PASTEBOARD", + "value": "Need to read clipboard content to support paste functionality" + } + ] +} \ No newline at end of file diff --git a/example/ohos/entry/src/main/resources/zh_CN/element/string.json b/example/ohos/entry/src/main/resources/zh_CN/element/string.json new file mode 100644 index 00000000..fde939cf --- /dev/null +++ b/example/ohos/entry/src/main/resources/zh_CN/element/string.json @@ -0,0 +1,20 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "模块描述" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "fijkplayer_example" + }, + { + "name": "permission_READ_PASTEBOARD", + "value": "需要读取剪贴板内容以支持粘贴文字功能" + } + ] +} \ No newline at end of file diff --git a/example/ohos/entry/src/ohosTest/ets/test/Ability.test.ets b/example/ohos/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 00000000..8abf7f2f --- /dev/null +++ b/example/ohos/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,35 @@ +import hilog from '@ohos.hilog'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium' + +export default function abilityTest() { + describe('ActsAbilityTest', function () { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(function () { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(function () { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(function () { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(function () { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + it('assertContain',0, function () { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let a = 'abc' + let b = 'b' + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b) + expect(a).assertEqual(a) + }) + }) +} \ No newline at end of file diff --git a/example/ohos/entry/src/ohosTest/ets/test/List.test.ets b/example/ohos/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 00000000..d766fe24 --- /dev/null +++ b/example/ohos/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,5 @@ +import abilityTest from './Ability.test' + +export default function testsuite() { + abilityTest() +} \ No newline at end of file diff --git a/example/ohos/entry/src/ohosTest/ets/testability/TestAbility.ets b/example/ohos/entry/src/ohosTest/ets/testability/TestAbility.ets new file mode 100644 index 00000000..e3f6e911 --- /dev/null +++ b/example/ohos/entry/src/ohosTest/ets/testability/TestAbility.ets @@ -0,0 +1,48 @@ +import UIAbility from '@ohos.app.ability.UIAbility'; +import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; +import hilog from '@ohos.hilog'; +import { Hypium } from '@ohos/hypium'; +import testsuite from '../test/List.test'; +import window from '@ohos.window'; + +export default class TestAbility extends UIAbility { + onCreate(want, launchParam) { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onCreate'); + hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? ''); + hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:'+ JSON.stringify(launchParam) ?? ''); + var abilityDelegator: any + abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() + var abilityDelegatorArguments: any + abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() + hilog.info(0x0000, 'testTag', '%{public}s', 'start run testcase!!!'); + Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite) + } + + onDestroy() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage) { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageCreate'); + windowStage.loadContent('testability/pages/Index', (err, data) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', + JSON.stringify(data) ?? ''); + }); + } + + onWindowStageDestroy() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageDestroy'); + } + + onForeground() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onForeground'); + } + + onBackground() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onBackground'); + } +} \ No newline at end of file diff --git a/example/ohos/entry/src/ohosTest/ets/testability/pages/Index.ets b/example/ohos/entry/src/ohosTest/ets/testability/pages/Index.ets new file mode 100644 index 00000000..545843d3 --- /dev/null +++ b/example/ohos/entry/src/ohosTest/ets/testability/pages/Index.ets @@ -0,0 +1,36 @@ + + +import hilog from '@ohos.hilog'; + +@Entry +@Component +struct Index { + aboutToAppear() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility index aboutToAppear'); + } + @State message: string = 'Hello World' + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + Button() { + Text('next page') + .fontSize(20) + .fontWeight(FontWeight.Bold) + }.type(ButtonType.Capsule) + .margin({ + top: 20 + }) + .backgroundColor('#0D9FFB') + .width('35%') + .height('5%') + .onClick(()=>{ + }) + } + .width('100%') + } + .height('100%') + } + } \ No newline at end of file diff --git a/example/ohos/entry/src/ohosTest/ets/testrunner/OpenHarmonyTestRunner.ts b/example/ohos/entry/src/ohosTest/ets/testrunner/OpenHarmonyTestRunner.ts new file mode 100644 index 00000000..47fc89b4 --- /dev/null +++ b/example/ohos/entry/src/ohosTest/ets/testrunner/OpenHarmonyTestRunner.ts @@ -0,0 +1,50 @@ + +import hilog from '@ohos.hilog'; +import TestRunner from '@ohos.application.testRunner'; +import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; + +var abilityDelegator = undefined +var abilityDelegatorArguments = undefined + +async function onAbilityCreateCallback() { + hilog.info(0x0000, 'testTag', '%{public}s', 'onAbilityCreateCallback'); +} + +async function addAbilityMonitorCallback(err: any) { + hilog.info(0x0000, 'testTag', 'addAbilityMonitorCallback : %{public}s', JSON.stringify(err) ?? ''); +} + +export default class OpenHarmonyTestRunner implements TestRunner { + constructor() { + } + + onPrepare() { + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner OnPrepare '); + } + + async onRun() { + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun run'); + abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() + abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() + var testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility' + let lMonitor = { + abilityName: testAbilityName, + onAbilityCreate: onAbilityCreateCallback, + }; + abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback) + var cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName + var debug = abilityDelegatorArguments.parameters['-D'] + if (debug == 'true') + { + cmd += ' -D' + } + hilog.info(0x0000, 'testTag', 'cmd : %{public}s', cmd); + abilityDelegator.executeShellCommand(cmd, + (err: any, d: any) => { + hilog.info(0x0000, 'testTag', 'executeShellCommand : err : %{public}s', JSON.stringify(err) ?? ''); + hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.stdResult ?? ''); + hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.exitCode ?? ''); + }) + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun end'); + } +} \ No newline at end of file diff --git a/example/ohos/entry/src/ohosTest/module.json5 b/example/ohos/entry/src/ohosTest/module.json5 new file mode 100644 index 00000000..3b02a75a --- /dev/null +++ b/example/ohos/entry/src/ohosTest/module.json5 @@ -0,0 +1,37 @@ + +{ + "module": { + "name": "entry_test", + "type": "feature", + "description": "$string:module_test_desc", + "mainElement": "TestAbility", + "deviceTypes": [ + "phone" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:test_pages", + "abilities": [ + { + "name": "TestAbility", + "srcEntry": "./ets/testability/TestAbility.ets", + "description": "$string:TestAbility_desc", + "icon": "$media:icon", + "label": "$string:TestAbility_label", + "exported": true, + "startWindowIcon": "$media:icon", + "startWindowBackground": "$color:start_window_background", + "skills": [ + { + "actions": [ + "action.system.home" + ], + "entities": [ + "entity.system.home" + ] + } + ] + } + ] + } +} diff --git a/example/ohos/entry/src/ohosTest/resources/base/element/color.json b/example/ohos/entry/src/ohosTest/resources/base/element/color.json new file mode 100644 index 00000000..3c712962 --- /dev/null +++ b/example/ohos/entry/src/ohosTest/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/example/ohos/entry/src/ohosTest/resources/base/element/string.json b/example/ohos/entry/src/ohosTest/resources/base/element/string.json new file mode 100644 index 00000000..65d8fa5a --- /dev/null +++ b/example/ohos/entry/src/ohosTest/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_test_desc", + "value": "test ability description" + }, + { + "name": "TestAbility_desc", + "value": "the test ability" + }, + { + "name": "TestAbility_label", + "value": "test label" + } + ] +} \ No newline at end of file diff --git a/example/ohos/entry/src/ohosTest/resources/base/media/icon.png b/example/ohos/entry/src/ohosTest/resources/base/media/icon.png new file mode 100644 index 00000000..ce307a88 Binary files /dev/null and b/example/ohos/entry/src/ohosTest/resources/base/media/icon.png differ diff --git a/example/ohos/entry/src/ohosTest/resources/base/profile/test_pages.json b/example/ohos/entry/src/ohosTest/resources/base/profile/test_pages.json new file mode 100644 index 00000000..b7e7343c --- /dev/null +++ b/example/ohos/entry/src/ohosTest/resources/base/profile/test_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "testability/pages/Index" + ] +} diff --git a/example/ohos/hvigor/hvigor-config.json5 b/example/ohos/hvigor/hvigor-config.json5 new file mode 100644 index 00000000..e7b855ac --- /dev/null +++ b/example/ohos/hvigor/hvigor-config.json5 @@ -0,0 +1,6 @@ + +{ + "modelVersion": "5.0.0", + "dependencies": { + } +} \ No newline at end of file diff --git a/example/ohos/hvigorfile.ts b/example/ohos/hvigorfile.ts new file mode 100644 index 00000000..acfff37c --- /dev/null +++ b/example/ohos/hvigorfile.ts @@ -0,0 +1,7 @@ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} \ No newline at end of file diff --git a/example/ohos/oh-package.json5 b/example/ohos/oh-package.json5 new file mode 100644 index 00000000..5efa2777 --- /dev/null +++ b/example/ohos/oh-package.json5 @@ -0,0 +1,20 @@ +{ + "modelVersion": "5.0.0", + "name": "fijkplayer_example", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "@ohos/flutter_ohos": "file:./har/flutter.har" + }, + "devDependencies": { + "@ohos/hypium": "1.0.6" + }, + "overrides": { + "@ohos/flutter_ohos": "file:./har/flutter.har", + "fijkplayer": "file:./har/fijkplayer.har", + "@ohos/flutter_module": "file:./entry" + } +} \ No newline at end of file diff --git a/ohos/.gitignore b/ohos/.gitignore new file mode 100644 index 00000000..c0f9ca4c --- /dev/null +++ b/ohos/.gitignore @@ -0,0 +1,9 @@ +/node_modules +/oh_modules +/.preview +/.idea +/build +/.cxx +/.test +/BuildProfile.ets +/oh-package-lock.json5 diff --git a/ohos/build-profile.json5 b/ohos/build-profile.json5 new file mode 100644 index 00000000..79961f96 --- /dev/null +++ b/ohos/build-profile.json5 @@ -0,0 +1,10 @@ +{ + "apiType": "stageMode", + "buildOption": { + }, + "targets": [ + { + "name": "default" + } + ] +} diff --git a/ohos/hvigorfile.ts b/ohos/hvigorfile.ts new file mode 100644 index 00000000..47e6e1f8 --- /dev/null +++ b/ohos/hvigorfile.ts @@ -0,0 +1,2 @@ +// Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently. +export { harTasks } from '@ohos/hvigor-ohos-plugin'; \ No newline at end of file diff --git a/ohos/index.ets b/ohos/index.ets new file mode 100644 index 00000000..adebe604 --- /dev/null +++ b/ohos/index.ets @@ -0,0 +1,4 @@ + + +import FijkplayerPlugin from './src/main/ets/components/fijkplayer/plugin/FijkplayerPlugin'; +export default FijkplayerPlugin; diff --git a/ohos/local.properties b/ohos/local.properties new file mode 100644 index 00000000..c1a4fa17 --- /dev/null +++ b/ohos/local.properties @@ -0,0 +1 @@ +hwsdk.dir=C:\\file\\ide\\deveco_install\\DevEco_Studio_5.0.9.300\\DevEco Studio\\sdk \ No newline at end of file diff --git a/ohos/oh-package.json5 b/ohos/oh-package.json5 new file mode 100644 index 00000000..0f039831 --- /dev/null +++ b/ohos/oh-package.json5 @@ -0,0 +1,12 @@ +{ + "name": "fijkplayer", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "index.ets", + "author": "", + "license": "Apache-2.0", + "dependencies": { + "@ohos/flutter_ohos": "file:./har/flutter.har", + "@zaiohos/ijkplayer-surface": "2.0.7-rc.4-surface-1.1" + } +} diff --git a/ohos/src/main/ets/components/fijkplayer/common/AssetsCopyUtil.ets b/ohos/src/main/ets/components/fijkplayer/common/AssetsCopyUtil.ets new file mode 100644 index 00000000..b168e6e1 --- /dev/null +++ b/ohos/src/main/ets/components/fijkplayer/common/AssetsCopyUtil.ets @@ -0,0 +1,85 @@ +import fs from '@ohos.file.fs'; +import { Log } from '@ohos/flutter_ohos'; + +const TAG = 'AssetsCopyUtil' +const FILE_PREFIX = 'flutter_assets/assets'; +const SEPARATOR_CHAR = '/'; + +/** + * 把resources/rawfile/flutter_assets/assets下所有资源,拷贝到files目录下 + */ +export function copyAssets(context: Context) { + if (!hasRawDir(context, 'flutter_assets')) { + log('flutter_assets not exists'); + return; + } + if (!hasRawDir(context, 'assets', 'flutter_assets')) { + log('flutter_assets/assets not exists'); + return; + } + copyFiles(context, FILE_PREFIX); +} + +function copyFiles(context: Context, parentPath: string) { + const list: Array = context.resourceManager.getRawFileListSync(parentPath); + for (let i = list.length - 1; i >= 0; i--) { + const filePath = parentPath + SEPARATOR_CHAR + list[i]; + if (context.resourceManager.isRawDir(filePath)) { + copyFiles(context, filePath); + } else { + copyToFile(context, filePath, parentPath); + } + } +} + +function hasRawDir(context: Context, dir: string, parentPath?: string) { + const list: Array = context.resourceManager.getRawFileListSync(parentPath ? parentPath : ''); + if (list.indexOf(dir) == -1) { + return false; + } + return context.resourceManager.isRawDir(parentPath ? (parentPath + SEPARATOR_CHAR) : '' + dir); +} + +/** + * + * @param origin rawFile下路径 + * @param target + */ +async function copyToFile(context: Context, origin: string, parentPath: string) { + try { + let originFile = await context.resourceManager.getRawFileContent(origin); + const targetParent = context.filesDir + SEPARATOR_CHAR + parentPath; + const targetPath = context.filesDir + SEPARATOR_CHAR + origin; + if (!fs.accessSync(targetParent)) { + fs.mkdirSync(targetParent, true); + } + if (fs.accessSync(targetPath)) { + log('file is exist,path=' + origin); + return; + } + let targetFile = fs.openSync(targetPath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); + fs.writeSync(targetFile.fd, originFile.buffer); + log('copy file success,path=' + targetPath); + } catch (e) { + log('failed:origin=' + origin + ",parentPath=" + parentPath); + log(JSON.stringify(e)); + } +} + +function log(msg: string) { + Log.d(TAG, msg); +} + +/** + * 路径转换 + * @param assetsPath assets下相对路径 + * @returns 拷贝资源的沙箱路径 + */ +export function getFilePathByAssetsPath(context: Context, assetsPath: string): string { + if (assetsPath.indexOf('/assets') != -1) { + return context.filesDir + SEPARATOR_CHAR + 'flutter_assets' + assetsPath; + } else { + return context.filesDir + SEPARATOR_CHAR + FILE_PREFIX + SEPARATOR_CHAR + assetsPath; + } +} + diff --git a/ohos/src/main/ets/components/fijkplayer/common/Constants.ets b/ohos/src/main/ets/components/fijkplayer/common/Constants.ets new file mode 100644 index 00000000..23d094ed --- /dev/null +++ b/ohos/src/main/ets/components/fijkplayer/common/Constants.ets @@ -0,0 +1,52 @@ +/** + * 参考:ijkplayer/src/main/cpp/ijkplayer/ijkplayer_android_def.h + */ +export class MediaInfoType { + static readonly MEDIA_INFO_UNKNOWN = 1; + static readonly MEDIA_INFO_STARTED_AS_NEXT = 2; + static readonly MEDIA_INFO_VIDEO_RENDERING_START = 3; + static readonly MEDIA_INFO_VIDEO_TRACK_LAGGING = 700; + static readonly MEDIA_INFO_BUFFERING_START = 701; + static readonly MEDIA_INFO_BUFFERING_END = 702; + static readonly MEDIA_INFO_NETWORK_BANDWIDTH = 703; + static readonly MEDIA_INFO_BAD_INTERLEAVING = 800; + static readonly MEDIA_INFO_NOT_SEEKABLE = 801; + static readonly MEDIA_INFO_METADATA_UPDATE = 802; + static readonly MEDIA_INFO_TIMED_TEXT_ERROR = 900; + static readonly MEDIA_INFO_VIDEO_ROTATION_CHANGED = 10001; + static readonly MEDIA_INFO_AUDIO_RENDERING_START = 10002; + static readonly MEDIA_INFO_AUDIO_DECODED_START = 10003; + static readonly MEDIA_INFO_VIDEO_DECODED_START = 10004; + static readonly MEDIA_INFO_OPEN_INPUT = 10005; + static readonly MEDIA_INFO_FIND_STREAM_INFO = 10006; + static readonly MEDIA_INFO_COMPONENT_OPEN = 10007; + static readonly MEDIA_INFO_VIDEO_SEEK_RENDERING_START = 10008; + static readonly MEDIA_INFO_AUDIO_SEEK_RENDERING_START = 10009; + static readonly MEDIA_INFO_MEDIA_ACCURATE_SEEK_COMPLETE = 10100; +} + + +/** + * 播放器的状态 + */ +export enum PlayerState { + unkown = -1, + idle = 0, + initialized = 1, + asyncPreparing = 2, + prepared = 3, + started = 4, + paused = 5, + completed = 6, + stopped = 7, + error = 8, + end = 9, +} + + +export class OptionsCategory { + static readonly OPT_CATEGORY_FORMAT = 1; + static readonly OPT_CATEGORY_CODEC = 2; + static readonly OPT_CATEGORY_SWS = 3; + static readonly OPT_CATEGORY_PLAYER = 4; +} \ No newline at end of file diff --git a/ohos/src/main/ets/components/fijkplayer/common/HostOption.ets b/ohos/src/main/ets/components/fijkplayer/common/HostOption.ets new file mode 100644 index 00000000..d923ff1d --- /dev/null +++ b/ohos/src/main/ets/components/fijkplayer/common/HostOption.ets @@ -0,0 +1,35 @@ +import { HashMap } from "@kit.ArkTS"; + + +export default class HostOption{ + static readonly REQUEST_AUDIOFOCUS = "request-audio-focus"; + static readonly RELEASE_AUDIOFOCUS = "release-audio-focus"; + static readonly REQUEST_SCREENON = "request-screen-on"; + static readonly ENABLE_SNAPSHOT = "enable-snapshot"; + + private mNumberOption:HashMap = new HashMap(); + private mStrOption:HashMap = new HashMap(); + + addNumberOption(key:string,value:number){ + this.mNumberOption.set(key,value); + } + + addStrOption(key:string,value:string){ + this.mStrOption.set(key,value); + } + + getNumberOption(key:string,defaultValue:number):number{ + if(this.mNumberOption.hasKey(key)){ + return this.mNumberOption.get(key); + } + return defaultValue; + } + + getStrOption(key:string,defaultValue:string):string{ + if(this.mStrOption.hasKey(key)){ + return this.mStrOption.get(key); + } + return defaultValue; + } + +} \ No newline at end of file diff --git a/ohos/src/main/ets/components/fijkplayer/common/SystemUtil.ets b/ohos/src/main/ets/components/fijkplayer/common/SystemUtil.ets new file mode 100644 index 00000000..e2753903 --- /dev/null +++ b/ohos/src/main/ets/components/fijkplayer/common/SystemUtil.ets @@ -0,0 +1,98 @@ +import window from '@ohos.window'; +import { common } from '@kit.AbilityKit'; +import { Log } from '@ohos/flutter_ohos'; +import { audio } from '@kit.AudioKit'; + +const TAG = 'SystemUtil' + +export async function setOrientation(context: common.Context, orientation: ScreenOrientation): Promise { + try { + Log.d(TAG, 'setOrientationLandscape()'); + const windowInstance: window.Window = await window.getLastWindow(context); + if (windowInstance) { + await windowInstance.setPreferredOrientation(getOrientationParams(orientation)); + return true; + } + } catch (e) { + Log.e(TAG, JSON.stringify(e)); + if (e['code'] == 1300002) { + return true; + } + } + return false; +} + +export async function setScreenLock(context: common.Context, isLock: boolean): Promise { + try { + const windowInstance: window.Window = await window.getLastWindow(context); + await windowInstance.setWindowKeepScreenOn(isLock); + return true; + } catch (e) { + Log.e(TAG, JSON.stringify(e)); + } + return false; +} + +export async function setBrightness(context: common.Context, brightness: number): Promise { + try { + const windowInstance: window.Window = await window.getLastWindow(context); + await windowInstance.setWindowBrightness(brightness); + return true; + } catch (e) { + Log.e(TAG, JSON.stringify(e)); + } + return false; +} + +/** + * 获取系统媒体音量 + * @returns + */ +export async function getSystemVolume(): Promise { + const value: audio.AudioVolumeGroupManager | undefined = await getAudioVolumeGroupManager(); + if (value) { + return value.getVolumeSync(audio.AudioVolumeType.MEDIA); + } else { + return 0; + } +} + +export async function isMute(): Promise { + const value: audio.AudioVolumeGroupManager | undefined = await getAudioVolumeGroupManager(); + if (value) { + return await value.isMute(audio.AudioVolumeType.MEDIA); + } else { + return false; + } +} + +async function getAudioVolumeGroupManager(): Promise { + let groupId: number = audio.DEFAULT_VOLUME_GROUP_ID; + let audioManager = audio.getAudioManager(); + let audioVolumeManager = audioManager.getVolumeManager(); + try { + const value: audio.AudioVolumeGroupManager = await audioVolumeManager.getVolumeGroupManager(groupId); + return value; + } catch (e) { + Log.e(TAG, JSON.stringify(e)); + return undefined; + } +} + + +function getOrientationParams(orientation: ScreenOrientation): window.Orientation { + switch (orientation) { + case ScreenOrientation.Portrait: + return window.Orientation.PORTRAIT; + case ScreenOrientation.Landscape: + return window.Orientation.LANDSCAPE; + case ScreenOrientation.Auto: + return window.Orientation.AUTO_ROTATION; + } +} + +export enum ScreenOrientation { + Portrait, + Landscape, + Auto, +} \ No newline at end of file diff --git a/ohos/src/main/ets/components/fijkplayer/core/FIjkPlayer.ets b/ohos/src/main/ets/components/fijkplayer/core/FIjkPlayer.ets new file mode 100644 index 00000000..11f3dee3 --- /dev/null +++ b/ohos/src/main/ets/components/fijkplayer/core/FIjkPlayer.ets @@ -0,0 +1,337 @@ +import { + EventChannel, + EventSink, + FlutterInjector, + FlutterManager, + FlutterPluginBinding, + Log, + MethodCall, + MethodCallHandler, + MethodChannel, + MethodResult, + StreamHandler +} from '@ohos/flutter_ohos'; +import { IjkMediaPlayer, MessageType, OnMessageCallback } from '@zaiohos/ijkplayer-surface'; +import { HashMap, uri } from '@kit.ArkTS'; +import { MediaInfoType, OptionsCategory, PlayerState } from '../common/Constants'; +import HostOption from '../common/HostOption'; +import { getFilePathByAssetsPath } from '../common/AssetsCopyUtil'; + +const TAG = "FIjkPlayerTag"; +const DEFAULT_WIDTH = 500; +const DEFAULT_HEIGHT = 500; + +export class FIjkPlayer implements MethodCallHandler, StreamHandler, OnMessageCallback { + private channel: MethodChannel | null = null; + private eventChannel: EventChannel | null = null; + private eventSink: EventSink | null = null; + private binding: FlutterPluginBinding; + private id: number; + private textureId?: number; + private surfaceId?: number; + private player?: IjkMediaPlayer; + private currentPlayerState: number = PlayerState.unkown; + private mWidth: number = 0; + private mHeight: number = 0; + private mRotate: number = 0; + private hasReset: boolean = false; + private mHostOption: HostOption = new HostOption(); + + constructor(id: number, binding: FlutterPluginBinding) { + this.id = id; + this.binding = binding; + this.channel = new MethodChannel(binding.getBinaryMessenger(), "befovy.com/fijkplayer/" + this.id); + this.channel.setMethodCallHandler(this); + this.eventChannel = new EventChannel(binding.getBinaryMessenger(), "befovy.com/fijkplayer/event/" + this.id); + this.eventChannel.setStreamHandler(this); + this.initPlayer(); + } + + onListen(args: ESObject, events: EventSink): void { + this.eventSink = events; + } + + onCancel(args: ESObject): void { + this.eventSink?.endOfStream(); + } + + initPlayer() { + this.player = new IjkMediaPlayer(); + //surface模式下,其它方法都依赖surfaceId,需要先设置surfaceId + this.setSurfaceId(); + this.player.setDebug(true); + this.setUpPlayer(); + this.player.setOnMessageCallback(this); + this.player.setMessageListener(); + } + + setUpPlayer() { + if (!this.player) { + return; + } + this.player.native_setup(); + this.player.setOption(OptionsCategory.OPT_CATEGORY_PLAYER.toString(), "enable-position-notify", '1'); + this.player.setOption(OptionsCategory.OPT_CATEGORY_PLAYER.toString(), "start-on-prepared", '0'); + } + + /** + * 纹理导出模式下,需更新bufferSize配置 + * @param width + * @param height + */ + updateSurfaceWH(width: number, height: number) { + this.binding.getTextureRegistry().setTextureBufferSize(this.textureId, width, height); + } + + setSurfaceId() { + this.textureId = this.binding.getTextureRegistry().getTextureId(); + this.surfaceId = this.binding.getTextureRegistry().registerTexture(this.textureId).getSurfaceId(); + this.player?.setSurfaceId(this.surfaceId); + } + + onMethodCall(call: MethodCall, result: MethodResult): void { + const methodName = call.method; + Log.d(TAG, 'onMethodCall=' + methodName); + switch (methodName) { + case "setupSurface": + result.success(this.textureId); + break; + case "setOption": + const category: number = call.argument('cat'); + const key: string = call.argument("key"); + if (call.hasArgument('long')) { + const value: number = call.argument('long'); + this.setOption(category, key, value); + } else if (call.hasArgument('str')) { + const value: string = call.argument('str'); + this.setOption(category, key, value); + } + result.success(null); + break; + case "applyOptions": + if (call.args instanceof Map) { + const map: Map = call.args as Map; + map.forEach((key: ESObject, value: ESObject) => { + if (typeof key === 'number' && value instanceof Map) { + const category = key as number; + const valueMap: Map = value as Map; + valueMap.forEach((key: ESObject, value: ESObject) => { + if (typeof key === 'string') { + const keyStr = key as string; + this.setOption(category, keyStr, value); + } + }) + } + }) + } + result.success(null); + break; + case "setDataSource": + if(this.hasReset){ + this.setUpPlayer(); + this.hasReset = false; + } + const url: string = call.argument('url'); + const uriObj = new uri.URI(url); + if (uriObj.scheme === 'asset') { + const filePath = uriObj.path; + this.player?.setDataSource(getFilePathByAssetsPath(this.binding.getApplicationContext(),filePath)); + } else { + this.player?.setDataSource(url); + } + this.handleEvent(MessageType.MEDIA_PLAYBACK_STATE_CHANGED, PlayerState.initialized, PlayerState.unkown, null); + result.success(null); + break; + case "prepareAsync": + if (this.player) { + this.player.prepareAsync(); + this.handleEvent(MessageType.MEDIA_PLAYBACK_STATE_CHANGED, PlayerState.asyncPreparing, + this.currentPlayerState, + null); + } + result.success(null); + break; + case "start": + if (this.player) { + this.player.start(); + } + result.success(null); + break; + case "pause": + if (this.player) { + this.player.pause(); + } + result.success(null); + break; + case "stop": + if (this.player) { + this.player.stop(); + this.handleEvent(MessageType.MEDIA_PLAYBACK_STATE_CHANGED, PlayerState.stopped, this.currentPlayerState, + null); + } + result.success(null); + break; + case "reset": + if (this.player) { + this.player.reset(); + this.handleEvent(MessageType.MEDIA_PLAYBACK_STATE_CHANGED, PlayerState.idle, this.currentPlayerState, null); + this.hasReset = true; + } + result.success(null); + break; + case "getCurrentPosition": + const position: number | undefined = this.player?.getCurrentPosition(); + result.success(position ? position : 0); + break; + case "setVolume": + const volume: number = call.argument('volume'); + const volumeStr = volume == undefined ? '1.0' : volume.toString(); + this.player?.setVolume(volumeStr, volumeStr); + result.success(null); + break; + case "seekTo": + const msec: number = call.argument('msec'); + if (this.currentPlayerState == PlayerState.completed) { + this.handleEvent(MessageType.MEDIA_PLAYBACK_STATE_CHANGED, PlayerState.paused, this.currentPlayerState, null); + } + const msecStr = msec == undefined ? '0' : msec.toString(); + this.player?.seekTo(msecStr); + result.success(null); + break; + case "setLoop": + const loop: number = call.argument('loop'); + const loopLocal = loop == undefined ? 1 : loop; + this.player?.setLoopCountNum(loopLocal); + result.success(null); + break; + case "setSpeed": + const speed: number = call.argument('speed'); + const speedLocal = speed == undefined ? 1 : speed; + this.player?.setSpeed(speedLocal.toString()); + result.success(null); + break; + case "snapshot": + result.notImplemented; + break; + + } + } + + onPlayerStateChangeLocal(newState: number, oldState: number) { + + } + + onMessage(what: number, arg1: number, arg2: number, obj: string) { + this.handleEvent(what, arg1, arg2, obj); + } + + handleEvent(what: number, arg1: number, arg2: number, extra: ESObject) { + Log.d(TAG, 'handleEvent(),what' + what); + const event: HashMap = new HashMap(); + switch (what) { + case MessageType.MEDIA_PREPARED: + event.set('event', 'prepared'); + event.set('duration', this.player?.getDuration()); + this.eventSink?.success(event); + break; + case MessageType.MEDIA_PLAYBACK_STATE_CHANGED: + this.currentPlayerState = arg1; + Log.d(TAG, `state_change(),new=${arg1},old=${arg2}`); + event.set("event", "state_change"); + event.set("new", arg1); + event.set("old", arg2); + this.onPlayerStateChangeLocal(arg1, arg2); + this.eventSink?.success(event) + break; + case MessageType.MEDIA_INFO: + switch (arg1) { + case MediaInfoType.MEDIA_INFO_VIDEO_RENDERING_START: + case MediaInfoType.MEDIA_INFO_AUDIO_RENDERING_START: + event.set("event", "rendering_start"); + event.set("type", arg1 === MediaInfoType.MEDIA_INFO_VIDEO_RENDERING_START ? 'video' : 'audio'); + this.eventSink?.success(event) + break; + case MediaInfoType.MEDIA_INFO_BUFFERING_START: + case MediaInfoType.MEDIA_INFO_BUFFERING_END: + event.set("event", "freeze"); + event.set("value", arg1 == MediaInfoType.MEDIA_INFO_BUFFERING_START); + this.eventSink?.success(event) + break; + case MediaInfoType.MEDIA_INFO_VIDEO_ROTATION_CHANGED: + event.set("event", "rotate"); + event.set("degree", arg2); + this.mRotate = arg2; + this.eventSink?.success(event); + if (this.mWidth > 0 && this.mWidth > 0) { + this.handleEvent(MessageType.MEDIA_SET_VIDEO_SIZE, this.mWidth, this.mHeight, null); + } + break; + } + break; + case MessageType.MEDIA_CURRENT_POSITION_UPDATE: + event.set("event", "pos"); + event.set("pos", arg1); + this.eventSink?.success(event); + break; + case MessageType.MEDIA_BUFFERING_UPDATE: + event.set("event", "buffering"); + event.set("head", arg1); + event.set("percent", arg2); + this.eventSink?.success(event); + break; + case MessageType.MEDIA_SET_VIDEO_SIZE: + event.set("event", "size_changed"); + if (this.mRotate == 0 || this.mRotate == 180) { + event.set('width', arg1); + event.set('height', arg2); + } else { + event.set('width', arg2); + event.set('height', arg1); + } + this.eventSink?.success(event); + this.mWidth = arg1; + this.mHeight = arg2; + this.updateSurfaceWH(this.mWidth, this.mHeight); + break; + case MessageType.MEDIA_SEEK_COMPLETE: + event.set("event", "seek_complete"); + event.set("pos", arg1); + event.set("err", arg2); + this.eventSink?.success(event); + break; + case MessageType.MEDIA_ERROR: + this.eventSink?.error(arg1.toString(), arg2.toString(), extra); + break; + } + } + + setOption(category: number | undefined, key: string, value: ESObject) { + if (typeof value === 'number') { + if (category != undefined && category != 0) { + this.player?.setOption(category.toString(), key, (value != undefined ? value : 0).toString()); + Log.d(TAG, `setOption1,category=${category},key=${key},value=${value}`); + } else if (category != undefined) { + // cat == 0, hostCategory + this.mHostOption.addNumberOption(key, value); + } + } else if (typeof value === 'string') { + if (category != undefined && category != 0) { + this.player?.setOption(category.toString(), key, value != undefined ? value : '0'); + Log.d(TAG, `setOption2,category=${category},key=${key},value=${value}`); + } else if (category != undefined) { + // cat == 0, hostCategory + this.mHostOption.addStrOption(key, value); + } else { + Log.d(TAG, "error arguments for setOptions"); + } + } + } + + getPlayId() { + return this.id; + } + + release() { + + } +} \ No newline at end of file diff --git a/ohos/src/main/ets/components/fijkplayer/plugin/FIjkPlayerPlugin.ets b/ohos/src/main/ets/components/fijkplayer/plugin/FIjkPlayerPlugin.ets new file mode 100644 index 00000000..fc23d2d0 --- /dev/null +++ b/ohos/src/main/ets/components/fijkplayer/plugin/FIjkPlayerPlugin.ets @@ -0,0 +1,181 @@ +import { + FlutterPlugin, + FlutterPluginBinding, + Log, + MethodCall, + MethodCallHandler, + MethodChannel, + MethodResult +} from '@ohos/flutter_ohos'; +import { HashMap } from '@kit.ArkTS'; +import { FIjkPlayer } from '../core/FIjkPlayer'; +import { BusinessError, deviceInfo } from '@kit.BasicServicesKit'; +import { getSystemVolume, ScreenOrientation, setBrightness, setOrientation, setScreenLock } from '../common/SystemUtil'; +import { audio } from '@kit.AudioKit'; +import { copyAssets } from '../common/AssetsCopyUtil'; + +const TAG = "FijkplayerPlugin"; + +/** FIjkPlayerPlugin **/ +export default class FIjkPlayerPlugin implements FlutterPlugin, MethodCallHandler { + private channel: MethodChannel | null = null; + private playerMap: HashMap = new HashMap(); + private nextPlayId = 0; + private binding?: FlutterPluginBinding; + private isScreenLock: boolean = false; + private windowBrightness: number = -1.0; + audioManager = audio.getAudioManager(); + // 创建音频会话管理器。 + audioSessionManager: audio.AudioSessionManager = this.audioManager.getSessionManager(); + // 设置音频并发模式。 + strategy: audio.AudioSessionStrategy = { + concurrencyMode: audio.AudioConcurrencyMode.CONCURRENCY_DEFAULT + }; + + getUniqueClassName(): string { + return "FIjkPlayerPlugin" + } + + onAttachedToEngine(binding: FlutterPluginBinding): void { + this.binding = binding; + this.channel = new MethodChannel(binding.getBinaryMessenger(), "befovy.com/fijk"); + this.channel.setMethodCallHandler(this) + + //拷贝flutter assets资源 + copyAssets(this.binding.getApplicationContext()); + } + + + onDetachedFromEngine(binding: FlutterPluginBinding): void { + if (this.channel != null) { + this.channel.setMethodCallHandler(null) + } + } + + private getNextPlayId(): number { + return this.nextPlayId++; + } + + + onMethodCall(call: MethodCall, result: MethodResult): void { + const methodName = call.method; + Log.d(TAG, 'onMethodCall=' + methodName); + switch (methodName) { + case "createPlayer": + if (!this.binding) { + return; + } + const player: FIjkPlayer = new FIjkPlayer(this.getNextPlayId(), this.binding); + this.playerMap.set(player.getPlayId(), player); + result.success(player.getPlayId()); + break; + case "releasePlayer": + const pid: number = call.argument('pid'); + const playerFind: FIjkPlayer = this.playerMap.get(pid); + if (playerFind) { + playerFind.release(); + this.playerMap.remove(pid); + } + result.success(null); + break; + case "getPlatformVersion": + result.success("OpenHarmony " + deviceInfo.sdkApiVersion); + break; + case "init": + result.success(null); + break; + case "logLevel": + break; + case "setOrientationPortrait": + if (this.binding) { + setOrientation(this.binding.getApplicationContext(), ScreenOrientation.Portrait).then((isChanged) => { + result.success(isChanged); + }); + } + break; + case "setOrientationLandscape": + Log.d(TAG, 'setOrientationLandscape()'); + if (this.binding) { + setOrientation(this.binding.getApplicationContext(), ScreenOrientation.Landscape).then((isChanged) => { + result.success(isChanged); + }); + } + break; + case "setOrientationAuto": + if (this.binding) { + setOrientation(this.binding.getApplicationContext(), ScreenOrientation.Auto).then((isChanged) => { + result.success(isChanged); + }); + } + break; + case "setScreenOn": + if (call.hasArgument('on') && this.binding) { + const isOn: boolean = call.argument('on'); + this.isScreenLock = isOn; + setScreenLock(this.binding.getApplicationContext(), isOn); + } + result.success(null); + break; + case "isScreenKeptOn": + result.success(this.isScreenLock); + break; + case "brightness": + result.success(this.windowBrightness); + break; + case "setBrightness": + if (call.hasArgument('brightness') && this.binding) { + const brightness: number = call.argument('brightness'); + this.windowBrightness = brightness; + setBrightness(this.binding.getApplicationContext(), brightness); + } + result.success(null); + break; + case "requestAudioFocus": + this.audioSessionManager.activateAudioSession(this.strategy).then(() => { + Log.d(TAG, 'activateAudioSession SUCCESS'); + }).catch((err: BusinessError) => { + Log.e(TAG, `ERROR: ${err}`); + }); + result.success(null); + break; + case "releaseAudioFocus": + this.audioSessionManager.deactivateAudioSession().then(() => { + Log.d(TAG, 'deactivateAudioSession SUCCESS'); + }).catch((err: BusinessError) => { + Log.e(TAG, `ERROR: ${err}`); + }); + result.success(null); + break; + case "volumeDown": + //ohos api仅系统应用支持设置音量 + break; + case "volumeUp": + //ohos api不支持 + break; + case "volumeMute": + //ohos api不支持 + break; + case "systemVolume": + getSystemVolume().then((volume: number) => { + result.success(volume); + }) + break; + case "volumeSet": + //ohos api不支持 + break; + case "volUiMode": + //设置音量ui相关 + break; + case "onLoad": + result.success(null); + break; + case "onUnload": + result.success(null); + break; + default: + Log.d(TAG, 'notImplemented(),method=' + methodName); + result.notImplemented(); + break; + } + } +} \ No newline at end of file diff --git a/ohos/src/main/module.json5 b/ohos/src/main/module.json5 new file mode 100644 index 00000000..3fb65562 --- /dev/null +++ b/ohos/src/main/module.json5 @@ -0,0 +1,10 @@ +{ + "module": { + "name": "fijkplayer", + "type": "har", + "deviceTypes": [ + "default", + "tablet" + ] + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 19c99b7c..75bfe9a0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -29,6 +29,8 @@ flutter: pluginClass: FijkPlugin ios: pluginClass: FijkPlugin + ohos: + pluginClass: FijkPlugin # To add assets to your plugin package, add an assets section, like this: # assets: