“VideoView xamarin – endless video” can be translated as “VideoView Xamarin – 无限视频.”

huangapple go评论92阅读模式
英文:

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&lt;TextView&gt;(Resource.Id.textViewMemory);
	hourTextView = FindViewById&lt;TextView&gt;(Resource.Id.textViewHour);
	memoryTimer = new Timer(UpdateMemory, null, 0, 1000);

	videoView = FindViewById&lt;VideoView&gt;(Resource.Id.videoView1);
	videoView.Completion += VideoView_Completion;

	Window.AddFlags(WindowManagerFlags.Fullscreen);

	PlayNextVideo();
}

private void PlayNextVideo()
{
	string tmpPath = App.AppMainFolder + &quot;/&quot; + videoUrls[1];
	videoView.SetVideoPath(tmpPath);
	videoView.Start();
}

private void VideoView_Completion(object sender, EventArgs e)
{
    videoView.StopPlayback();
    videoView.Dispose();
    videoView = FindViewById&lt;VideoView&gt;(Resource.Id.videoView1);

    hourTextView.Text = DateTime.Now.ToString(&quot;HH:mm:ss&quot;);

    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(() =&gt;
	{
		var currentTime = DateTime.Now.ToString(&quot;HH:mm:ss&quot;);
		memoryTextView.Text = &quot;RAM : &quot; + (memoryInfo.AvailMem / 1048576L) + &quot;MB | time : &quot; + 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&lt;VideoView&gt;(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&lt;VideoView&gt;(Resource.Id.videoView1);
videoView.SetOnPreparedListener(new MyListener());

</details>



huangapple
  • 本文由 发表于 2023年5月22日 14:59:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定