Raspberry Pi 作为录音机 Windows 10 物联网
Raspberry Pi as a Sound Recorder Win 10 iot
设备:Raspberry PI3
OS: WIN 10 物联网
编程语言:C#
我正在尝试编写一个 24/7 录音机,它会在 Raspberry 打开后立即记录所有内容。
音频文件长度应在 30 分钟后。
然后,这些文件将按年、月、日存储在文件夹中的 U 盘上。
U盘上的文件夹创建已经可以了。
我遇到的问题是文件已创建但没有内容。
该文件的大小为 0kb。
这是代码。
感谢您的帮助。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Devices.Enumeration;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Media.Capture;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Windows.Media.MediaProperties;
using Windows.Media.Playback;
using Windows.Storage;
using Windows.Storage.Streams;
namespace Recording_PI
{
/// <summary>
/// Eine leere Seite, die eigenständig verwendet oder zu der innerhalb eines Rahmens navigiert werden kann.
/// </summary>
public sealed partial class MainPage : Page
{
bool isRecording;
LowLagMediaRecording audioRecording;
MediaCapture audioCapture = new MediaCapture();
public MainPage()
{
this.InitializeComponent();
Checked();
Task.Delay(10000).Wait();
Unchecked();
}
private async void Checked()
{
var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings();
settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Audio;
settings.MediaCategory = Windows.Media.Capture.MediaCategory.Other;
settings.AudioProcessing = Windows.Media.AudioProcessing.Default;
await audioCapture.InitializeAsync(settings);
StorageFolder externalDevices = KnownFolders.RemovableDevices;
IReadOnlyList<StorageFolder> externalDrives = await externalDevices.GetFoldersAsync();
StorageFolder usbStorage = externalDrives[0];
//ENSURE FOLDER EXISTS
if (await usbStorage.TryGetItemAsync("Recording") == null)
await usbStorage.CreateFolderAsync("Recording");
string Folder_Pfad = "Recording\" + DateTime.Now.Year.ToString();
if (await usbStorage.TryGetItemAsync(Folder_Pfad) == null)
await usbStorage.CreateFolderAsync(Folder_Pfad);
Folder_Pfad = Folder_Pfad + "\" + DateTime.Now.Month.ToString();
if (await usbStorage.TryGetItemAsync(Folder_Pfad) == null)
await usbStorage.CreateFolderAsync(Folder_Pfad);
Folder_Pfad = Folder_Pfad + "\" + DateTime.Now.Day.ToString();
if (await usbStorage.TryGetItemAsync(Folder_Pfad) == null)
await usbStorage.CreateFolderAsync(Folder_Pfad);
string Dateiname = "\" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + " "
+ DateTime.Now.Hour.ToString() + "_" + DateTime.Now.Minute.ToString() + "_" + DateTime.Now.Second.ToString()+ ".mp3";
string Dateispeicher_Ort = Folder_Pfad + Dateiname;
StorageFile recordFile = await usbStorage.CreateFileAsync(Dateispeicher_Ort, Windows.Storage.CreationCollisionOption.GenerateUniqueName);
await audioCapture.StartRecordToStorageFileAsync(MediaEncodingProfile.CreateMp3(AudioEncodingQuality.Medium), recordFile);
}
private async void Unchecked()
{
if (isRecording)
{
await audioCapture.StopRecordAsync();
}
}
}
}
The problem I have is that the file is created but has no content. The
file is 0kb in size.
这是因为当 运行 on Raspberry Pi with Windows IoT Core 时,您的代码在以下行出现异常,因此文件尚未写入内容。如果您调试应用程序,您将由于此异常而被终止。
await audioCapture.StartRecordToStorageFileAsync(MediaEncodingProfile.CreateMp3(AudioEncodingQuality.Auto), recordFile);
您需要使用以下行来代替上面的行:
var audioRecording = await audioCapture.PrepareLowLagRecordToStorageFileAsync(MediaEncodingProfile.CreateM4a(AudioEncodingQuality.Auto), recordFile);
await audioRecording.StartAsync();
参考:“MediaCapture.PrepareLowLagRecordToStorageFileAsync”
你的代码还有其他问题。
isRecording
变量从未初始化或分配任何值,所以它总是 false
。所以这一行永远不会被执行:await audioCapture.StopRecordAsync();
Unchecked()
方法可能会在 Checked()
函数完成之前执行,因为在 Checked()
方法中你使用了 await
,所以代码将继续执行,然后 Task.Delay(10000).Wait();
和 Unchecked();
。在这种情况下,音频将在开始之前停止。
另一个建议是在您的代码中添加 try-catch,以便您可以在应用程序终止之前处理异常。
基于上述所有内容,我对您的代码进行了一些修改。下面是完整的代码,你可以试试看是否有帮助。
MainPage.xaml:
<Page
x:Class="Recording_PI.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Recording_PI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel VerticalAlignment="Center">
<TextBlock Name="Result" Text="" />
</StackPanel>
</Page>
MainPage.xaml.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Devices.Enumeration;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Media.Capture;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Windows.Media.MediaProperties;
using Windows.Media.Playback;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
namespace Recording_PI
{
/// <summary>
/// Eine leere Seite, die eigenständig verwendet oder zu der innerhalb eines Rahmens navigiert werden kann.
/// </summary>
public sealed partial class MainPage : Page
{
bool isRecording;
LowLagMediaRecording audioRecording;
MediaCapture audioCapture = new MediaCapture();
public MainPage()
{
this.InitializeComponent();
Checked();
}
private async void Checked()
{
try
{
var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings();
settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Audio;
settings.MediaCategory = Windows.Media.Capture.MediaCategory.Other;
settings.AudioProcessing = Windows.Media.AudioProcessing.Default;
await audioCapture.InitializeAsync(settings);
StorageFolder externalDevices = KnownFolders.RemovableDevices;
IReadOnlyList<StorageFolder> externalDrives = await externalDevices.GetFoldersAsync();
StorageFolder usbStorage = externalDrives[0];
//ENSURE FOLDER EXISTS
if (await usbStorage.TryGetItemAsync("Recording") == null)
await usbStorage.CreateFolderAsync("Recording");
string Folder_Pfad = "Recording\" + DateTime.Now.Year.ToString();
if (await usbStorage.TryGetItemAsync(Folder_Pfad) == null)
await usbStorage.CreateFolderAsync(Folder_Pfad);
Folder_Pfad = Folder_Pfad + "\" + DateTime.Now.Month.ToString();
if (await usbStorage.TryGetItemAsync(Folder_Pfad) == null)
await usbStorage.CreateFolderAsync(Folder_Pfad);
Folder_Pfad = Folder_Pfad + "\" + DateTime.Now.Day.ToString();
if (await usbStorage.TryGetItemAsync(Folder_Pfad) == null)
await usbStorage.CreateFolderAsync(Folder_Pfad);
string Dateiname = "\" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + " "
+ DateTime.Now.Hour.ToString() + "_" + DateTime.Now.Minute.ToString() + "_" + DateTime.Now.Second.ToString() + ".mp3";
string Dateispeicher_Ort = Folder_Pfad + Dateiname;
StorageFile recordFile = await usbStorage.CreateFileAsync(Dateispeicher_Ort, Windows.Storage.CreationCollisionOption.GenerateUniqueName);
isRecording = true;
//await audioCapture.StartRecordToStorageFileAsync(MediaEncodingProfile.CreateMp3(AudioEncodingQuality.Auto), recordFile);
var audioRecording = await audioCapture.PrepareLowLagRecordToStorageFileAsync(MediaEncodingProfile.CreateM4a(AudioEncodingQuality.Medium), recordFile);
await audioRecording.StartAsync();
Task.Delay(10000).Wait();
Unchecked();
}
catch (Exception ex)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, ()=> {
Result.Text += ex.Message;
});
}
}
private async void Unchecked()
{
if (isRecording)
{
await audioCapture.StopRecordAsync();
}
}
}
}
package.appxmanifest中的能力:
<Capabilities>
<uap:Capability Name="removableStorage" />
<DeviceCapability Name="microphone" />
</Capabilities>
设备:Raspberry PI3 OS: WIN 10 物联网 编程语言:C#
我正在尝试编写一个 24/7 录音机,它会在 Raspberry 打开后立即记录所有内容。 音频文件长度应在 30 分钟后。 然后,这些文件将按年、月、日存储在文件夹中的 U 盘上。 U盘上的文件夹创建已经可以了。
我遇到的问题是文件已创建但没有内容。 该文件的大小为 0kb。 这是代码。
感谢您的帮助。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Devices.Enumeration;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Media.Capture;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Windows.Media.MediaProperties;
using Windows.Media.Playback;
using Windows.Storage;
using Windows.Storage.Streams;
namespace Recording_PI
{
/// <summary>
/// Eine leere Seite, die eigenständig verwendet oder zu der innerhalb eines Rahmens navigiert werden kann.
/// </summary>
public sealed partial class MainPage : Page
{
bool isRecording;
LowLagMediaRecording audioRecording;
MediaCapture audioCapture = new MediaCapture();
public MainPage()
{
this.InitializeComponent();
Checked();
Task.Delay(10000).Wait();
Unchecked();
}
private async void Checked()
{
var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings();
settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Audio;
settings.MediaCategory = Windows.Media.Capture.MediaCategory.Other;
settings.AudioProcessing = Windows.Media.AudioProcessing.Default;
await audioCapture.InitializeAsync(settings);
StorageFolder externalDevices = KnownFolders.RemovableDevices;
IReadOnlyList<StorageFolder> externalDrives = await externalDevices.GetFoldersAsync();
StorageFolder usbStorage = externalDrives[0];
//ENSURE FOLDER EXISTS
if (await usbStorage.TryGetItemAsync("Recording") == null)
await usbStorage.CreateFolderAsync("Recording");
string Folder_Pfad = "Recording\" + DateTime.Now.Year.ToString();
if (await usbStorage.TryGetItemAsync(Folder_Pfad) == null)
await usbStorage.CreateFolderAsync(Folder_Pfad);
Folder_Pfad = Folder_Pfad + "\" + DateTime.Now.Month.ToString();
if (await usbStorage.TryGetItemAsync(Folder_Pfad) == null)
await usbStorage.CreateFolderAsync(Folder_Pfad);
Folder_Pfad = Folder_Pfad + "\" + DateTime.Now.Day.ToString();
if (await usbStorage.TryGetItemAsync(Folder_Pfad) == null)
await usbStorage.CreateFolderAsync(Folder_Pfad);
string Dateiname = "\" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + " "
+ DateTime.Now.Hour.ToString() + "_" + DateTime.Now.Minute.ToString() + "_" + DateTime.Now.Second.ToString()+ ".mp3";
string Dateispeicher_Ort = Folder_Pfad + Dateiname;
StorageFile recordFile = await usbStorage.CreateFileAsync(Dateispeicher_Ort, Windows.Storage.CreationCollisionOption.GenerateUniqueName);
await audioCapture.StartRecordToStorageFileAsync(MediaEncodingProfile.CreateMp3(AudioEncodingQuality.Medium), recordFile);
}
private async void Unchecked()
{
if (isRecording)
{
await audioCapture.StopRecordAsync();
}
}
}
}
The problem I have is that the file is created but has no content. The file is 0kb in size.
这是因为当 运行 on Raspberry Pi with Windows IoT Core 时,您的代码在以下行出现异常,因此文件尚未写入内容。如果您调试应用程序,您将由于此异常而被终止。
await audioCapture.StartRecordToStorageFileAsync(MediaEncodingProfile.CreateMp3(AudioEncodingQuality.Auto), recordFile);
您需要使用以下行来代替上面的行:
var audioRecording = await audioCapture.PrepareLowLagRecordToStorageFileAsync(MediaEncodingProfile.CreateM4a(AudioEncodingQuality.Auto), recordFile);
await audioRecording.StartAsync();
参考:“MediaCapture.PrepareLowLagRecordToStorageFileAsync”
你的代码还有其他问题。
isRecording
变量从未初始化或分配任何值,所以它总是false
。所以这一行永远不会被执行:await audioCapture.StopRecordAsync();
Unchecked()
方法可能会在Checked()
函数完成之前执行,因为在Checked()
方法中你使用了await
,所以代码将继续执行,然后Task.Delay(10000).Wait();
和Unchecked();
。在这种情况下,音频将在开始之前停止。
另一个建议是在您的代码中添加 try-catch,以便您可以在应用程序终止之前处理异常。
基于上述所有内容,我对您的代码进行了一些修改。下面是完整的代码,你可以试试看是否有帮助。
MainPage.xaml:
<Page
x:Class="Recording_PI.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Recording_PI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel VerticalAlignment="Center">
<TextBlock Name="Result" Text="" />
</StackPanel>
</Page>
MainPage.xaml.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Devices.Enumeration;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Media.Capture;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Windows.Media.MediaProperties;
using Windows.Media.Playback;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
namespace Recording_PI
{
/// <summary>
/// Eine leere Seite, die eigenständig verwendet oder zu der innerhalb eines Rahmens navigiert werden kann.
/// </summary>
public sealed partial class MainPage : Page
{
bool isRecording;
LowLagMediaRecording audioRecording;
MediaCapture audioCapture = new MediaCapture();
public MainPage()
{
this.InitializeComponent();
Checked();
}
private async void Checked()
{
try
{
var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings();
settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Audio;
settings.MediaCategory = Windows.Media.Capture.MediaCategory.Other;
settings.AudioProcessing = Windows.Media.AudioProcessing.Default;
await audioCapture.InitializeAsync(settings);
StorageFolder externalDevices = KnownFolders.RemovableDevices;
IReadOnlyList<StorageFolder> externalDrives = await externalDevices.GetFoldersAsync();
StorageFolder usbStorage = externalDrives[0];
//ENSURE FOLDER EXISTS
if (await usbStorage.TryGetItemAsync("Recording") == null)
await usbStorage.CreateFolderAsync("Recording");
string Folder_Pfad = "Recording\" + DateTime.Now.Year.ToString();
if (await usbStorage.TryGetItemAsync(Folder_Pfad) == null)
await usbStorage.CreateFolderAsync(Folder_Pfad);
Folder_Pfad = Folder_Pfad + "\" + DateTime.Now.Month.ToString();
if (await usbStorage.TryGetItemAsync(Folder_Pfad) == null)
await usbStorage.CreateFolderAsync(Folder_Pfad);
Folder_Pfad = Folder_Pfad + "\" + DateTime.Now.Day.ToString();
if (await usbStorage.TryGetItemAsync(Folder_Pfad) == null)
await usbStorage.CreateFolderAsync(Folder_Pfad);
string Dateiname = "\" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + " "
+ DateTime.Now.Hour.ToString() + "_" + DateTime.Now.Minute.ToString() + "_" + DateTime.Now.Second.ToString() + ".mp3";
string Dateispeicher_Ort = Folder_Pfad + Dateiname;
StorageFile recordFile = await usbStorage.CreateFileAsync(Dateispeicher_Ort, Windows.Storage.CreationCollisionOption.GenerateUniqueName);
isRecording = true;
//await audioCapture.StartRecordToStorageFileAsync(MediaEncodingProfile.CreateMp3(AudioEncodingQuality.Auto), recordFile);
var audioRecording = await audioCapture.PrepareLowLagRecordToStorageFileAsync(MediaEncodingProfile.CreateM4a(AudioEncodingQuality.Medium), recordFile);
await audioRecording.StartAsync();
Task.Delay(10000).Wait();
Unchecked();
}
catch (Exception ex)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, ()=> {
Result.Text += ex.Message;
});
}
}
private async void Unchecked()
{
if (isRecording)
{
await audioCapture.StopRecordAsync();
}
}
}
}
package.appxmanifest中的能力:
<Capabilities>
<uap:Capability Name="removableStorage" />
<DeviceCapability Name="microphone" />
</Capabilities>