无法在 Android 上显示实时流数据

2024-03-01

我正在尝试将原始 H264 数据从相机设备显示到我的 Android 应用程序。我能够在 Textview 上接收数据,但无法在 Textureview 上显示它。我是 android 的初学者,我不是解码原始数据的专家。如果有人能提出解决方案,我们将不胜感激。请找到以下代码:

获取数据的代码

    public class myVideoReceiver extends Thread {
    public boolean bKeepRunning2 = true;
    public String lastMessage2 = "";

    public void run() {
        String message2;
        byte[] lmessage2 = new byte[MAX_UDP_DATAGRAM_LEN2];
        DatagramPacket packet2 = new DatagramPacket(lmessage2, lmessage2.length);

        try {
            DatagramSocket socket2 = new DatagramSocket(UDP_SERVER_PORT2);

            while(bKeepRunning2) {
                socket2.receive(packet2);
                message2 = new String(lmessage2, 0, packet2.getLength());
                lastMessage2 = message2;
                runOnUiThread(updateTextMessage2);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }

        if (mysocket != null) {
            mysocket.close();
        }
    }

    public void kill() {
        bKeepRunning2 = false;
    }

    public String getLastMessage() {
        return lastMessage2;

    }  }
   //Added release function
    public void release() {
    }

    public Runnable updateTextMessage2 = new Runnable() {
    public void run() {
        if (myVideoReceiver == null) return;
        VIDEO_RESPONSE.setText(myVideoReceiver.getLastMessage());

    }
};

在纹理视图上显示原始数据的代码:

public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener{

private TextureView m_surface;// View that contains the Surface Texture

private myVideoReceiver provider;// Object that connects to our server and gets H264 frames

private MediaCodec m_codec;// Media decoder

private DecodeFramesTask m_frameTask;// AsyncTask that takes H264 frames and uses the decoder to update the Surface Texture

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Get a referance to the TextureView in the UI
    m_surface = (TextureView)findViewById(R.id.textureView);

    // Add this class as a call back so we can catch the events from the Surface Texture
    m_surface.setSurfaceTextureListener(this);
}

@Override
// Invoked when a TextureView's SurfaceTexture is ready for use
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        // when the surface is ready, we make a H264 provider Object. When its constructor runs it starts an AsyncTask to log into our server and start getting frames
        provider = new myVideoReceiver();

        // Create the format settinsg for the MediaCodec
        MediaFormat format = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, 1920, 1080);// MIMETYPE: a two-part identifier for file formats and format contents
        // Set the PPS and SPS frame
        format.setByteBuffer("csd-0", ByteBuffer.wrap(provider.lastMessage2.getBytes()));
        // Set the buffer size
        format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 100000);

        try {
            // Get an instance of MediaCodec and give it its Mime type
            m_codec = MediaCodec.createDecoderByType(MediaFormat.MIMETYPE_VIDEO_AVC);
            // Configure the Codec
            m_codec.configure(format, new Surface(m_surface.getSurfaceTexture()), null, 0);
            // Start the codec
            m_codec.start();
            // Create the AsyncTask to get the frames and decode them using the Codec
            m_frameTask = new DecodeFramesTask();
            m_frameTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }catch(Exception e){
            e.printStackTrace();
        }
}

@Override
// Invoked when the SurfaceTexture's buffers size changed
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}

@Override
// Invoked when the specified SurfaceTexture is about to be destroyed
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    return false;
}

@Override
// Invoked when the specified SurfaceTexture is updated through updateTexImage()
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}

private class DecodeFramesTask extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... data) {
        while(!isCancelled()) {
            // Get the next frame
            //byte[] frame = provider.nextFrame();
            //New code
            byte[] frame = provider.lastMessage2.getBytes();
            Log.e("Frame", "Value in frame data : "+frame);
            Log.e("Frame length","Frame length"+frame.length);
            // For getting the 'frame.length' 
            for(int i = 0; i < 10 && i < frame.length; i++) {
                Log.e("Framelength","Frame length"+frame.length);
            }

            // Now we need to give it to the Codec to decode into the surface

            // Get the input buffer from the decoder
            int inputIndex = m_codec.dequeueInputBuffer(-1);// Pass in -1 here as in this example we don't have a playback time reference
            Log.e("InputIndex","Value in Input index : "+inputIndex);
            // If  the buffer number is valid use the buffer with that index
            if(inputIndex>=0) {
                ByteBuffer buffer = null;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                    buffer = m_codec.getInputBuffer(inputIndex);

                    Log.e("MycustomData","Value in Buffer :"+buffer);
                    Log.e("If InputIndex","if Input index : "+inputIndex);
                }
                buffer.put(frame);
                // tell the decoder to process the frame
                m_codec.queueInputBuffer(inputIndex, 0, frame.length, 0, 0);
            }

            MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
            int outputIndex = m_codec.dequeueOutputBuffer(info, 0);
            Log.e("MycustomData","value in outputIndex: "+outputIndex);
            if (outputIndex >= 0) {
                Log.e("Inside if outputindex","value : "+outputIndex);
                m_codec.releaseOutputBuffer(outputIndex, true);
            }

            // wait for the next frame to be ready, our server makes a frame every 250ms
            try{Thread.sleep(250);}catch (Exception e){e.printStackTrace();}
        }
        return "";
    }

    @Override
    protected void onPostExecute(String result) {
        try {
            m_codec.stop();
            m_codec.release();
        }catch(Exception e){
            e.printStackTrace();
        }
        provider.release();
    }

}

@Override
public void onStop(){
    super.onStop();
    m_frameTask.cancel(true);
    provider.release();
}
}

提前致谢

请查找以下错误日志:

E/Frame: Value in frame data : [B@aa987a7
E/InputIndex: Value in Input index : 0
E/MycustomData: Value in Buffer :java.nio.DirectByteBuffer[pos=0 lim=4194304 cap=4194304]
E/If InputIndex: if Input index : 0
E/MycustomData: value in outputIndex: -1
I/ViewRootImpl: jank_removeInvalidNode all the node in jank list is out of time
W/InputMethodManager: startInputReason = 1
W/libEGL: EGLNativeWindowType 0x7c452aa010 disconnect failed
D/ViewRootImpl[Page_01]: surface should not be released
E/Frame: Value in frame data : [B@5dcf743
E/InputIndex: Value in Input index : 1
E/MycustomData: Value in Buffer :java.nio.DirectByteBuffer[pos=0 lim=4194304 cap=4194304]
E/If InputIndex: if Input index : 1
E/MycustomData: value in outputIndex: -1
E/Frame: Value in frame data : [B@33de1c0
E/InputIndex: Value in Input index : 2
E/MycustomData: Value in Buffer :java.nio.DirectByteBuffer[pos=0 lim=4194304 cap=4194304]
E/If InputIndex: if Input index : 2
E/MycustomData: value in outputIndex: -1
E/Frame: Value in frame data : [B@6a1cef9
E/InputIndex: Value in Input index : 3
E/MycustomData: Value in Buffer :java.nio.DirectByteBuffer[pos=0 lim=4194304 cap=4194304]
E/If InputIndex: if Input index : 3
E/MycustomData: value in outputIndex: -1
E/Frame: Value in frame data : [B@d8b2e3e
E/InputIndex: Value in Input index : 4
E/MycustomData: Value in Buffer :java.nio.DirectByteBuffer[pos=0 lim=4194304 cap=4194304]
E/If InputIndex: if Input index : 4
E/MycustomData: value in outputIndex: -1
E/Frame: Value in frame data : [B@e4dc49f
E/InputIndex: Value in Input index : 0
E/MycustomData: Value in Buffer :java.nio.DirectByteBuffer[pos=0 lim=4194304 cap=4194304]
E/If InputIndex: if Input index : 0
E/MycustomData: value in outputIndex: -1
E/Frame: Value in frame data : [B@aff59ec
E/InputIndex: Value in Input index : 1
E/MycustomData: Value in Buffer :java.nio.DirectByteBuffer[pos=0 lim=4194304 cap=4194304]
E/If InputIndex: if Input index : 1
E/MycustomData: value in outputIndex: -1
E/Frame: Value in frame data : [B@4a761b5
E/InputIndex: Value in Input index : 2
E/MycustomData: Value in Buffer :java.nio.DirectByteBuffer[pos=0 lim=4194304 cap=4194304]
E/If InputIndex: if Input index : 2
E/MycustomData: value in outputIndex: -1
E/Frame: Value in frame data : [B@207f04a
E/InputIndex: Value in Input index : 3
E/MycustomData: Value in Buffer :java.nio.DirectByteBuffer[pos=0 lim=4194304 cap=4194304]
E/If InputIndex: if Input index : 3
E/MycustomData: value in outputIndex: -1
E/Frame: Value in frame data : [B@bd40bbb
E/InputIndex: Value in Input index : 4
E/MycustomData: Value in Buffer :java.nio.DirectByteBuffer[pos=0 lim=4194304 cap=4194304]
E/If InputIndex: if Input index : 4
E/MycustomData: value in outputIndex: -1

截屏:

Updated screenshot Srtreaming image


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法在 Android 上显示实时流数据 的相关文章

  • 浓缩咖啡测试失败

    我正在 Android 中进行一些 Espresso 测试 测试失败并出现以下错误 java lang ClassCastException androidx fragment app testing FragmentScenario Em
  • Signal R Native Android 应用程序协商失败

    我正在尝试创建一个可以连接到我的基本 SignalR Hub 的 Android 应用程序 它只是一个基本的集线器文件 我想用它来测试一些东西 但到目前为止我还没有运气 有人可以看看我做错的事情吗 每次我尝试运行它时 我都会收到以下堆栈竞赛
  • 使用root清除状态栏通知

    我目前正在开发一个应用程序 使用无障碍服务来处理通知 特别烦人的是第三方应用无法清除状态栏通知 除了启动链接到通知的意图 并启动应用程序 我寻找了很长时间的方法使用 root 关闭通知或清除完整列表 但我失败了 我想我记得我看到过一个应用程
  • 我在尝试连接 FTP 服务器时收到“无法连接到主机”Logcat 消息,我做错了什么?

    我正在 Android 上开发一个应用程序 它连接到 FTP 服务器来上传和下载文件 为了建立连接 我使用基于 apache commons net 库的 FTPClient 类this http androiddev orkitra co
  • 发生存储异常。无法在firebase中上传图片

    在我能够更改图像并将其上传到 firebase 之前 这段代码就可以工作 但现在我突然收到此错误 我不知道问题是什么 public class SettingsActivity extends AppCompatActivity priva
  • 在phonegap中播放本地声音

    我有一个 wav文件在我的www文件夹 我正在使用 jQuery 和以下代码 警报响起 但声音不播放 难道我做错了什么
  • fresco 的 Proguard 错误

    我正在使用 ProGuard 当我在发布配置中运行项目时 出现以下错误 Warning com facebook imagepipeline bitmaps DalvikBitmapFactory can t find referenced
  • 服务在后台运行?

    我正在构建的应用程序的功能之一是记录功能 我通过在服务中启动 MediaRecorder 对象来实现此目的 Intent intent new Intent v getContext RecordService class Messenge
  • 在屏幕上随机生成一个圆圈并将其设为绿色或红色

    所以我一直在尝试制作一个游戏应用程序 它可以在 Android 屏幕上随机显示带有文本的红色按钮或带有文本的绿色按钮 如果有人可以帮助我 我将不胜感激 另外 如果有人知道如何做到这一点 我想慢慢地产生更快的酷优势 谢谢 SuppressLi
  • 如何从静态快捷方式启动活动的现有实例

    我的应用程序中有一个活动 MainActivity 并且有一个静态快捷方式 指向 TempActivity 由于静态快捷方式将始终设置 FLAG ACTIVITY NEW TASK 和 FLAG ACTIVITY CLEAR TASK 因此
  • 从Asynctask返回结果

    如果我的 Android 应用程序中有这个后台工作文件 并且它从我的数据库获取数据 我如何将字符串 结果 传递给另一个类 后台工作人员连接到我的服务器 然后使用 php 连接到数据库 public class BackgroundWorke
  • 在 Android 中关闭 Spinner 中的下拉菜单

    在 Android 中打开和关闭微调器时 我需要为箭头图标设置动画 打开微调器时我可以旋转箭头 我只是放了一个setOnTouchListener on the Spinner 当下拉菜单关闭或隐藏时 问题就来了 因为我不知道如何在该操作上
  • android studio 底部工具栏的“运行”选项卡消失了

    Android Studio 底部工具栏中曾经有一个 运行 选项卡 但该选项卡不再显示 怎么把它带回来 请检查下图以了解它消失之前的位置 Run 选项卡曾经位于 TODO 选项卡之前的红色圆圈中 查看 gt 工具窗口 gt 运行 Or us
  • BluetoothLeScanner 服务内部问题

    Update从Android 10以上我认为你需要ACCESS BACKGROUND LOCATION权限 因此 如果此代码在最新的 Android 版本上不起作用 就是针对此问题的 ACCESS BACKGROUND LOCATION 受
  • 如何使用 onSearchRequested() 调用搜索对话框

    我正在尝试实现搜索对话框 但无法显示活动中的搜索 我在清单文件中定义了主要活动 此活动向用户显示了他们必须从中选择的选项列表 选项之一是 搜索 选项
  • UnsupportedOperationException:特权进程中不允许使用 WebView

    我在用android sharedUserId android uid system 在我的清单中获得一些不可避免的权利 从 HDMI 输入读取安卓盒子 http eweat manufacturer globalsources com s
  • EditText 的高度不会扩展到其父级的高度

    我在滚动视图中放置了编辑文本 高度 match parent并期望它的高度等于滚动视图 但事实并非如此 它的高度就像wrap content这意味着如果 EditText 中没有文本 我必须将光标指向要弹出的软键盘的第一 行 我想要的是我可
  • 协程和 Firebase:如何实现类似 Javascript 的 Promise.all()

    在 Javascript 中 您可以同时启动两个 或更多 异步任务 等待它们完成 然后执行某些操作 继续 const firstReturn secondReturn await Promise all firstPromise secon
  • 使用 eclipse 配置mockito 时出现问题。给出错误:java.lang.verifyError

    当我将我的mockito库添加到类路径中 并使用一个简单的mockito示例进行测试时 我尝试使用模拟对象为函数add返回错误的值 我得到java lang verifyerror 以下是用于测试的代码 后面是 logcat Test pu
  • Android 视图和视图组

    在安卓中ViewGroup继承自View A ViewGroup是一个容器 里面装有Views ViewGroup LinearLayout View TextView 为什么 Android 的人们将这种关系定义为Inheritance而

随机推荐

  • Powershell 中非常大的 XML 文件

    对于非常大的文本文件 我们可以选择使用 StreamReader 和 StreamWriter 然后允许逐行查找 替换 但是 我有一个 XML 文件 需要在其中进行查找 替换并进行更多控制 例如查找 替换特定节点中的值 该节点是具有特定属性
  • Git - 如何自动将目录中的更改推送到另一个分支

    完成问题重写 所以我以为我正在非常简单和直接地解释这个问题 但似乎我过于简单化了 所以这里是所有额外的细节 希望这可以帮助每个人看到这也不是重复的 我有一个存储库 项目 我想在其中自动化将提交从一个分支中的一个目录推送到另一个分支的过程 我
  • 使用“:”(冒号)按属性选择元素

    在我的项目中 有一个库生成元素的情况 我需要从那里选择特定的元素 它恰好包含带有 的属性 换句话说 我最终尝试选择使用 document querySelectorAll xml space 但是 在 Chrome 中测试时 它不起作用 也
  • 无法在 Xcode 10 中查找屏幕比例和意外的物理屏幕方向

    我最近将 Xcode 更新到版本 10 现在我的控制台显示 MyApp 1618 133310 AXMediaCommon Unable to look up screen scale MyApp 1618 133310 AXMediaCo
  • 点击 pageControl 滚动到另一个视图(点击点)

    我已经设置了具有 2 个视图的 PageViewControll 我可以在视图之间移动 并且 pageControl 点 对应于正确的页面 但是点击点还不能滚动到正确的视图 我在这里找到了一些关于如何创建该函数的答案 但无法成功实现以使其工
  • 带有自定义操作的 installshield

    我正在使用 installshield 2012 prime 创建一个基本的 msi 项目 我有两个问题 1 MSI 在创建要安装的应用程序的文件夹和文件后需要运行自定义 exe 只需在安装应用程序时执行一次此操作 2 MSI 还需要告诉正
  • iOS MobileVLCKit 存档问题

    在尝试归档我的项目时 我得到了这个error ld bitcode bundle could not be generated because Users MobileVLCKit MobileVLCKit framework Mobile
  • 如何将 Apigility 与现有 ZF2 应用程序结合使用?

    我有一个带有一些模块的 ZF2 应用程序 我希望允许在我的应用程序中使用具有 Apigility 的现有模块 我尝试使用 Composer 安装这些模块 require php gt 5 3 3 phpoffice phpexcel mon
  • 为映射和/或嵌套对象自定义 Spring @RequestParam 反序列化

    RestController class MyController RequestMapping public void test Container container Spring 默认使用 Dot Notation 来反序列化嵌套的
  • 为什么 Google+ 登录完成登录时出错?

    我最近一直在处理 Android 上的 Google 登录问题 有一件事一直困扰着我 在他们所有官方认可的示例中 没有一个方法专门显示登录过程 每次尝试让某人登录时调用的方法称为resolveSignInError 如下所示 private
  • 溢出与 Inf

    当我输入一个大于 max 的数字时double https en wikipedia org wiki Double precision floating point format在 Matlab 中大约是1 79769e 308 例如10
  • NSOutlineView拖线卡住+蓝色边框

    我想要正确的行为蓝色拖动条 and 没有蓝色矩形拖动时 你知道我的错误在哪里吗 如您所见 蓝色条卡在顶部 就像本主题中一样 使用拖放重新排列时 小圆线条卡在 NSOutlineView 顶部 https stackoverflow com
  • 如何在 playwright-java 中切换到新选项卡或窗口?

    我们如何切换到运行测试时打开的新窗口 以及如何返回到 playwright java 中的父窗口 没有像 Selenium 这样的 Switch 操作 您可以使用waitForPage or waitForPopup功能 您只需要知道触发该
  • C 迭代结构体数组

    说我已经声明了一个结构 struct mystruct char a 10 double b struct mystruct array 20 test1 1 0 test2 2 0 lt I just want to declare 2
  • XML 标签、属性及其定义

    我正在寻找一个包含所有 XML 标签及其属性以及这些属性的定义 即它们影响 执行的操作 的列表的地方 我认为 MSDN W3C 甚至 Stack Overflow 上都会有这个 但我在这两个地方以及其他地方都找不到它 可能我在这些网站上查找
  • R 中的波形点 (~.)

    谁能解释一下 R 中的波形点 我已经看过一些关于它的帖子 我知道波形符用于公式 指定自变量和因变量 而且 我知道点用于指示所有其他变量 更具体地说 有人可以解释这个例子中的波形点吗 x lt sample 10 x gt detect gt
  • 链接 PHP 文本

    我正在使用 TinySong api 生成链接 它可以工作 现在我尝试使用 linkify 来生成链接 事实并非如此 我不确定为什么它没有链接 我相信我使用了正确的变量 这是代码
  • Java 中的 Perlin 噪声

    对于我正在从事的元胞自动机项目 我需要使用不同的算法和技术随机生成二维布尔数组 目前 我在应用程序中只有一种类型的随机化 循环遍历数组中的每个单元格并生成随机双变量 然后如果随机数高于 0 5 则将该单元格设置为 true 如果不是 则设置
  • 未定义的子例程 &main::首先在 hello.pl 第 6 行调用

    我的 Perl 代码面临一个问题 我创建了一个包 Welcome pm 并在脚本 hello pl 中使用它 但出现以下错误 未定义子例程 main First 在 hello pl 第 6 行调用 我也查看了其他答案 但仍然无法弄清楚代码
  • 无法在 Android 上显示实时流数据

    我正在尝试将原始 H264 数据从相机设备显示到我的 Android 应用程序 我能够在 Textview 上接收数据 但无法在 Textureview 上显示它 我是 android 的初学者 我不是解码原始数据的专家 如果有人能提出解决