利用Mono For Android实现某个文件夹下视频文件的循环播放

这段时间一直在写这个方面的程序,废话不多说,直接上代码:

//VideoView.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

using Android.Media;


namespace SimplePlayer
{
    [Activity(Label = "视频播放界面")]
    public class VideoView : Activity,ISurfaceHolderCallback,MediaPlayer.IOnCompletionListener
    {
        private static int index=0;

        #region private data
        private List<string> playList;
        private MediaPlayer mediaPlayer;
        private string filePath;
        //private SurfaceView surface;
        #endregion


        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Create your application here
            //设置布局
            SetContentView(Resource.Layout.VideoView);
            //初始化
            Init();
            //添加视频播放完成监听
            mediaPlayer.SetOnCompletionListener(this);
            Button closeBtn = FindViewById<Button>(Resource.Id.closeBtn);
            SurfaceView surface = FindViewById<SurfaceView>(Resource.Id.surface);

            //closeBtn点击事件处理
            closeBtn.Click += new EventHandler(closeBtn_Click);
            var holder = surface.Holder;
            holder.AddCallback(this);
            holder.SetType(SurfaceType.PushBuffers);
            holder.SetFixedSize(300, 200);
        }

        //初始化函数,用来初始化要用到的资源
        public void Init()
        {
            
            playList = new List<string>();
            mediaPlayer = new MediaPlayer();
            playList.Add("/test1.mp4");
            playList.Add("/test3.mp4");
            playList.Add("/test4.mp4");
            playList.Add("/test5.mp4");
            //playList.Add("/test6.mp4");
            //playList.Add("/test7.mp4");
            //playList.Add("/test8.mp4");
            //playList.Add("/test9.mp4");
        }

        public string GetListItem(int index)
        {
            return playList[index];
        }

        public int GetListLength()
        {
            return playList.Count;
        }

        //播放功能实现
        public void play()
        {
            try
            {
                //mediaPlayer.SetDisplay(holder);
                index %= GetListLength();               
                mediaPlayer.Reset();
                filePath = "file://" + Android.OS.Environment.ExternalStorageDirectory + GetListItem(index);
                //++index;
                mediaPlayer.SetDataSource(filePath);
                mediaPlayer.Prepared += new EventHandler(mediaPlayer_Prepared);
                mediaPlayer.PrepareAsync();
            }
            catch (Exception e)
            {
                Android.Util.Log.Debug("MEDIA_PALYER", e.Message);
                Toast.MakeText(this, e.Message, ToastLength.Short).Show();
            }
            finally
            {
                              
            }
            
        }

        void mediaPlayer_Prepared(object sender, EventArgs e)
        {
            mediaPlayer.Start();
        }

        void closeBtn_Click(object sender, EventArgs e)
        {
            //关闭当前活动
            this.Finish();
            //垃圾回收
            GC.Collect();
            Process.KillProcess(Process.MyTid());
        }

        public void SurfaceCreated(ISurfaceHolder holder)
        {
            mediaPlayer.SetDisplay(holder);
            try
            {
                //play();
                filePath = "file://" + Android.OS.Environment.ExternalStorageDirectory + GetListItem(index);
                mediaPlayer.SetDataSource(filePath);
                mediaPlayer.Prepared += new EventHandler(mediaPlayer_Prepared);
                mediaPlayer.PrepareAsync();
                
            }
            catch (Exception e)
            {
                Android.Util.Log.Debug("MEDIA_PALYER", e.Message);
                Toast.MakeText(this, e.Message, ToastLength.Short).Show();
            }
            //finally
            //{
            //    mediaPlayer.Reset();
            //}
            //play();
        }

        public void SurfaceChanged(ISurfaceHolder holder, Android.Graphics.Format format, int i, int j)
        {

        }

        public void SurfaceDestroyed(ISurfaceHolder holder)
        {
            mediaPlayer.Release();
        }

        public void OnCompletion(MediaPlayer mp)
        {
            ++index;
            play();
            
        }
    }
}

该文件用于视频播放见面的显示,另外一个文件是SimplePlayer.cs,其具体代码如下:

//SimplePlayer.cs
using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace SimplePlayer
{
    [Activity(Label = "简单播放器", MainLauncher = true, Icon = "@drawable/icon")]
    public class SimplePlayer : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button playBtn = FindViewById<Button>(Resource.Id.playBtn);
            playBtn.Click += new EventHandler(playBtn_Click);
        }

        void playBtn_Click(object sender, EventArgs e)
        {
            Intent i = new Intent();
            i.SetClass(this, typeof(VideoView));
            i.AddFlags(ActivityFlags.NewTask);
            StartActivity(i);
        }
    }
}

两个布局文件分别是Main.axml和VideoView.axml,其代码如下:

Main.axml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button  
    android:id="@+id/playBtn"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/PlayBtn"
    />
  
</LinearLayout>

VideoView.axml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  <Button 
      android:id="@+id/closeBtn"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/CloseBtn"
      />
  <SurfaceView
    android:id="@+id/surface"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    />
</LinearLayout>

很简单,但是存在以下几个问题,希望有兴趣的可以帮忙看一下:

  1. 有时候不能播放出全部视频;
  2. 不能实现视频的循环播放,即播放完成后不能跳到第一个视频;
  3. 如果将mediaPlayer.SetDisplay()放在try语句中只能播放两个视频,而放在外面则能播放四个;
  4. 添加视频文件后似乎又不能播放;
  5. Prepared事件处理函数会执行多次,具体执行情况是播放第一个视频执行一次,第二个播放两次,第三个播放三次······这个应该就是问题所在了,如何解决?

个人觉得上面的程序没有逻辑上的问题,希望大家能将这个程序完善一下!

posted @ 2013-07-16 15:38  荏苒追寻  阅读(848)  评论(0编辑  收藏  举报