SurfaceProvider+Player视频播放开发过程分享 原创
1.视频播放的功能
显示效果:
点击图中文字跳转
部分代码如下:
Mainabilitysilce中
package com.example.jltflianxi.slice;
import com.example.jltflianxi.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.common.Source;
import ohos.media.player.Player;
import java.io.*;
import java.text.Format;
import java.util.jar.Manifest;
public class MainAbilitySlice extends AbilitySlice {
static final HiLogLabel logLabel=new HiLogLabel(HiLog.LOG_APP,0x001010,"视频测试");
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
Button button = (Button) findComponentById(ResourceTable.Id_jltf_btn);
if (button !=null){
HiLog.info(logLabel,"按键存在");
button.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
HiLog.info(logLabel,"跳转开始");
Intent intent = new Intent();
Operation operation = (Operation) new Intent.OperationBuilder()
.withAction("action.vedio_layout")
.build();
intent.setOperation(operation);
startAbility(intent);
HiLog.info(logLabel,"跳转结束");
}
});
}
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
vedioabilitysilce中
package com.example.jltflianxi.slice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.Surface;
import ohos.agp.graphics.SurfaceOps;
import ohos.global.resource.RawFileDescriptor;
import com.example.jltflianxi.ResourceTable;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.common.Source;
import ohos.media.player.Player;
public class VideoAbilitySlice extends AbilitySlice {
static final HiLogLabel logLabel=new HiLogLabel(HiLog.LOG_APP,0x001010,"视频测试");
private static Player player;
private SurfaceProvider sfProvider;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_vedio_layout);
player=new Player(getContext());
sfProvider=new SurfaceProvider(getContext());
sfProvider.getSurfaceOps().get().addCallback(new VideoSurfaceCallback());
sfProvider.pinToZTop(true);
DependentLayout dependentLayout=(DependentLayout)findComponentById(ResourceTable.Id_vediolayout);
if(dependentLayout!=null)
{
dependentLayout.addComponent(sfProvider);
}
}
private void playLocalFile(Surface surface) {
try {
RawFileDescriptor filDescriptor = getResourceManager().getRawFileEntry("resources/rawfile/index1.mp4").openRawFileDescriptor();
config.json
"abilities": [
{
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home",
"action.vedio_layout"
]
}
],
"orientation": "landscape",
"visible" : true,
"name": "com.example.jltflianxi.MainAbility",
"icon": "$media:icon",
"description": "$string:mainability_description",
"label": "jltflianxi",
"type": "page",
"launchType": "standard"
}
]
中
"actions": [
"action.system.home",
"action.vedio_layout"
]
添加
"action.vedio_layout"
Player
官方的一段案例
1. try (FileInputStream in = new FileInputStream(new File(PLAYER_TEST_FILE))) {
2. FileDescriptor fd = in.getFD();
3. Source source = new Source(fd);
4. Player impl = new Player();
5. impl.setSource(source);
6. impl.prepare();
7. impl.play();
8. } catch (IOException e) {
9. // ...
10. }
11.
我们用的是SurfaceProvider+ Player 完成的播放
pause() 暂停播放。
getCurrentTime() 获取当前播放位置。
getDuration() 获取媒体文件总时长。
setPlaybackSpeed(float speed) 设置播放速度。
getPlaybackSpeed() 获取播放速度。
总结:播放本地的视频功能开发已经完成,但是一些小插件没有完成,比如进度条,暂停,播放时长与分布式特征的各项实现等。
👍👍👍