我想要在Android上顯示YouTube視頻。我GOOGLE了,但我無法找到任何解決我的最後一個錯誤:在Android設備上播放視頻
08-16 12:05:55.950: E/MediaPlayer(2418): error (1, -2147483648)
08-16 12:05:56.274: E/MediaPlayer(2418): Error (1,-2147483648)
我加入到清單文件的用戶權限互聯網:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myapp.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myapp.MovieActivity"
android:parentActivityName="com.example.myapp.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myapp.MainActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
這是我MovieActivity.java:
package com.example.myapp;
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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.renderscript.Element;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.VideoView;
@SuppressLint("NewApi")
public class MovieActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.movie_activity);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
getActionBar().setDisplayHomeAsUpEnabled(true);
}
ImageView m_avatar = (ImageView) findViewById(R.id.Avatar);
m_avatar.setImageResource(R.drawable.lambda);
TextView m_pagedescription = (TextView) findViewById(R.id.pageDescription);
m_pagedescription.setText("What do you like to do?");
TextView m_nickname = (TextView) findViewById(R.id.nickName);
m_nickname.setText("User");
new MoviePlayer("http://www.youtube.com/watch?v=1FJHYqE0RDg",this).execute();
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void btnSkip_OnClick(View v)
{
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
public class MoviePlayer extends AsyncTask<Void, Void, Void>
{
ProgressBar m_progressBar;
VideoView m_movieView;
String m_movieUrl;
MediaController m_mc;
Context c;
public MoviePlayer(final String url, Context context)
{
c = context;
m_movieUrl = url;
m_progressBar = (ProgressBar) findViewById(R.id.progressBar);
m_movieView = (VideoView) findViewById(R.id.movie);
m_mc = (MediaController) findViewById(R.id.mediaController);
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
m_progressBar.setVisibility(View.VISIBLE);
m_movieView.setVisibility(View.GONE);
}
@SuppressLint("NewApi")
@Override
protected Void doInBackground(Void... params)
{
try
{
if(m_movieUrl.isEmpty())
{
String url = "http://www.youtube.com/watch?v=1FJHYqE0RDg";
m_movieUrl = getUrlVideoRTSP(url);
}
Log.e("Video url for playing=========>>>>>", m_movieUrl);
}
catch (Exception e)
{
Log.e("Login Soap Calling in Exception", e.toString());
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
m_progressBar.setVisibility(View.GONE);
m_movieView.setVisibility(View.VISIBLE);
m_movieView.setVideoURI(Uri.parse(m_movieUrl));
m_mc = new MediaController(c);
m_movieView.setMediaController(m_mc);
m_movieView.requestFocus();
m_movieView.start();
m_mc.show();
}
public 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 = (Element) doc.getDocumentElement();
NodeList list = ((Document) 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 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;
}
}
}
終於這是我movie_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<ImageView
android:id="@+id/Avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@string/Avatar"
android:maxHeight="80dp"
android:maxWidth="80dp"
android:src="@drawable/ic_launcher">
</ImageView>
<TextView
android:id="@+id/nickName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/Avatar"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/Avatar"
android:gravity="center_vertical"
android:maxLength="15"
android:paddingLeft="16dp"
android:text="@string/nicknamePH"
android:textAlignment="gravity"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
<View
android:id="@+id/Ruler1"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="@id/nickName"
android:background="#000000">
</View>
<TextView
android:id="@+id/pageDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/Ruler1"
android:gravity="center_vertical|center_horizontal"
android:text="@string/pageDescription"
android:textAlignment="gravity"
android:textAppearance="?android:attr/textAppearanceSmall">
</TextView>
<View
android:id="@+id/Ruler2"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000000"
android:layout_below="@id/pageDescription">
</View>
<Button
android:id="@+id/Skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="btnSkip_OnClick"
android:text="@string/skipebutton">
</Button>
<TextView
android:id="@+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/Skip"
android:clickable="true"
android:gravity="center_vertical|center_horizontal"
android:linksClickable="true"
android:text="@string/detailspc"
android:textAlignment="gravity"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
<MediaController
android:id="@+id/mediaController"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/details" >
</MediaController>
<FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@id/Ruler2" android:layout_above="@id/mediaController">
<VideoView
android:id="@+id/movie"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</VideoView>
<!--
<ProgressDialog
android:id="@+id/progressDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
</ProgressDialog>
-->
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:contentDescription="@string/progressbar"
android:indeterminate="true" >
</ProgressBar>
</FrameLayout>
</RelativeLayout>
在模擬器上,當我嘗試運行視頻時,我只是:「無法播放此視頻。」錯誤信息。 我看了一些文章,指責視頻格式/分辨率。如果你認爲這是我的情況可能是如此友好提供應該工作的鏈接? 錯誤並不是非常明顯的錯誤解釋。所以也許有人比我更熟練可以幫助:)
在此先感謝您的任何提示。
它運行在真實的設備上嗎?我知道某些設備上運行的服務/小部件在avds上存在問題。 – bofredo