AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.charleslin.playlivevideo" > <uses-permission android:name="android.permission.INTERNET" > </uses-permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" android:configChanges="orientation|screenSize" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".VideoViewActivity" android:configChanges="orientation|screenSize" > </activity> </application> </manifest>
MainActivity.java
package com.example.charleslin.playlivevideo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the layout from video_main.xml setContentView(R.layout.activity_main); // Locate the button in activity_main.xml button = (Button) findViewById(R.id.MyButton); // Capture button clicks button.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // Start NewActivity.class Intent myIntent = new Intent(MainActivity.this, VideoViewActivity.class); startActivity(myIntent); } }); } }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/MyButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/button" /> </RelativeLayout>
VideoViewActivity.java
package com.example.charleslin.playlivevideo; import android.app.Activity; import android.app.ProgressDialog; import android.media.MediaPlayer; import android.media.MediaPlayer.OnPreparedListener; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.widget.MediaController; import android.widget.VideoView; public class VideoViewActivity extends Activity { // Declare variables ProgressDialog pDialog; VideoView videoview; // Insert your Video URL String VideoURL = "http://iphonelive.rtve.es/LA1_LV3_IPH/bitrate_1.m3u8"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the layout from video_main.xml setContentView(R.layout.videoview_main); // Find your VideoView in your video_main.xml layout videoview = (VideoView) findViewById(R.id.VideoView); // Execute StreamVideo AsyncTask // Create a progressbar pDialog = new ProgressDialog(VideoViewActivity.this); // Set progressbar title pDialog.setTitle("Android Video Streaming Tutorial"); // Set progressbar message pDialog.setMessage("Buffering..."); pDialog.setIndeterminate(false); pDialog.setCancelable(false); // Show progressbar pDialog.show(); try { // Start the MediaController MediaController mediacontroller = new MediaController(VideoViewActivity.this); mediacontroller.setAnchorView(videoview); // Get the URL from String VideoURL Uri video = Uri.parse(VideoURL); videoview.setMediaController(mediacontroller); videoview.setVideoURI(video); } catch (Exception e) { Log.e("Error", e.getMessage()); e.printStackTrace(); } videoview.requestFocus(); videoview.setOnPreparedListener(new OnPreparedListener() { // Close the progress bar and play the video public void onPrepared(MediaPlayer mp) { pDialog.dismiss(); videoview.start(); } }); } }
videoview_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<VideoView
android:id="@+id/VideoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
文章標籤
全站熱搜