I am trying to listen to audio data using memory stream for a certain pattern can I read the audio input async. This is my code but there is a problem with ReadAsync. I am using windowsPhone 8.1. can anyone help with this, listen to the audio data from the MIC for an audio pattern, but the problem i am having is with memory streams.
public Page3()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
InitializeAudioRecording();
isRecording = false;
CaptureAudio();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// InitializeAudioRecording();
// LoadAudioQualities();
// CaptureAudio();
}
public async void InitializeAudioRecording()
{
try
{
if (_mediaCaptureManager != null)
{
throw new InvalidOperationException("Audio is already initialized");
}
_mediaCaptureManager = new MediaCapture();
_mediaCaptureManager.Failed += MediaCaptureOnFailed;
_mediaCaptureManager.RecordLimitationExceeded += new RecordLimitationExceededEventHandler(MediaCaptureOnRecordLimitationExceeded);
var settings = new MediaCaptureInitializationSettings();
settings.StreamingCaptureMode = StreamingCaptureMode.Audio;
settings.MediaCategory = MediaCategory.Other;
settings.AudioProcessing = (_rawAudioSupported && _userRequestedRaw) ? AudioProcessing.Raw : AudioProcessing.Default;
Debug.WriteLine("Device initialised successfully");
// Next, we will initialize the local media capture object.
await _mediaCaptureManager.InitializeAsync(settings);
}
catch (Exception e)
{
DoCleanup();
throw e;
}
}
public async Task CaptureAudio()
{
short[] signal = new short[BLOCKSIZE];
int bufferReadResult;
var audioQualities = Enum.GetValues(typeof(AudioEncodingQuality)).Cast<AudioEncodingQuality>();
MediaEncodingProfile recordProfile = MediaEncodingProfile.CreateM4a(AudioEncodingQuality.Auto);
try
{
message = new InMemoryRandomAccessStream();
await message.FlushAsync();
isRecording = true;
await _mediaCaptureManager.StartRecordToStreamAsync(recordProfile, message);
do {
bufferReadResult = message.ReadAsync(message, 0, BLOCKSIZE);
....process the audio data.....
} while(bufferReadResult != BLOCKSIZE);
Debug.WriteLine("Stopping recording");
await _mediaCaptureManager.StopRecordAsync();
await message.FlushAsync();
message.Dispose();
Debug.WriteLine("Stop recording successful");
isRecording = false;
}
catch(Exception ee)
{
new MessageDialog(ee.ToString()).ShowAsync();
}
}
private async void MediaCaptureOnFailed(MediaCapture sender, MediaCaptureFailedEventArgs errorEventArgs)
{
//if (CaptureFailed != null && forwardEvents) CaptureFailed(this, errorEventArgs);
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
var warningMessage = new MessageDialog(String.Format("The audio capture failed: {0}", errorEventArgs.Message), "Capture Failed");
await warningMessage.ShowAsync();
});
}
private async void MediaCaptureOnRecordLimitationExceeded(MediaCapture sender)
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
await sender.StopRecordAsync();
var warningMessage = new MessageDialog("The recording has stopped because you exceeded the maximum recording length.", "Recording Stoppped");
await warningMessage.ShowAsync();
});
}
/// <summary>
/// Cleans up the resources.
/// </summary>
private void CleanupSink()
{
if (message != null)
{
message.Dispose();
message = null;
recordingStarted = false;
}
}
private void DoCleanup()
{
if (_mediaCaptureManager != null)
{
_mediaCaptureManager.Failed -= MediaCaptureOnFailed;
_mediaCaptureManager = null;
}
CleanupSink();
}
private void GraphWaveform(short[] _recordStorageFile)
{
cvDrawingArea.Children.Clear();
double canvasHeight = cvDrawingArea.Height;
double canvasWidth = cvDrawingArea.Width;
int observablePoints = 1800;
double xScale = canvasWidth / observablePoints;
double yScale = (canvasHeight / (double)(amplitude * 2)) * ((double)amplitude / MAX_AMPLITUDE);
Windows.UI.Xaml.Shapes.Polyline graphLine = new Windows.UI.Xaml.Shapes.Polyline();
//graphLine.Stroke = System.Windows.Media.Brushes.Black;
graphLine.StrokeThickness = 1;
for (int i = 0; i < observablePoints;
i++)
{
graphLine.Points.Add(new Point(i * xScale, (canvasHeight / 2) - (_recordStorageFile[i] * yScale)));
}
cvDrawingArea.Children.Add(graphLine);
}
/// <summary>
/// Asynchronous method cleaning up resources and stopping recording if necessary.
/// </summary>
public async Task CleanUpAsync()
{
try
{
forwardEvents = true;
if (_mediaCaptureManager == null ) return;
if (recordingStarted)
{
await _mediaCaptureManager.StopRecordAsync();
}
DoCleanup();
}
catch (Exception)
{
DoCleanup();
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire