Как я могу прочитать функцию javascript в ASP.NET Ядро Blazor и камера

#javascript #asp.net-core #webrtc #blazor

#javascript #asp.net-core #webrtc #блейзор #blazor

Вопрос:

Как я могу прочитать функцию javascript в ASP.NET Core Blazor. Я пытаюсь прочитать скрипт из javascript, чтобы показать изображение с камеры. Я знаю, что могу использовать API или использовать Angular, но я хочу упростить

Я добавляю больше файлов, которые я создаю

  @page "/"
@inject IJSRuntime IJR

<h1>Hello, world!</h1>
<div id="container">
    <div class="select">
        <label for="audioSource">Audio input source: </label><select id="audioSource"></select>
    </div>

    <div class="select">
        <label for="audioOutput">Audio output destination: </label><select id="audioOutput"></select>
    </div>

    <div class="select">
        <label for="videoSource">Video source: </label><select id="videoSource"></select>
    </div>

    <video id="video" playsinline autoplay></video>
    <video id="@audo" playsinline autoplay></video>

</div>

@code
{


    public object audo;

    protected override void OnInitialized()
    {
        
        audo = IJR.InvokeAsync<Object>("test.start");

    }

}
  

Это javascript

 'use strict';

const videoElement = document.querySelector('video');
const audioInputSelect = document.querySelector('select#audioSource');
const audioOutputSelect = document.querySelector('select#audioOutput');
const videoSelect = document.querySelector('select#videoSource');
const selectors = [audioInputSelect, audioOutputSelect, videoSelect];

audioOutputSelect.disabled = !('sinkId' in HTMLMediaElement.prototype);

function gotDevices(deviceInfos) {
  // Handles being called several times to update labels. Preserve values.
  const values = selectors.map(select => select.value);
  selectors.forEach(select => {
    while (select.firstChild) {
      select.removeChild(select.firstChild);
    }
  });
  for (let i = 0; i !== deviceInfos.length;   i) {
    const deviceInfo = deviceInfos[i];
    const option = document.createElement('option');
    option.value = deviceInfo.deviceId;
    if (deviceInfo.kind === 'audioinput') {
      option.text = deviceInfo.label || `microphone ${audioInputSelect.length   1}`;
      audioInputSelect.appendChild(option);
    } else if (deviceInfo.kind === 'audiooutput') {
      option.text = deviceInfo.label || `speaker ${audioOutputSelect.length   1}`;
      audioOutputSelect.appendChild(option);
    } else if (deviceInfo.kind === 'videoinput') {
      option.text = deviceInfo.label || `camera ${videoSelect.length   1}`;
      videoSelect.appendChild(option);
    } else {
      console.log('Some other kind of source/device: ', deviceInfo);
    }
  }
  selectors.forEach((select, selectorIndex) => {
    if (Array.prototype.slice.call(select.childNodes).some(n => n.value === values[selectorIndex])) {
      select.value = values[selectorIndex];
    }
  });
}

navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(handleError);

// Attach audio output device to video element using device/sink ID.
function attachSinkId(element, sinkId) {
  if (typeof element.sinkId !== 'undefined') {
    element.setSinkId(sinkId)
        .then(() => {
          console.log(`Success, audio output device attached: ${sinkId}`);
        })
        .catch(error => {
          let errorMessage = error;
          if (error.name === 'SecurityError') {
            errorMessage = `You need to use HTTPS for selecting audio output device: ${error}`;
          }
          console.error(errorMessage);
          // Jump back to first output device in the list as it's the default.
          audioOutputSelect.selectedIndex = 0;
        });
  } else {
    console.warn('Browser does not support output device selection.');
  }
}

function changeAudioDestination() {
  const audioDestination = audioOutputSelect.value;
  attachSinkId(videoElement, audioDestination);
}

function gotStream(stream) {
  window.stream = stream; // make stream available to console
  videoElement.srcObject = stream;
  // Refresh button list in case labels have become available
  return navigator.mediaDevices.enumerateDevices();
}

function handleError(error) {
  console.log('navigator.MediaDevices.getUserMedia error: ', error.message, error.name);
}

windows.test = {
    start:function{
  if (window.stream) {
    window.stream.getTracks().forEach(track => {
      track.stop();
    });
  }
  const audioSource = audioInputSelect.value;
  const videoSource = videoSelect.value;
  const constraints = {
    audio: {deviceId: audioSource ? {exact: audioSource} : undefined},
    video: {deviceId: videoSource ? {exact: videoSource} : undefined}
  };
  navigator.mediaDevices.getUserMedia(constraints).then(gotStream).then(gotDevices).catch(handleError);
}
};
audioInputSelect.onchange = start;
audioOutputSelect.onchange = changeAudioDestination;

videoSelect.onchange = start;

start();
  

Что не так, что он показывает какие-либо данные?

Это файл, который использует WebRTC, и я хочу, чтобы мы использовали его для даты захвата

Комментарии:

1. Вы получаете сообщение об ошибке? Кроме того, где у вас определена функция javascript?

Ответ №1:

Здесь я публикую рабочий код.

Серверное приложение Blazor

_Host.cshtml

 @page "/"
@namespace WebRtcBlazorApp.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = null;
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>WebRtcBlazorApp</title>
    <base href="~/" />
    <link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
    <link href="css/site.css" rel="stylesheet" />
</head>
<body>
    <app>
        <component type="typeof(App)" render-mode="ServerPrerendered" />
    </app>

    <div id="blazor-error-ui">
        <environment include="Staging,Production">
            An error has occurred. This application may no longer respond until reloaded.
        </environment>
        <environment include="Development">
            An unhandled exception has occurred. See browser dev tools for details.
        </environment>
        <a href="" class="reload">Reload</a>
        <a class="dismiss">🗙</a>
    </div>

    <script src="_framework/blazor.server.js"></script>

    <script src="~/js/JsFunctionText.js"></script>
    <script src="~/js/WebRtc.js"></script>
</body>
</html>
  

Index.razor

 @page "/"

<h1 id="myHeader">Welcome To WebRTC Blazor</h1>

<div id="container">
    <div class="select">
        <label for="audioSource">Audio input source: </label><select id="audioSource"></select>
    </div>

    <div class="select">
        <label for="audioOutput">Audio output destination: </label><select id="audioOutput"></select>
    </div>

    <div class="select">
        <label for="videoSource">Video source: </label><select id="videoSource"></select>
    </div>

    <video id="video" playsinline autoplay></video>
</div>


@code{

    [Inject]
    IJSRuntime JSRuntime { get; set; }

    protected async override Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await JSRuntime.InvokeVoidAsync("test.start");

            StateHasChanged();
        }
    }
}
  

WebRtc.js («wwwroot/js/WebRtc.js «)

 'use strict'


let videoElement;
let audioInputSelect;
let audioOutputSelect;
let videoSelect;
let selectors;
let dummySelect;

function pageReady() {
    videoElement = document.querySelector('video');
    audioInputSelect = document.querySelector('select#audioSource');
    audioOutputSelect = document.querySelector('select#audioOutput');
    videoSelect = document.querySelector('select#videoSource');
    selectors = [audioInputSelect, audioOutputSelect, videoSelect];

    dummySelect = document.querySelector('select#dummy');

    audioOutputSelect.disabled = !('sinkId' in HTMLMediaElement.prototype);

    audioInputSelect.onchange = window.test.start;
    audioOutputSelect.onchange = changeAudioDestination;

    videoSelect.onchange = window.test.start;
}

function gotDevices(deviceInfos) {
    // Handles being called several times to update labels. Preserve values.
    const values = selectors.map(select => select.value);
    selectors.forEach(select => {
        while (select.firstChild) {
            select.removeChild(select.firstChild);
        }
    });
    for (let i = 0; i !== deviceInfos.length;   i) {
        const deviceInfo = deviceInfos[i];
        const option = document.createElement('option');
        option.value = deviceInfo.deviceId;
        if (deviceInfo.kind === 'audioinput') {
            option.text = deviceInfo.label || `microphone ${audioInputSelect.length   1}`;
            audioInputSelect.appendChild(option);
        } else if (deviceInfo.kind === 'audiooutput') {
            option.text = deviceInfo.label || `speaker ${audioOutputSelect.length   1}`;
            audioOutputSelect.appendChild(option);
        } else if (deviceInfo.kind === 'videoinput') {
            option.text = deviceInfo.label || `camera ${videoSelect.length   1}`;
            videoSelect.appendChild(option);
        } else {
            console.log('Some other kind of source/device: ', deviceInfo);
        }
    }
    selectors.forEach((select, selectorIndex) => {
        if (Array.prototype.slice.call(select.childNodes).some(n => n.value === values[selectorIndex])) {
            select.value = values[selectorIndex];
        }
    });
}

navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(handleError);

// Attach audio output device to video element using device/sink ID.
function attachSinkId(element, sinkId) {
    if (typeof element.sinkId !== 'undefined') {
        element.setSinkId(sinkId)
            .then(() => {
                console.log(`Success, audio output device attached: ${sinkId}`);
            })
            .catch(error => {
                let errorMessage = error;
                if (error.name === 'SecurityError') {
                    errorMessage = `You need to use HTTPS for selecting audio output device: ${error}`;
                }
                console.error(errorMessage);
                // Jump back to first output device in the list as it's the default.
                audioOutputSelect.selectedIndex = 0;
            });
    } else {
        console.warn('Browser does not support output device selection.');
    }
}

function changeAudioDestination() {
    const audioDestination = audioOutputSelect.value;
    attachSinkId(videoElement, audioDestination);
}

function gotStream(stream) {
    window.stream = stream; // make stream available to console
    videoElement.srcObject = stream;
    // Refresh button list in case labels have become available
    return navigator.mediaDevices.enumerateDevices();
}

function handleError(error) {
    console.log('navigator.MediaDevices.getUserMedia error: ', error.message, error.name);
}

window.test = {
    start: function () {

        pageReady();

        if (window.stream) {
            window.stream.getTracks().forEach(track => {
                track.stop();
            });
        }
        const audioSource = audioInputSelect.value;
        const videoSource = videoSelect.value;
        const constraints = {
            audio: { deviceId: audioSource ? { exact: audioSource } : undefined },
            video: { deviceId: videoSource ? { exact: videoSource } : undefined }
        };

        
        navigator.mediaDevices.getUserMedia(constraints).then(gotStream).then(gotDevices).catch(handleError);
    }
};
  

Снимок экрана прилагается

Ответ №2:

JsInterop в Blazor требует, чтобы функции javascript были объявлены в глобальной области видимости ( window ). Чтобы это сработало, вы должны переместить свой JS в его собственный файл в веб-корне (например, myJsFunctions.js ) и объявить функцию на window :

 window.myJsFunctions = {
  start: function() {
    if (window.stream) {
      window.stream.getTracks().forEach(track => {
        track.stop();
      });
    }
    const audioSource = audioInputSelect.value;
    const videoSource = videoSelect.value;
    const constraints = {
      audio: {deviceId: audioSource ? {exact: audioSource} : undefined},
      video: {deviceId: videoSource ? {exact: videoSource} : undefined}
    };
    navigator.mediaDevices.getUserMedia(constraints).then(gotStream).then(gotDevices).catch(handleError);
  }
};
  

А затем загрузить этот скрипт в точку входа внутри элемента wwwroot/index.html (Blazor WebAssembly) или Pages/_Host.cshtml` (сервер Blazor):

 <script src="myJsFunctions.js"></script>
  

А затем укажите полный путь к функции при вызове в вашем компоненте:

 audo = await JSRuntime.InvokeAsync<Object>("myJsFunctions.start");
  

Кроме того, ваша start функция, похоже, ничего не возвращает. Если это так, вам следует вызвать InvokeVoidAsync :

 await JSRuntime.InvokeVoidAsync("myJsFunctions.start");
  

Подробнее смотрите в документах.

Комментарии:

1. это не работает Microsoft. JsInterop.JSException: не удалось найти ‘main’ в ‘window’. Ошибка: не удалось найти ‘main’ в ‘window’. вот в чем у меня проблема

2. Я не уверен, к чему относится эта ошибка. Вам придется предоставить больше контекста, показав больше имеющегося у вас кода, который выдает вам эту ошибку

3. Я пытаюсь добавить окно в javascript, но оно не работает