我试图让我的 MediaStreamSource 在 Windows Phone 7.1 上工作。对于较小的分辨率,如 256*256 或 512*512,它可以工作,但在某些分辨率下,它在我调用 ReportOpenMediaCompleted 后立即关闭。我没有得到任何例外等。
这是我的代码:
public class BitmapStreamSource : MediaStreamSource
{
MediaStreamDescription bitmapMediaStream;
const int width = 1024;
const int height = 1024;
protected override void OpenMediaAsync()
{
Dictionary<MediaSourceAttributesKeys, string> mediaStreamAttributes;
List<MediaStreamDescription> availableMediaStreams;
Dictionary<MediaStreamAttributeKeys, string> bitmapStreamAttributes;
mediaStreamAttributes = new Dictionary<MediaSourceAttributesKeys, string>(2);
mediaStreamAttributes[MediaSourceAttributesKeys.CanSeek] = false.ToString();
mediaStreamAttributes[MediaSourceAttributesKeys.Duration] = "-1";
bitmapStreamAttributes = new Dictionary<MediaStreamAttributeKeys,string>(3);
bitmapStreamAttributes[MediaStreamAttributeKeys.VideoFourCC]= "RGBA";
bitmapStreamAttributes[MediaStreamAttributeKeys.Width]= width.ToString();
bitmapStreamAttributes[MediaStreamAttributeKeys.Height] = height.ToString();
bitmapMediaStream = new MediaStreamDescription(MediaStreamType.Video, bitmapStreamAttributes);
availableMediaStreams = new List<MediaStreamDescription>(1);
availableMediaStreams.Add(bitmapMediaStream);
ReportOpenMediaCompleted(mediaStreamAttributes, availableMediaStreams);
}
protected override void SeekAsync(long seekToTime)
{
ReportSeekCompleted(seekToTime);
}
int sampleCount = 0;
protected override void GetSampleAsync(MediaStreamType mediaStreamType)
{
int pixel = width * height;
var buffer = new byte[pixel * 4];
for (int i = 0; i < pixel; i++)
{
byte value = (byte)((i + sampleCount) % 256);
var k = i * 4;
//buffer[k] = 255; // B
buffer[k + 1] = value; // G
buffer[k + 2] = 255; // R
//buffer[k + 3] = value; // A
}
var stream = new MemoryStream(buffer);
var keys = new Dictionary<MediaSampleAttributeKeys, string>();
var sample = new MediaStreamSample(bitmapMediaStream, stream, 0, buffer.Length, 0, keys);
sampleCount++;
ReportGetSampleCompleted(sample);
}
protected override void CloseMedia()
{
Debug.WriteLine("CloseMedia()");
}
protected override void GetDiagnosticAsync(MediaStreamSourceDiagnosticKind diagnosticKind)
{
throw new NotImplementedException();
}
protected override void SwitchMediaStreamAsync(MediaStreamDescription mediaStreamDescription)
{
throw new NotImplementedException();
}
}