Reputation: 2036
I have to play an audio part out of a big file (300MB+).
This is my code:
// Media source is a local file. // datName = "sound.dat" // pointer = position in file // length = length of the part to play try { file = await StorageFile.GetFileFromApplicationUriAsync (new Uri(@"ms-appx:///Data/" + datName)); // Get the media source as a stream. IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read); stream.Seek((ulong)pointer); // This is working, position changes from 0 to pointer stream.Size = (ulong)length; // Is not working, Size remains unchanged at total file size media.SetSource(stream, file.ContentType); media.Play(); } catch (Exception ex) { if (ex is FormatException || ex is ArgumentException) { //ShowFileErrorMsg(); } }
Please note the comments on stream Seek and Size. The file is played from position zero.
How can I play the sound from pointer to pointer + length?
Upvotes: 1
Views: 207
Reputation: 2036
I've solved my problem by using the binary reader. I read the required area in a byte buffer and convert it to an IRandomAccessStream.
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///Data/" + fileName)); using (Stream stream = (await file.OpenReadAsync()).AsStreamForRead()) using (BinaryReader reader = new BinaryReader(stream)) { reader.BaseStream.Position = pointer; byte[] buffer = reader.ReadBytes((int)length); IRandomAccessStream nstream = new MemoryStream(buffer).AsRandomAccessStream(); media.SetSource(nstream, ""); media.Play(); }
This version is now working.
Upvotes: 1