无从下手MonoDroid的一个媒体播放器流

我有我的代码媒体播放器是不能显示任何视频。 它抛出JavaLangException约一个IllegalStateException当prepareasync()方法被调用。

我环顾四周相当多的过为什么这应该是这种情况,但无法找到在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(); } } }

谢谢

保罗

--------------解决方案-------------

检查出的MediaPlayer Android的文档,他们列出调用PrepareAsync时可能导致IllegalStateException异常的事情:

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

分类:C# 时间:2015-03-15 人气:0
本文关键词: MonoDroid的
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.205 (s). 10 q(s)