您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
15. 完成通过界面多次打开视频文件,修正了多次打开显示出现的bug~1
发布时间:2021-06-14 10:40:29编辑:雪饮阅读()
爲了處理視頻多次打開時候由於多綫程導致會出現沒有規律的打不開的錯誤,所以要在cpp/GLVideoView.cpp處理下綫程鎖問題:
#include "GLVideoView.h"
#include "XTexture.h"
#include "XLog.h"
void GLVideoView::SetRender(void *win)
{
view = win;
}
void GLVideoView::Close()
{
mux.lock();
if(txt)
{
txt->Drop();
txt = 0;
}
mux.unlock();
}
void GLVideoView::Render(XData data)
{
if(!view) return;
if(!txt)
{
txt = XTexture::Create();
txt->Init(view,(XTextureType)data.format);
}
txt->Draw(data.datas,data.width,data.height);
}
#include "XTexture.h"
#include "XLog.h"
void GLVideoView::SetRender(void *win)
{
view = win;
}
void GLVideoView::Close()
{
mux.lock();
if(txt)
{
txt->Drop();
txt = 0;
}
mux.unlock();
}
void GLVideoView::Render(XData data)
{
if(!view) return;
if(!txt)
{
txt = XTexture::Create();
txt->Init(view,(XTextureType)data.format);
}
txt->Draw(data.datas,data.width,data.height);
}
然後cpp/native-lib.cpp中要提供一個open方法,供openurl界面調用:
#include <jni.h>
#include <string>
#include <android/native_window_jni.h>
#include "XLog.h"
#include "IPlayerPorxy.h"
extern "C"
JNIEXPORT
jint JNI_OnLoad(JavaVM *vm,void *res)
{
IPlayerPorxy::Get()->Init(vm);
/*IPlayerPorxy::Get()->Open("/sdcard/v1080.mp4");
IPlayerPorxy::Get()->Start();
IPlayerPorxy::Get()->Open("/sdcard/1080.mp4");
IPlayerPorxy::Get()->Start();*/
return JNI_VERSION_1_4;
}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_xplay_XPlay_InitView(JNIEnv *env, jobject instance, jobject surface) {
// TODO
ANativeWindow *win = ANativeWindow_fromSurface(env,surface);
IPlayerPorxy::Get()->InitView(win);
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_xplay_MainActivity_stringFromJNI(JNIEnv *env, jobject thiz) {
// TODO: implement stringFromJNI()
}extern "C"
JNIEXPORT void JNICALL
Java_com_example_xplay_OpenUrl_Open(JNIEnv *env, jobject thiz, jstring url_) {
const char *url = env->GetStringUTFChars(url_, 0);
IPlayerPorxy::Get()->Open(url);
IPlayerPorxy::Get()->Start();
env->ReleaseStringUTFChars(url_, url);
}
作爲OpenUrl.java則要提供調用上面open事件的編寫:
package com.example.xplay;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
/**
* Created by Administrator on 2018-03-11.
*/
public class OpenUrl extends AppCompatActivity {
private Button btfile;
private Button btrtmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.openurl );
btfile = findViewById( R.id.playvideo );
btfile.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText t = findViewById( R.id.fileurl );
//用户输入的URL,打开视频
Open(t.getText().toString());
//关闭当前窗口
finish();
}
}
);
}
public native void Open(String url);
}
那最後的效果大致如:
关键字词:bug