Android online video playback

Source: Internet
Author: User

During this time, I always saw online video playback materials and summarized them as follows:

1. You don't have to say anything about playing a local video, whether it's videoview, surfaceview, or mediaplayer.

2. Online playback server video:

A. You can use videoview or surfaceview to play a video in progressive download format.

B. Streaming Media videos can also be used (this is the best and most popular. It's a pity that I have read some materials and I haven't done it yet ...)

The code for implementing progressive download video playback is attached below:

Package CN. itcast. videoplayer;

Import java. Io. file;

Import Android. App. activity;
Import Android. Media. mediaplayer;
Import Android. Media. mediaplayer. onpreparedlistener;
Import Android. OS. Bundle;
Import Android. OS. environment;
Import Android. View. surfaceholder;
Import Android. View. surfaceholder. Callback;
Import Android. View. surfaceview;
Import Android. View. view;
Import Android. widget. edittext;
Import Android. widget. Toast;

Public class mainactivity extends activity {
Private edittext nametext;
Private string path;
Private mediaplayer;
Private surfaceview;
Private Boolean pause;
Private int position;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Mediaplayer = new mediaplayer ();
Nametext = (edittext) This. findviewbyid (R. Id. filename );
Surfaceview = (surfaceview) This. findviewbyid (R. Id. surfaceview );

Surfaceview. getholder (). settype (surfaceholder. surface_type_push_buffers );
Surfaceview. getholder (). setfixedsize (176,144 );
Surfaceview. getholder (). setkeepscreenon (true );
Surfaceview. getholder (). addcallback (New surfacecallback ());
}

Private final class surfacecallback implements callback {
Public void surfacechanged (surfaceholder holder, int format, int width, int height ){
}
Public void surfacecreated (surfaceholder holder ){
If (position> 0 & path! = NULL ){
Play (position );
Position = 0;
}
}
Public void surfacedestroyed (surfaceholder holder ){
If (mediaplayer. isplaying ()){
Position = mediaplayer. getcurrentposition ();
Mediaplayer. Stop ();
}
}
}

@ Override
Protected void ondestroy (){
Mediaplayer. Release ();
Mediaplayer = NULL;
Super. ondestroy ();
}

Public void mediaplay (view v ){
Switch (V. GETID ()){
Case R. Id. playbutton:
String filename = "http://daily3gp.com/vids/family_guy_penis_car.3gp ";
If (filename. startswith ("HTTP ")){
Path = filename;
Play (0 );
} Else {
File file = new file (environment. getexternalstoragedirectory (), filename );
If (file. exists ()){
Path = file. getabsolutepath ();
Play (0 );
} Else {
Path = NULL;
Toast. maketext (this, R. String. filenoexsit, 1). Show ();
}
}

Break;

Case R. Id. pausebutton:
If (mediaplayer. isplaying ()){
Mediaplayer. Pause ();
Pause = true;
} Else {
If (pause ){
Mediaplayer. Start ();
Pause = false;
}
}
Break;

Case R. Id. resetbutton:
If (mediaplayer. isplaying ()){
Mediaplayer. seekto (0 );
} Else {
If (path! = NULL ){
Play (0 );
}
}
Break;
Case R. Id. stopbutton:
If (mediaplayer. isplaying ()){
Mediaplayer. Stop ();
}
Break;
}
}

Private void play (INT position ){
Try {
Mediaplayer. Reset ();
Mediaplayer. setdatasource (PATH );
Mediaplayer. setdisplay (surfaceview. getholder ());
Mediaplayer. Prepare (); // Buffer
Mediaplayer. setonpreparedlistener (New preparelistener (position ));
} Catch (exception e ){
E. printstacktrace ();
}
}

Private final class preparelistener implements onpreparedlistener {
Private int position;

Public preparelistener (INT position ){
This. Position = position;
}

Public void onprepared (mediaplayer MP ){
Mediaplayer. Start (); // play the video
If (position> 0) mediaplayer. seekto (position );
}
}
}



XML code:

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Background = "# ffffff"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/FILENAME"
/>

<Edittext
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: Id = "@ + ID/FILENAME"
/>

<Linearlayout
Android: Orientation = "horizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
>
<Imagebutton
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/play"
Android: Id = "@ + ID/playbutton"
Android: onclick = "mediaplay"
/>
<Imagebutton
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/pause"
Android: Id = "@ + ID/pausebutton"
Android: onclick = "mediaplay"
/>
<Imagebutton
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/RESET"
Android: Id = "@ + ID/resetbutton"
Android: onclick = "mediaplay"
/>
<Imagebutton
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/stop"
Android: Id = "@ + ID/stopbutton"
Android: onclick = "mediaplay"
/>

</Linearlayout>

<Surfaceview
Android: layout_width = "fill_parent"
Android: layout_height = "240dp"
Android: Id = "@ + ID/surfaceview"
/>
</Linearlayout>

Androidmanifest. XML code:

<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "cn. itcast. videoplayer"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
<Activity Android: Name = ". mainactivity"
Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>

</Application>
<Uses-SDK Android: minsdkversion = "5"/>
<! -- Access Internet permissions -->
<Uses-Permission Android: Name = "android. Permission. Internet"/>

</Manifest>

I am writing the URL to death. Otherwise, you can enter the URL through edittext. The video in the URL must be in progressive download format. (You can use code to convert a video to a progressive download format. You can also use quicktime to export the video to a progressive download format)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.