VLCException:无法在本机端执行实例化

问题描述 投票:0回答:1

VLCException:在本机端执行实例化失败。确保您在特定于平台的项目中安装了正确的 VideoLAN.LibVLC.[YourPlatform] 包 LibVLCSharp.Shared.Internal.OnNativeInstanciationError ()(位于:0) LibVLCSharp.Shared.Internal..ctor(System.Func

1[TResult] create, System.Action
1[T] 版本)(位于:0) LibVLCSharp.Shared.LibVLC..ctor(System.String[]选项)(位于:0) CameraLogic.Start ()(位于 Assets/Scripts/Cameras/CameraLogic.cs:28)

我试图在一个unity项目上做到这一点,我导入了libvlc、libvlccore、libvlcsharp的所有dll,但它返回给我这个错误,我不知道如何解决它(没有安装vlc-unity资产).

我需要这个来为相机接口运行 rtsp 服务器。这是我的代码:

using System;
using System.Runtime.InteropServices;
using UnityEngine;
using LibVLCSharp.Shared;

public class CameraLogic : MonoBehaviour
{
    public Material screenMat;
    public GameObject screen;
    public float changeSpeed = 1.0f; // Velocidad de cambio de textura (segundos por cambio)
    public bool hasSignal;
    public Texture2D[] noSignalFrames;
    private float timer = 0.0f;
    private int currentIndex = 0;
    public int id = 0;
    public string nombre = "No Signal";
    public string cameraID;
    public Texture2D cameraFrame;

    private Material selfMat;
    private LibVLC _libVLC;
    private MediaPlayer _mediaPlayer;
    private GCHandle _gcHandle;

    private void Start()
    { 
        // Opciones para LibVLC
        _libVLC = new LibVLC();
        _mediaPlayer = new MediaPlayer(_libVLC);

        // Inicializar la textura de la cámara
        cameraFrame = new Texture2D(640, 480, TextureFormat.RGBA32, false);
        selfMat = new Material(screenMat);

        // Asignar el material al objeto de la pantalla
        if (screen != null)
        {
            screen.GetComponent<Renderer>().material = selfMat;
        }
        else
        {
            Debug.LogError("El objeto 'screen' no está asignado.");
        }

        // Configurar VLC para renderizar en la textura de Unity
        _mediaPlayer.SetVideoFormat("RV32", (uint)cameraFrame.width, (uint)cameraFrame.height, (uint)(cameraFrame.width * 4));
        _gcHandle = GCHandle.Alloc(cameraFrame.GetPixels32(), GCHandleType.Pinned);

        _mediaPlayer.SetVideoCallbacks(
            (IntPtr opaque, IntPtr planes) =>
            {
                planes = _gcHandle.AddrOfPinnedObject();
                return IntPtr.Zero;
            },
            (IntPtr opaque, IntPtr picture, IntPtr planes) =>
            {
                // Puedes añadir código adicional si necesitas procesar algo al desbloquear
            },
            (IntPtr opaque, IntPtr picture) =>
            {
                if (cameraFrame != null)
                {
                    cameraFrame.Apply();
                }
                else
                {
                    Debug.LogError("cameraFrame es null.");
                }
            }
        );

        var media = new Media(_libVLC, "Here is the rtsp link");
        _mediaPlayer.Play(media);
    }

    void Update()
    {
        if (_mediaPlayer == null)
        {
            hasSignal = false;
            Debug.LogError("MediaPlayer no está inicializado.");
            return;
        }

        if (cameraFrame == null)
        {
            Debug.LogError("cameraFrame no está inicializado.");
            return;
        }

        if (!_mediaPlayer.IsPlaying)
        {
            hasSignal = false;
            timer += Time.deltaTime;

            if (timer >= changeSpeed)
            {
                ShowNoSignal();
                timer = 0.0f;
            }
        }
        else
        {
            hasSignal = true;
            PrintImage();
        }
        if (!hasSignal)
        {
            ShowNoSignal();
        }
    }

    void ShowNoSignal()
    {
        if (noSignalFrames.Length > 0)
        {
            selfMat.mainTexture = noSignalFrames[currentIndex];
            selfMat.SetTexture("_EmissionMap", noSignalFrames[currentIndex]);
            currentIndex = (currentIndex + 1) % noSignalFrames.Length;
        }
        else
        {
            Debug.LogError("No hay marcos de 'no signal' disponibles.");
        }
    }

    void PrintImage()
    {
        if (selfMat != null)
        {
            selfMat.mainTexture = cameraFrame;
            selfMat.SetTexture("_EmissionMap", cameraFrame);
        }
        else
        {
            Debug.LogError("selfMat no está inicializado.");
        }
    }

    private void OnDisable()
    {
        if (_gcHandle.IsAllocated)
        {
            _gcHandle.Free();
        }
        _mediaPlayer?.Stop();
        _mediaPlayer?.Dispose();
        _libVLC?.Dispose();
    }
}

unity-game-engine unityscript libvlc libvlcsharp
1个回答
0
投票

您需要 vlc-unity,因为它是允许您将 VLC 视频公开为纹理的软件包。

vlc-unity 资产还负责将库放在正确的位置,而您可能没有这样做。

© www.soinside.com 2019 - 2024. All rights reserved.