Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
39c36717
D
Docs
项目概览
OpenHarmony
/
Docs
1 年多 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
39c36717
编写于
8月 23, 2023
作者:
F
flz
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify file in 0815 Signed-off-by: FULIZHONG<fulizhong1@huawei.com>
Signed-off-by:
N
flz
<
fulizhong1@huawei.com
>
上级
d593874d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
33 addition
and
35 deletion
+33
-35
zh-cn/application-dev/media/using-avplayer-for-playback.md
zh-cn/application-dev/media/using-avplayer-for-playback.md
+17
-18
zh-cn/application-dev/media/video-playback.md
zh-cn/application-dev/media/video-playback.md
+16
-17
未找到文件。
zh-cn/application-dev/media/using-avplayer-for-playback.md
浏览文件 @
39c36717
...
...
@@ -64,9 +64,10 @@
import
media
from
'
@ohos.multimedia.media
'
;
import
fs
from
'
@ohos.file.fs
'
;
import
common
from
'
@ohos.app.ability.common
'
;
import
{
BusinessError
}
from
'
@ohos.base
'
;
export
class
AVPlayerDemo
{
private
avPlayer
;
private
avPlayer
:
media
.
AVPlayer
;
private
count
:
number
=
0
;
private
isSeek
:
boolean
=
true
;
// 用于区分模式是否支持seek操作
private
fileSize
:
number
=
-
1
;
...
...
@@ -74,28 +75,24 @@ export class AVPlayerDemo {
// 注册avplayer回调函数
setAVPlayerCallback
()
{
// seek操作结果回调函数
this
.
avPlayer
.
on
(
'
seekDone
'
,
(
seekDoneTime
)
=>
{
this
.
avPlayer
.
on
(
'
seekDone
'
,
(
seekDoneTime
:
number
)
=>
{
console
.
info
(
`AVPlayer seek succeeded, seek time is
${
seekDoneTime
}
`
);
})
// error回调监听函数,当avPlayer在操作过程中出现错误时调用reset接口触发重置流程
this
.
avPlayer
.
on
(
'
error
'
,
(
err
)
=>
{
// error回调监听函数,当avPlayer在操作过程中出现错误时调用
reset接口触发重置流程
this
.
avPlayer
.
on
(
'
error
'
,
(
err
:
BusinessError
)
=>
{
console
.
error
(
`Invoke avPlayer failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
this
.
avPlayer
.
reset
();
// 调用reset重置资源,触发idle状态
})
// 状态机变化回调函数
this
.
avPlayer
.
on
(
'
stateChange
'
,
async
(
state
,
r
eason
)
=>
{
this
.
avPlayer
.
on
(
'
stateChange
'
,
async
(
state
:
string
,
reason
:
media
.
StateChangeR
eason
)
=>
{
switch
(
state
)
{
case
'
idle
'
:
// 成功调用reset接口后触发该状态机上报
console
.
info
(
'
AVPlayer state idle called.
'
);
this
.
avPlayer
.
release
();
// 调用release接口销毁实例对象
break
;
case
'
initialized
'
:
// avplayer 设置播放源后触发该状态上报
console
.
info
(
'
AVPlayerstate initialized called.
'
);
this
.
avPlayer
.
prepare
().
then
(()
=>
{
console
.
info
(
'
AVPlayer prepare succeeded.
'
);
},
(
err
)
=>
{
console
.
error
(
`Invoke prepare failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
console
.
info
(
'
AVPlayer state initialized called.
'
);
this
.
avPlayer
.
prepare
();
break
;
case
'
prepared
'
:
// prepare调用成功后上报该状态机
console
.
info
(
'
AVPlayer state prepared called.
'
);
...
...
@@ -166,9 +163,11 @@ export class AVPlayerDemo {
// 返回类型为{fd,offset,length},fd为HAP包fd地址,offset为媒体资源偏移量,length为播放长度
let
context
=
getContext
(
this
)
as
common
.
UIAbilityContext
;
let
fileDescriptor
=
await
context
.
resourceManager
.
getRawFd
(
'
01.mp3
'
);
let
avFileDescriptor
:
media
.
AVFileDescriptor
=
{
fd
:
fileDescriptor
.
fd
,
offset
:
fileDescriptor
.
offset
,
length
:
fileDescriptor
.
length
};
this
.
isSeek
=
true
;
// 支持seek操作
// 为fdSrc赋值触发initialized状态机上报
this
.
avPlayer
.
fdSrc
=
f
ileDescriptor
;
this
.
avPlayer
.
fdSrc
=
avF
ileDescriptor
;
}
// 以下demo为使用fs文件系统打开沙箱地址获取媒体文件地址并通过dataSrc属性进行播放(seek模式)示例
...
...
@@ -178,9 +177,9 @@ export class AVPlayerDemo {
// 创建状态机变化回调函数
this
.
setAVPlayerCallback
();
// dataSrc播放模式的的播放源地址,当播放为Seek模式时fileSize为播放文件的具体大小,下面会对fileSize赋值
let
src
=
{
let
src
:
media
.
AVDataSrcDescriptor
=
{
fileSize
:
-
1
,
callback
:
(
buf
,
length
,
pos
)
=>
{
callback
:
(
buf
:
ArrayBuffer
,
length
:
number
,
pos
:
number
)
=>
{
let
num
=
0
;
if
(
buf
==
undefined
||
length
==
undefined
||
pos
==
undefined
)
{
return
-
1
;
...
...
@@ -196,7 +195,7 @@ export class AVPlayerDemo {
// 通过UIAbilityContext获取沙箱地址filesDir,以Stage模型为例
let
pathDir
=
context
.
filesDir
;
let
path
=
pathDir
+
'
/01.mp3
'
;
await
fs
.
open
(
path
).
then
((
file
)
=>
{
await
fs
.
open
(
path
).
then
((
file
:
fs
.
File
)
=>
{
this
.
fd
=
file
.
fd
;
})
// 获取播放文件的大小
...
...
@@ -213,9 +212,9 @@ export class AVPlayerDemo {
// 创建状态机变化回调函数
this
.
setAVPlayerCallback
();
let
context
=
getContext
(
this
)
as
common
.
UIAbilityContext
;
let
src
:
object
=
{
let
src
:
media
.
AVDataSrcDescriptor
=
{
fileSize
:
-
1
,
callback
:
(
buf
,
length
,
pos
)
=>
{
callback
:
(
buf
:
ArrayBuffer
,
length
:
number
)
=>
{
let
num
=
0
;
if
(
buf
==
undefined
||
length
==
undefined
)
{
return
-
1
;
...
...
@@ -230,7 +229,7 @@ export class AVPlayerDemo {
// 通过UIAbilityContext获取沙箱地址filesDir,以Stage模型为例
let
pathDir
=
context
.
filesDir
;
let
path
=
pathDir
+
'
/01.mp3
'
;
await
fs
.
open
(
path
).
then
((
file
)
=>
{
await
fs
.
open
(
path
).
then
((
file
:
fs
.
File
)
=>
{
this
.
fd
=
file
.
fd
;
})
this
.
isSeek
=
false
;
// 不支持seek操作
...
...
zh-cn/application-dev/media/video-playback.md
浏览文件 @
39c36717
...
...
@@ -73,9 +73,10 @@
import
media
from
'
@ohos.multimedia.media
'
;
import
fs
from
'
@ohos.file.fs
'
;
import
common
from
'
@ohos.app.ability.common
'
;
import
{
BusinessError
}
from
'
@ohos.base
'
;
export
class
AVPlayerDemo
{
private
avPlayer
;
private
avPlayer
:
media
.
AVPlayer
;
private
count
:
number
=
0
;
private
surfaceID
:
string
;
// surfaceID用于播放画面显示,具体的值需要通过Xcomponent接口获取,相关文档链接见上面Xcomponent创建方法
private
isSeek
:
boolean
=
true
;
// 用于区分模式是否支持seek操作
...
...
@@ -84,29 +85,25 @@ export class AVPlayerDemo {
// 注册avplayer回调函数
setAVPlayerCallback
()
{
// seek操作结果回调函数
this
.
avPlayer
.
on
(
'
seekDone
'
,
(
seekDoneTime
)
=>
{
this
.
avPlayer
.
on
(
'
seekDone
'
,
(
seekDoneTime
:
number
)
=>
{
console
.
info
(
`AVPlayer seek succeeded, seek time is
${
seekDoneTime
}
`
);
})
// error回调监听函数,当avPlayer在操作过程中出现错误时调用reset接口触发重置流程
this
.
avPlayer
.
on
(
'
error
'
,
(
err
)
=>
{
this
.
avPlayer
.
on
(
'
error
'
,
(
err
:
BusinessError
)
=>
{
console
.
error
(
`Invoke avPlayer failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
this
.
avPlayer
.
reset
();
// 调用reset重置资源,触发idle状态
})
// 状态机变化回调函数
this
.
avPlayer
.
on
(
'
stateChange
'
,
async
(
state
,
r
eason
)
=>
{
this
.
avPlayer
.
on
(
'
stateChange
'
,
async
(
state
:
string
,
reason
:
media
.
StateChangeR
eason
)
=>
{
switch
(
state
)
{
case
'
idle
'
:
// 成功调用reset接口后触发该状态机上报
console
.
info
(
'
AVPlayer state idle called.
'
);
this
.
avPlayer
.
release
();
// 调用release接口销毁实例对象
break
;
case
'
initialized
'
:
// avplayer 设置播放源后触发该状态上报
console
.
info
(
'
AVPlayerstate initialized called.
'
);
console
.
info
(
'
AVPlayer
state initialized called.
'
);
this
.
avPlayer
.
surfaceId
=
this
.
surfaceID
;
// 设置显示画面,当播放的资源为纯音频时无需设置
this
.
avPlayer
.
prepare
().
then
(()
=>
{
console
.
info
(
'
AVPlayer prepare succeeded.
'
);
},
(
err
)
=>
{
console
.
error
(
`Invoke prepare failed, code is
${
err
.
code
}
, message is
${
err
.
message
}
`
);
});
this
.
avPlayer
.
prepare
();
break
;
case
'
prepared
'
:
// prepare调用成功后上报该状态机
console
.
info
(
'
AVPlayer state prepared called.
'
);
...
...
@@ -177,9 +174,11 @@ export class AVPlayerDemo {
// 返回类型为{fd,offset,length},fd为HAP包fd地址,offset为媒体资源偏移量,length为播放长度
let
context
=
getContext
(
this
)
as
common
.
UIAbilityContext
;
let
fileDescriptor
=
await
context
.
resourceManager
.
getRawFd
(
'
H264_AAC.mp4
'
);
let
avFileDescriptor
:
media
.
AVFileDescriptor
=
{
fd
:
fileDescriptor
.
fd
,
offset
:
fileDescriptor
.
offset
,
length
:
fileDescriptor
.
length
};
this
.
isSeek
=
true
;
// 支持seek操作
// 为fdSrc赋值触发initialized状态机上报
this
.
avPlayer
.
fdSrc
=
f
ileDescriptor
;
this
.
avPlayer
.
fdSrc
=
avF
ileDescriptor
;
}
// 以下demo为使用fs文件系统打开沙箱地址获取媒体文件地址并通过dataSrc属性进行播放(seek模式)示例
...
...
@@ -189,9 +188,9 @@ export class AVPlayerDemo {
// 创建状态机变化回调函数
this
.
setAVPlayerCallback
();
// dataSrc播放模式的的播放源地址,当播放为Seek模式时fileSize为播放文件的具体大小,下面会对fileSize赋值
let
src
=
{
let
src
:
media
.
AVDataSrcDescriptor
=
{
fileSize
:
-
1
,
callback
:
(
buf
,
length
,
pos
)
=>
{
callback
:
(
buf
:
ArrayBuffer
,
length
:
number
,
pos
:
number
)
=>
{
let
num
=
0
;
if
(
buf
==
undefined
||
length
==
undefined
||
pos
==
undefined
)
{
return
-
1
;
...
...
@@ -207,7 +206,7 @@ export class AVPlayerDemo {
// 通过UIAbilityContext获取沙箱地址filesDir,以Stage模型为例
let
pathDir
=
context
.
filesDir
;
let
path
=
pathDir
+
'
/H264_AAC.mp4
'
;
await
fs
.
open
(
path
).
then
((
file
)
=>
{
await
fs
.
open
(
path
).
then
((
file
:
fs
.
File
)
=>
{
this
.
fd
=
file
.
fd
;
})
// 获取播放文件的大小
...
...
@@ -224,9 +223,9 @@ export class AVPlayerDemo {
// 创建状态机变化回调函数
this
.
setAVPlayerCallback
();
let
context
=
getContext
(
this
)
as
common
.
UIAbilityContext
;
let
src
:
object
=
{
let
src
:
media
.
AVDataSrcDescriptor
=
{
fileSize
:
-
1
,
callback
:
(
buf
,
length
,
pos
)
=>
{
callback
:
(
buf
:
ArrayBuffer
,
length
:
number
)
=>
{
let
num
=
0
;
if
(
buf
==
undefined
||
length
==
undefined
)
{
return
-
1
;
...
...
@@ -241,7 +240,7 @@ export class AVPlayerDemo {
// 通过UIAbilityContext获取沙箱地址filesDir,以Stage模型为例
let
pathDir
=
context
.
filesDir
;
let
path
=
pathDir
+
'
/H264_AAC.mp4
'
;
await
fs
.
open
(
path
).
then
((
file
)
=>
{
await
fs
.
open
(
path
).
then
((
file
:
fs
.
File
)
=>
{
this
.
fd
=
file
.
fd
;
})
this
.
isSeek
=
false
;
// 不支持seek操作
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录