【问题标题】:xamarin.android: video freezes for a moment when startedxamarin.android:视频在启动时冻结片刻
【发布时间】:2018-03-26 13:04:32
【问题描述】:

我尝试在 Android 4.4 上的应用中显示一系列视频和一系列图像(视频后跟两个图像在一个屏幕上,然后是下一个视频,然后是下两个图像,依此类推)。我首先加载一个视频,然后在视频结束时运行 OnComplete() 方法:

public class OnComplete : Object, MediaPlayer.IOnCompletionListener
{
    public void OnCompletion(MediaPlayer mp)
    {
        Runnable switchMedia = new Runnable(() =>
        {
            MainActivity._handler.Post(new Runnable(() =>
            {
                int imageListLength = MainActivity.imgList.Count;
                if(imageListLength > 0)
                {
                    MainActivity.imageIndex = (MainActivity.imageIndex + 1) % imageListLength;
                    MainActivity.fullScreenVideo.Visibility = ViewStates.Gone;
                    MainActivity.dividedLayout.Visibility = ViewStates.Visible;                        
                    MainActivity.imgSwitcher1.SetImageURI(Android.Net.Uri.FromFile(new Java.IO.File(MainActivity.imgList[MainActivity.imageIndex])));
                    MainActivity.imageIndex = (MainActivity.imageIndex + 1) % imageListLength;
                    MainActivity.imgSwitcher2.SetImageURI(Android.Net.Uri.FromFile(new Java.IO.File(MainActivity.imgList[MainActivity.imageIndex])));
                }
                MainActivity._handler.PostDelayed(new Runnable(() =>
                {
                    int videoListLength = MainActivity.vidList.Count;
                    if(videoListLength > 0)
                    {
                        MainActivity.videoIndex = (MainActivity.videoIndex + 1) % videoListLength;
                        MainActivity.fullScreenVideo.Visibility = ViewStates.Visible;
                        MainActivity.dividedLayout.Visibility = ViewStates.Gone;
                        MainActivity.fullScreenVideo.SetVideoURI(Android.Net.Uri.FromFile(new Java.IO.File(MainActivity.vidList[MainActivity.videoIndex])));                           
                        MainActivity.fullScreenVideo.Start();
                    }
                }), 10000);
            }));
        });
        switchMedia.Run();
    }
}

这是一个主类:

[Activity(Label = "RandomDisplay", MainLauncher = true)]
public class MainActivity : Activity, ViewSwitcher.IViewFactory
{
    public static List<string> imgList;
    public static int imageIndex = 0;
    public static List<string> vidList;
    public static int videoIndex = 0;
    public static LinearLayout dividedLayout;
    public static ImageSwitcher imgSwitcher1;
    public static ImageSwitcher imgSwitcher2;
    public static VideoView fullScreenVideo;
    public static Handler _handler;

    public View MakeView()
    {
        ImageView imageView = new ImageView(this);
        imageView.SetScaleType(ImageView.ScaleType.FitXy);
        imageView.LayoutParameters = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
        return imageView;
    }

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        imgList = new List<string>();
        vidList = new List<string>();
        PopulateList(ref imgList, @"/sdcard/Download/IV/Img/");
        PopulateList(ref vidList, @"/sdcard/Download/IV/Video");
        Window.AddFlags(WindowManagerFlags.Fullscreen);
        ActionBar.Hide();
        _handler = new Handler();
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        imgSwitcher1 = FindViewById<ImageSwitcher>(Resource.Id.ImageSwitcher1);
        imgSwitcher1.SetFactory(this);
        imgSwitcher2 = FindViewById<ImageSwitcher>(Resource.Id.ImageSwitcher2);
        imgSwitcher2.SetFactory(this);
        fullScreenVideo = FindViewById<VideoView>(Resource.Id.FullScreenVideo);
        dividedLayout = FindViewById<LinearLayout>(Resource.Id.DividedScreenLayout);
        dividedLayout.Visibility = ViewStates.Gone;
        fullScreenVideo.SetVideoURI(Android.Net.Uri.FromFile(new Java.IO.File(vidList[0])));
        fullScreenVideo.SetOnCompletionListener(new OnComplete());
        fullScreenVideo.Start();
    }

    private void PopulateList(ref List<string> list, string directory)
    {
        Handler h = new Handler();
        if (!Directory.Exists(directory))
        {
            h.PostAtFrontOfQueue(new Runnable(() =>
            {
                Toast.MakeText(this, directory + " doesn't exist.", ToastLength.Long).Show();
            }));
            return;
        }
        h.Post(new Runnable(() =>
        {
            Toast.MakeText(this, directory + " exists.", ToastLength.Long).Show();
        }));
        foreach (string s in Directory.GetFiles(directory))
        {
            list.Add(s);
        }
    }
}

视频和图像从 sdcard/Download 目录加载。视频播放始终如下所示:视频开始 --> 暂停一秒钟 --> 继续正常播放到最后。它可能有什么问题?

【问题讨论】:

    标签: android xamarin xamarin.android


    【解决方案1】:

    视频和图像从 sdcard/Download 目录加载。视频播放始终如下所示:视频开始 --> 暂停一秒钟 --> 继续正常播放到最后。它可能有什么问题?

    设备需要时间将视频加载到内存中。它没有问题,您需要的是在其中放置一个负载。您可以使用ProgressDialogProgressBar 来执行此操作。并在OnPrepared时停止进度。

    【讨论】:

    • 我不确定我的描述是否具有误导性。问题不在于加载视频需要一段时间。问题是视频实际上开始播放一点点,然后冻结一秒钟,然后继续流畅播放。我尝试使用 OnPrepared 方法开始播放视频,但没有任何改变。
    • 你在其他设备/模拟器上试过了吗?可能是设备的问题。
    • 我尝试了两台设备(我的手机和一台在安卓上运行的显示器)。结果相同。
    • 你能通过在线存储库分享一个基本的演示项目吗?以及您测试的视频大小和格式。
    • 视频为 .mp4 不大于 10MB
    猜你喜欢
    • 2015-08-13
    • 2022-01-17
    • 1970-01-01
    • 2015-04-04
    • 2022-11-17
    • 2015-09-25
    • 1970-01-01
    • 2012-07-06
    • 2018-05-14
    相关资源
    最近更新 更多