鸿蒙 视频播放开发(基础文,适合新人) 原创
陈浩南xxx
发布于 2021-4-26 20:36
浏览
1收藏
官方指导地址:https://developer.harmonyos.com/cn/docs/documentation/doc-guides/media-video-player-0000000000044178
关键步骤 1 player初始化
player = new Player(getContext());
2,设置 Source; 视频播放的源(可以是本地,也可以是网络)
目前只研究到放文件在工程里
private String url = "entry/resources/base/media/gubeishuizhen.mp4";
RawFileDescriptor fd = null;
try {
fd = getContext().getResourceManager().getRawFileEntry(url).openRawFileDescriptor();
Source source = new Source(fd.getFileDescriptor(), fd.getStartPosition(), fd.getFileSize());
player.setSource(source);
} catch (IOException e) {
Log.e(TAG, "Exception %s", e.toString());
}
3,渲染到SurfaceProvider上
surfaceProvider = (SurfaceProvider) findComponentById(ResourceTable.Id_surface_provider);
surfaceProvider.pinToZTop(true);// 不设置这个 画面不显示;
surfaceProvider.getSurfaceOps().get().addCallback(new SurfaceOps.Callback() {
@Override
public void surfaceCreated(SurfaceOps surfaceOps) {
Log.d(TAG, "surfaceCreated");
player.setVideoSurface(surfaceOps.getSurface());
player.prepare();
}
@Override
public void surfaceChanged(SurfaceOps holder, int format, int width, int height) {
Log.d(TAG, "surfaceChanged format:%d ;width:%d ;height:%d", format, width, height);
}
@Override
public void surfaceDestroyed(SurfaceOps surfaceOps) {
Log.d(TAG, "surfaceDestroyed");
}
});
4,player的各种监听回调;
player.setPlayerCallback(new Player.IPlayerCallback() {
@Override
public void onPrepared() {
Log.d(TAG, "onPrepared");
}
@Override
public void onMessage(int type, int extra) {
Log.d(TAG, "onMessage type:%d ;extra:%d", type, extra);
}
@Override
public void onError(int errorType, int errorCode) {
Log.d(TAG, "onError errorType:%d ;errorCode:%d", errorType, errorCode);
}
@Override
public void onResolutionChanged(int width, int height) {
Log.d(TAG, "onResolutionChanged width:%d ;height:%d", width, height);
}
@Override
public void onPlayBackComplete() {
Log.d(TAG, "onPlayBackComplete");
}
@Override
public void onRewindToComplete() {
Log.d(TAG, "onRewindToComplete");
}
@Override
public void onBufferingChange(int percent) {
Log.d(TAG, "onBufferingChange percent:%d", percent);
}
@Override
public void onNewTimedMetaData(Player.MediaTimedMetaData mediaTimedMetaData) {
Log.d(TAG, "onNewTimedMetaData");
}
@Override
public void onMediaTimeIncontinuity(Player.MediaTimeInfo mediaTimeInfo) {
Log.d(TAG, "onMediaTimeIncontinuity");
}
});
5,播放,暂停等
@Override
public void onClick(Component component) {
switch (component.getId()) {
case ResourceTable.Id_play_btn:
if (player != null) {
player.play();
}
break;
case ResourceTable.Id_pause_btn:
if (player != null) {
player.pause();
}
break;
}
}
6,时间的更新,用的EventHandler
EventRunner runner = EventRunner.create(true);
timeHandler = new TimeHandler(runner);
sendTimeHandler();
private class TimeHandler extends EventHandler {
public TimeHandler(EventRunner runner) throws IllegalArgumentException {
super(runner);
}
@Override
protected void processEvent(InnerEvent event) {
super.processEvent(event);
if (event.eventId == 0) {
if (player != null && player.isNowPlaying()) {
int duration = player.getDuration();
int currentTime = player.getCurrentTime();
getUITaskDispatcher().syncDispatch(new Runnable() {
@Override
public void run() {
progressBar.setProgressValue(currentTime);
progressBar.setMaxValue(duration);
durationText.setText(DateUtils.msToString(duration));
currentTimeText.setText(DateUtils.msToString(currentTime));
}
});
}
}
sendTimeHandler();
}
}
private void sendTimeHandler() {
timeHandler.sendEvent(
0, 1000, EventHandler.Priority.IMMEDIATE);
}
©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
标签
已于2021-4-27 09:11:10修改
赞
6
收藏 1
回复
相关推荐
赞赞赞!
源码可以发下吗
上面有官方文档,还有就是这么简单的还要源码;那无能为力;
那个视频进度的时间是怎么显示的
文章第6段代码说的就是时间如何获取,如何定时,怎么显示在ui上