gpt4 book ai didi

android - "can' t video play error display"在android中播放视频后

转载 作者:搜寻专家 更新时间:2023-11-01 08:56:50 25 4
gpt4 key购买 nike

在我的代码中,在视频查看器中显示 youtube 视频。

我已成功完成,但视频结束时显示对话框“无法播放视频”

这里是我的代码

package com.example.videodemo;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends Activity {

private VideoView videoViewer;
private ProgressBar mProgressBar;
String videoUrl ="http://podcast.20min-tv.ch/podcast/20min/199733.mp4";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

videoViewer=(VideoView)findViewById(R.id.videoView1);
//videoViewer.setVideoPath("/sdcard/documentariesandyou.mp4");


/* Uri uri=Uri.parse(path1);
http://www.youtube.com/watch?v=Hxy8BZGQ5Jo
videoViewer.setVideoURI(uri); */


mProgressBar = (ProgressBar) findViewById(R.id.Progressbar);
mProgressBar.setProgress(0);
mProgressBar.setMax(100);
mProgressBar.setVisibility(View.INVISIBLE);

videoViewer.setOnCompletionListener(new OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {

Toast.makeText(MainActivity.this, "End Video", Toast.LENGTH_LONG).show();
}
});

new YourAsyncTask().execute();

}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();

Toast.makeText(MainActivity.this, "Pause Video", Toast.LENGTH_LONG).show();

}

@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();

Toast.makeText(MainActivity.this, "Stop Video", Toast.LENGTH_LONG).show();
}

@Override
protected void onDestroy() {
super.onDestroy();
//Toast.makeText(MainActivity.this, "Destroy Video", Toast.LENGTH_LONG).show();
/*WifiManager wifiManager = null;
wifiManager.disconnect();*/

}
private class YourAsyncTask extends AsyncTask<Void, Void, Void>
{
ProgressDialog progressDialog;

@Override
protected void onPreExecute()
{
super.onPreExecute();
progressDialog = ProgressDialog.show(MainActivity.this, "", "Loading Video wait...", true);
}

@Override
protected Void doInBackground(Void... params)
{
try
{
String url = "https://www.youtube.com/watch?v=SyrO83x7g-E";
videoUrl = getUrlVideoRTSP(url);
Log.e("Video url for playing=========>>>>>", videoUrl);
}
catch (Exception e)
{
Log.e("Login Soap Calling in Exception", e.toString());
}
return null;
}

@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
try {
progressDialog.dismiss();

videoViewer.setVideoURI(Uri.parse(videoUrl));
MediaController mc = new MediaController(MainActivity.this);
//videoViewer.setMediaController(mc);
videoViewer.requestFocus();
videoViewer.start();
mc.show();
} catch (Exception e) {

e.printStackTrace();
}
}

}

public static String getUrlVideoRTSP(String urlYoutube)
{
try
{
String gdy = "http://gdata.youtube.com/feeds/api/videos/";
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
String id = extractYoutubeId(urlYoutube);
URL url = new URL(gdy + id);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
Document doc = documentBuilder.parse(connection.getInputStream());
Element el = doc.getDocumentElement();
NodeList list = el.getElementsByTagName("media:content");///media:content
String cursor = urlYoutube;
for (int i = 0; i < list.getLength(); i++)
{
Node node = list.item(i);
if (node != null)
{
NamedNodeMap nodeMap = node.getAttributes();
HashMap<String, String> maps = new HashMap<String, String>();
for (int j = 0; j < nodeMap.getLength(); j++)
{
Attr att = (Attr) nodeMap.item(j);
maps.put(att.getName(), att.getValue());
}
if (maps.containsKey("yt:format"))
{
String f = maps.get("yt:format");
if (maps.containsKey("url"))
{
cursor = maps.get("url");
}
if (f.equals("1"))
return cursor;
}
}
}
return cursor;
}
catch (Exception ex)
{
Log.e("Get Url Video RTSP Exception======>>", ex.toString());
}
return urlYoutube;

}

protected static String extractYoutubeId(String url) throws MalformedURLException
{
String id = null;
try
{
String query = new URL(url).getQuery();
if (query != null)
{
String[] param = query.split("&");
for (String row : param)
{
String[] param1 = row.split("=");
if (param1[0].equals("v"))
{
id = param1[1];
}
}
}
else
{
if (url.contains("embed"))
{
id = url.substring(url.lastIndexOf("/") + 1);
}
}
}
catch (Exception ex)
{
Log.e("Exception", ex.toString());
}
return id;
}
}

最佳答案

使用该方法提取videoid

public String extractYoutubeId(String videoUrl) {

String video_id = "";
if (videoUrl != null && videoUrl.trim().length() > 0) {

String expression = "^.*((youtu.be" + "\\/)" + "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*";
CharSequence input = videoUrl;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
String groupIndex1 = matcher.group(7);
if (groupIndex1 != null && groupIndex1.length() == 11)
video_id = groupIndex1;
}
}

return video_id;

}

UPDATE 再添加一个监听器

videoview.setOnErrorListener(new OnErrorListener() {

@Override
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
// hadle the error
return true;
}
});

关于android - "can' t video play error display"在android中播放视频后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18417296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com