mediaCapture在后台任务WP 8.1中工作

时间:2016-01-18 14:28:16

标签: camera windows-phone-8.1 background-task

面对后台任务中初始化mediaCapture的问题。 API说mediaCapture在后台不支持,但是我已经看到了几个实现这个功能的WP 8.1应用程序,只是对冻结屏幕感兴趣。 以下是运行应用程序时运行良好的代码

public async Task Initialize()
{
    var cameraId = await GetCameraId(Panel.Back);
    if(cameraId != null)
    {
        var settings = new MediaCaptureInitializationSettings
        {
            VideoDeviceId = cameraId.Id
        };

        if(this.mediaCapture == null)
        {
            this.mediaCapture = new MediaCapture();
        }

        try
        {
            await this.mediaCapture.InitializeAsync(settings);
        }
        catch(Exception) { }
        this.torchControl = this.mediaCapture?.VideoDeviceController?.TorchControl;
        this.IsSupported = this.torchControl != null && this.torchControl.Supported;
    }
}

public void TurnOn(float powerPercent)
{
    if(this.torchControl != null)
    {
        if(this.torchControl.PowerSupported)
        {
            this.torchControl.PowerPercent = powerPercent;
        }

        this.torchControl.Enabled = true;
        SettingsProvider.Instance.IsFlashlightOn = this.torchControl.Enabled;
    }
}
private static async Task<DeviceInformation> GetCameraId(Panel desiredCamera)
{
    var deviceId = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
    return deviceId?.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desiredCamera);
}
public void ClearMediaCapture()
{
    if(this.torchControl.Enabled)
    {
        this.TurnOff();
    }

    this.mediaCapture?.Dispose();
    this.mediaCapture = null;
}

寻找实施或文章的示例。提前谢谢。

0 个答案:

没有答案