无法使用monodroid启动媒体播放器流

时间:2012-04-02 00:40:20

标签: c# xamarin.android

我的代码中有一个媒体播放器,无法显示任何视频。调用prepareasync()方法时,它抛出一个关于IllegalStateException的JavaLangException。

我已经看了很多,为什么会出现这种情况,但在C#中找不到任何东西(只有Java)。

任何帮助将不胜感激

using System;
using System.Security.Cryptography;
using System.Text;

using Android.App;
using Android.Media;
using Android.OS;
using Android.Views;
using Android.Widget;

namespace androidvid
{
    [Activity(Theme = "@android:style/Theme.NoTitleBar")]
    public class videoplayer : Activity, ISurfaceHolderCallback
    {
        private MediaPlayer mp;
        private string url;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.video);
            url = base.Intent.GetStringExtra("url");
            string urlHost = "http://replays.robstream.co.uk/racereplays/replaysmobile.php?";

            string pk = "oq54rfju6vnjkm123grt";

            DateTime d1 = new DateTime(1970, 1, 1);
            DateTime d2 = DateTime.UtcNow;
            TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);

            double sd = ts.TotalMilliseconds;

            string UTC = sd.ToString().Substring(0, 10);
            string MD5String = UTC + pk + url;
            MD5String = GetMD5Hash(MD5String);

            urlHost += "fish=" + url;
            urlHost += "&cust=xyzab";
            urlHost += "&t=" + UTC;
            urlHost += "&h=" + MD5String;

            url = urlHost;

            mp = new Android.Media.MediaPlayer();
            var surface = FindViewById<SurfaceView>(Resource.Id.surfaceView);
            var holder = surface.Holder;
            holder.AddCallback(this);
            holder.SetType(Android.Views.SurfaceType.PushBuffers);
            holder.SetFixedSize(400, 300);
            try
            {
               mp.PrepareAsync();
            }
            catch (Java.Lang.IllegalStateException e)
            {
                Toast.MakeText(this, e.ToString(), ToastLength.Short).Show();
            }
            mp.Prepared += (object s, EventArgs e) => { mp.Start(); };

            Button btnClose = FindViewById<Button>(Resource.Id.btnClose);
            btnClose.Click += (object s, EventArgs e) => { this.Finish(); };
            mp.Prepared += (object s, EventArgs e) => { mp.Start(); };
        }

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

    public void SurfaceChanged(ISurfaceHolder holder, int a, int b, int c)
    { }

    public void SurfaceCreated(ISurfaceHolder holder)
    {
        try
        {
            mp.SetDisplay(holder);
            mp.SetDataSource(url);
            mp.PrepareAsync();
            mp.Start();
        }
        catch (Java.Lang.IllegalStateException e)
        {
            Toast.MakeText(this, e.ToString(), ToastLength.Short).Show();
        }
    }

    public static string GetMD5Hash(string input)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
        bs = x.ComputeHash(bs);
        System.Text.StringBuilder s = new System.Text.StringBuilder();
        foreach (byte b in bs)
        {
            s.Append(b.ToString("x2").ToLower());
        }
        return s.ToString();
    }

    static public string GetMd5Sum(string str)
    {
        Encoder enc = System.Text.Encoding.Unicode.GetEncoder();
        byte[] unicodeText = new byte[str.Length * 2];
        enc.GetBytes(str.ToCharArray(), 0, str.Length, unicodeText, 0, true);
        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] result = md5.ComputeHash(unicodeText);
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < result.Length; i++)
        {
            sb.Append(result[i].ToString("x2"));
        }
        return sb.ToString();
    }
}
}

由于

1 个答案:

答案 0 :(得分:0)

查看MediaPlayer的Android文档,他们列出了调用PrepareAsync时可能导致IllegalStateException的内容:

http://developer.android.com/reference/android/media/MediaPlayer.html