我必须用大文件(300MB +)播放音频部分。
这是我的代码:
// 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(); } }
请注意关于流量搜索和大小的评论。文件从零位置播放。
如何从指针到指针+长度播放声音?
答案 0 :(得分:1)
我使用二进制阅读器解决了我的问题。我在字节缓冲区中读取所需区域并将其转换为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(); }
此版本现已正常运作。