英文:
VideoView xamarin - endless video
问题
I'm trying to code an application that loops a video indefinitely in a Xamarin application.
I'm using an Odroid-X4U as a device to connect to a TV to create a "SaverScreen" that plays videos.
Here is my code:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
session = new SessionManager(this);
memoryTextView = FindViewById<TextView>(Resource.Id.textViewMemory);
hourTextView = FindViewById<TextView>(Resource.Id.textViewHour);
memoryTimer = new Timer(UpdateMemory, null, 0, 1000);
videoView = FindViewById<VideoView>(Resource.Id.videoView1);
videoView.Completion += VideoView_Completion;
Window.AddFlags(WindowManagerFlags.Fullscreen);
PlayNextVideo();
}
private void PlayNextVideo()
{
string tmpPath = App.AppMainFolder + "/" + videoUrls[1];
videoView.SetVideoPath(tmpPath);
videoView.Start();
}
private void VideoView_Completion(object sender, EventArgs e)
{
videoView.StopPlayback();
videoView.Dispose();
videoView = FindViewById<VideoView>(Resource.Id.videoView1);
hourTextView.Text = DateTime.Now.ToString("HH:mm:ss");
currentIndex = (currentIndex + 1) % videoUrls.Length;
PlayNextVideo();
}
This code is functional; however, after several hours, the video freezes completely, and I have to restart my device completely to restart the video.
I also added a code block to check the RAM, and it's not a storage problem:
private void UpdateMemory(object state)
{
var memoryInfo = new ActivityManager.MemoryInfo();
var activityManager = (ActivityManager)GetSystemService(ActivityService);
activityManager.GetMemoryInfo(memoryInfo);
RunOnUiThread(() =>
{
var currentTime = DateTime.Now.ToString("HH:mm:ss");
memoryTextView.Text = "RAM: " + (memoryInfo.AvailMem / 1048576L) + "MB | time: " + currentTime;
});
}
Does anyone have a solution so that the video doesn't freeze after a while?
I also checked the device logs (logcat), and nothing is displayed about the video bug.
英文:
I'm trying to code an application that loops a video indefinitely in an xamarin application.
I'm using a Odroid-X4U as device to connect to a TV to make a "SaverScreen" that play videos.
Here my code :
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
session = new SessionManager(this);
memoryTextView = FindViewById<TextView>(Resource.Id.textViewMemory);
hourTextView = FindViewById<TextView>(Resource.Id.textViewHour);
memoryTimer = new Timer(UpdateMemory, null, 0, 1000);
videoView = FindViewById<VideoView>(Resource.Id.videoView1);
videoView.Completion += VideoView_Completion;
Window.AddFlags(WindowManagerFlags.Fullscreen);
PlayNextVideo();
}
private void PlayNextVideo()
{
string tmpPath = App.AppMainFolder + "/" + videoUrls[1];
videoView.SetVideoPath(tmpPath);
videoView.Start();
}
private void VideoView_Completion(object sender, EventArgs e)
{
videoView.StopPlayback();
videoView.Dispose();
videoView = FindViewById<VideoView>(Resource.Id.videoView1);
hourTextView.Text = DateTime.Now.ToString("HH:mm:ss");
currentIndex = (currentIndex + 1) % videoUrls.Length;
PlayNextVideo();
}
this code is functional, however after several hours the video freezes completely and I have to restart my device completely to restart the video.
I also put a little bloc of code to check the ram, and it's not a storage problem
private void UpdateMemory(object state)
{
var memoryInfo = new ActivityManager.MemoryInfo();
var activityManager = (ActivityManager)GetSystemService(ActivityService);
activityManager.GetMemoryInfo(memoryInfo);
RunOnUiThread(() =>
{
var currentTime = DateTime.Now.ToString("HH:mm:ss");
memoryTextView.Text = "RAM : " + (memoryInfo.AvailMem / 1048576L) + "MB | time : " + currentTime;
});
}
Does anyone have a solution so that the video doesn't freeze after a while?
I also went to the device logs (logcat) and nothing is displayed about the video bug.
答案1
得分: 1
根据有关使用 VideoView 实现无缝视频循环的 Android 本机案例的内容,您可以尝试使用 videoView.setOnPreparedListener
方法来循环播放视频。
首先,声明自定义的监听器类:
public class MyListener : Java.Lang.Object, IOnPreparedListener
{
public void OnPrepared(MediaPlayer mp)
{
mp.Looping = true;
}
}
然后为 videoview 设置监听器:
videoView = FindViewById<VideoView>(Resource.Id.videoView1);
videoView.SetOnPreparedListener(new MyListener());
英文:
According to the native android case about Seamless video Loop with VideoView, you can try to loop the video with the videoView.setOnPreparedListener
method.
First of all, declare the custom listener class:
public class MyListener : Java.Lang.Object, IOnPreparedListener
{
public void OnPrepared(MediaPlayer mp)
{
mp.Looping = true;
}
}
And then set the listener for the videoview:
videoView = FindViewById<VideoView>(Resource.Id.videoView1);
videoView.SetOnPreparedListener(new MyListener());
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论