[我正在使用Xamarin Android开发用于歌曲建议的应用程序,并且正在使用嵌入的youtube url,而我想了解的是为什么某些视频播放正常,而其他视频却显示错误而根本无法播放。错误是“视频不可用”。我找到了说可以将html上传到服务器的解决方案,但是我无法上传它,因为我是动态更改视频的。
谢谢。
protected async override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Window.RequestFeature(Android.Views.WindowFeatures.NoTitle);
SetContentView(Resource.Layout.songSuggestion_layout);
SetViews();
categories = new Categories();
songs = new Songs();
songSuggsted = new Songs();
playlist = new Playlist();
await SetListSongsOfSuggestion();
var metrics = Resources.DisplayMetrics;
//fix video screen height and width
//intDisplayWidth = (FnConvertPixelsToDp(metrics.WidthPixels) + 200);
//intDisplayHeight = (FnConvertPixelsToDp(metrics.HeightPixels)) / (2);
intDisplayHeight = 500;
intDisplayWidth = 1000;
PlayInWebView();
}
public void PlayInWebView()
{
string strUrl = songSuggsted[position].UrlSong;
// string strUrl= "https://www.youtube.com/watch?v=u_VsvZmIWxY";
string id = FnGetVideoID(strUrl);
if (!string.IsNullOrEmpty(id))
{
strUrl = string.Format("http://youtube.com/embed/{0}", id);
}
else
{
Toast.MakeText(this, "Video url is not in correct format", ToastLength.Long).Show();
return;
}
string html = @"<html><body><iframe width=""videoWidth"" height=""videoHeight"" src=""strUrl""></iframe></body></html>";
var myWebView = (WebView)FindViewById(Resource.Id.videoView);
var settings = myWebView.Settings;
settings.JavaScriptEnabled = true;
settings.UseWideViewPort = true;
settings.LoadWithOverviewMode = true;
settings.JavaScriptCanOpenWindowsAutomatically = true;
settings.DomStorageEnabled = true;
settings.SetRenderPriority(WebSettings.RenderPriority.High);
settings.BuiltInZoomControls = false;
settings.JavaScriptCanOpenWindowsAutomatically = true;
myWebView.SetWebChromeClient(new WebChromeClient());
settings.AllowFileAccess = true;
settings.SetPluginState(WebSettings.PluginState.On);
string strYouTubeURL = html.Replace("videoWidth", intDisplayWidth.ToString()).Replace("videoHeight", intDisplayHeight.ToString()).Replace("strUrl", strUrl);
myWebView.LoadDataWithBaseURL(null, strYouTubeURL, "text/html", "UTF-8", null);
}
static string FnGetVideoID(string strVideoURL)
{
const string regExpPattern = @"youtu(?:\.be|be\.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)";
//for Vimeo: vimeo\.com/(?:.*#|.*/videos/)?([0-9]+)
var regEx = new Regex(regExpPattern);
var match = regEx.Match(strVideoURL);
return match.Success ? match.Groups[1].Value : null;
}
int FnConvertPixelsToDp(float pixelValue)
{
var dp = (int)((pixelValue) / Resources.DisplayMetrics.Density);
return dp;
}
[我正在使用Xamarin Android开发用于歌曲建议的应用程序,并且我正在使用嵌入的YouTube网址,而我想了解的是为什么某些视频可以正常播放而其他视频却可以播放...]]
您可以尝试以下代码:
在android布局中创建webview。