Last active
October 6, 2019 03:45
-
-
Save ChiiAyano/2b45acce95a498af472b4102c3423535 to your computer and use it in GitHub Desktop.
MediaDataStream を使う
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyMediaPlayer | |
{ | |
private Android.Media.MediaPlayer MediaPlayerInstance { get; set; } | |
public void CreateMediaPlayer() | |
{ | |
this.MediaPlayerInstance = new Android.Media.MediaPlayer(); | |
} | |
public void Play(System.IO.Stream stream) | |
{ | |
var source = new StreamMediaDataSource(stream); | |
this.MediaPlayerInstance.Reset(); | |
this.MediaPlayerInstance.SetDataSource(source); | |
this.MediaPlayerInstance.Prepare(); | |
this.MediaPlayerInstance.Start(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StreamMediaDataSource : Android.Media.MediaDataSource | |
{ | |
private System.IO.Stream MediaStream { get; } | |
public override long Size => this.MediaStream.Length; | |
public StreamMediaDataSource(System.IO.Stream stream) | |
{ | |
this.MediaStream = stream; | |
} | |
public override void Close() | |
{ | |
this.MediaStream.Close(); | |
} | |
public override int ReadAt(long position, byte[] buffer, int offset, int size) | |
{ | |
// 読み取りしたい位置とかがくるのでシークして読んで返す | |
this.MediaStream.Seek(position, System.IO.SeekOrigin.Begin); | |
var result = this.MediaStream.Read(buffer, offset, size); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment