videoview - Play a video on android device -


i'm tring display youtube video on android. googled can't find solution last error:

08-16 12:05:55.950: e/mediaplayer(2418): error (1, -2147483648) 08-16 12:05:56.274: e/mediaplayer(2418): error (1,-2147483648) 

i added manifest file user permission internet:

<?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> 

this 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?");      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 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;             (int = 0; < list.getlength(); i++)             {                 node node = list.item(i);                 if (node != null)                 {                     namednodemap nodemap = node.getattributes();                     hashmap<string, string> maps = new hashmap<string, string>();                     (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("&");                 (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;         }        } } 

and 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> 

on emulator when try run video just: "can't play video." error message. read post blame video format/resolution. if think case kind provide link that's supposed work? error not explanatory of wrong. maybe more skilled me :)

thanks in advance hint.

@bofredo: no, doesn't work on real device.

i found issue. wrote code using hemant vc's code: "can't video play error display" after playing video in android

but realized dom parser doesn't work, @ least me. wrote 2 implementations:

the first based on dom hemant vc's code, , second based on xpath (i think it's simpler these cases).

changing @ [0] method call [1] or [2] mediaplayer switches way url. think dom parser ugly i'll use xpath, maybe readers able improve it. biggest issue xpath understanding how set namespaces. used suraj's code: multiple namespace in xml xpath value

[edit]: code came bigger project there unused imports

import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.util.iterator; import javax.xml.xmlconstants; import javax.xml.namespace.namespacecontext; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import javax.xml.xpath.xpath; import javax.xml.xpath.xpathfactory; 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 com.radarmeup.r; 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 org.w3c.dom.element; import org.xml.sax.inputsource; 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);         }                            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);     }      private class mynamespacecontext implements namespacecontext      {                        public string getnamespaceuri(string prefix)          {             if (prefix == null) {                 throw new illegalargumentexception("no prefix provided!");             } else if (prefix.equals("namespace")) {                 return "http://www.w3.org/2005/atom";             } else if (prefix.equals("media")) {                 return "http://search.yahoo.com/mrss/";             } else if (prefix.equals("gd")) {                 return "http://schemas.google.com/g/2005";             } else if (prefix.equals("yt")) {                 return "http://gdata.youtube.com/schemas/2007";             } else {                 return xmlconstants.null_ns_uri;             }         }          public string getprefix(string namespaceuri)          {             return null;         }          public iterator<?> getprefixes(string namespaceuri)          {             return null;         }     }      private 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             {                    m_movieurl = geturlvideortspbyxpath(m_movieurl);//[0]                                log.e("video url playing =>", m_movieurl);//here must shown url rtsp://something/video.3gp             }             catch (exception e)             {                 log.e("login soap calling in exception", e.tostring());             }             return null;         }          @override         protected void onpostexecute(void result)         {             super.onpostexecute(result);                    m_movieview = (videoview) findviewbyid(r.id.movie);                     m_progressbar = (progressbar) findviewbyid(r.id.progressbar);             m_mc = (mediacontroller) findviewbyid(r.id.mediacontroller);             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();         }          //[1]         public string geturlvideortspbyxpath(string urlyoutube)         {             string ret = "";             try             {                        string gdy = "http://gdata.youtube.com/feeds/api/videos/";                 string id = extractyoutubeid(urlyoutube);                 url url = new url(gdy + id);                 inputsource xml = new inputsource(url.openstream());                  xpath xpath = xpathfactory.newinstance().newxpath();                 xpath.setnamespacecontext(new mynamespacecontext());                 ret = xpath.evaluate("//media:content[@yt:format='1']/@url",xml);                                        }             catch(exception ex)             {                 ret = "";                 log.e("get url video rtsp exception======>>", ex.tostring());             }             return ret;                  }          //[2]         public string geturlvideortspbydom(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();//entry                 nodelist list = el.getelementsbytagname("media:content");///media:content                 node n;                 namednodemap nnm;                 attr a;                 string urlstring = "";                 boolean found = false;                 for(int = 0; i<list.getlength();++i)                 {//loop nodes media:content:                     n = list.item(i);                     if(n != null)                     {                         nnm = n.getattributes();                         for(int j=0;j<nnm.getlength();++j)                         {//loop i-th node's attributes:                             = (attr) nnm.item(j);                             if(a.getname().equalsignorecase("url"))                                 urlstring = a.getvalue();                             if(a.getname().equalsignorecase("yt:format") && a.getvalue().equalsignorecase("1"))                             {                                 found = true;                                 break;                             }                             else if (a.getname().equalsignorecase("yt:format") && !a.getvalue().equalsignorecase("1"))                             {//wrong node => urlstring cleaning:                                 urlstring = "";                             }                            }                         if(found)                         {                             if (urlstring.isempty())                             {//if didn't find url yet have loop again in node's attributes:                                 for(int j=0;j<nnm.getlength();++j)                                 {//loop i-th node's attributes:                                     = (attr) nnm.item(j);                                     if(a.getname().equalsignorecase("url"))                                         urlstring = a.getvalue();                                 }                             }else{                                 urlyoutube = urlstring;                                 break;                             }                         }                     }                 }               }             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("&");                     (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;         }     } } 

i hope can useful else.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -