安卓TV开发(十) 智能电视开发之在线视频直播


               转载注明出处:http://blog.csdn.net/sk719887916/article/details/46582987

               从<安卓TV开发(八) 移动智能终端多媒体之在线加载网页视频源> 中我总结了怎么去解析一个网页,获取里面数据实现展现,如何去播放视频呢,今天就给大家简单介绍下比较常用视频开源框架vitamio,  项目sdk地址:https://www.vitamio.org/en/  .



             解压vitamio sdk 可以看到有个InitActivity的子模块,可以添加到我们的项目中,当然也可以copy代码到自己的项目中,之后我们播放的Activity继承initActivity即可,当然我们还需要用Vparser包将上篇文章解展现出的视频地址解析成真实地址用来播放,其代码如下。

public class PlayActivity extends Activity {

	private VideoView mVideoView;

	private String mUrl;

	private String path = "";

	private String mRealUrl;
	
	private VParser mVParser;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (!LibsChecker.checkVitamioLibs(this))
			return;
		setContentView(R.layout.activity_one);

	
		mVideoView = (VideoView) findViewById(R.id.surface_view);

		
		mRealUrl = "http://222.73.3.75/vkp.tc.qq.com/c0013ghdds8.mp4?vkey=CBA83E4B773DB0DBB3C5010896C3E4938C700EF47AC30134F2FD8C300E3D57B857508AF5A0418D53&br=66&platform=0&fmt=mp4&level=3";
		mVideoView.setVideoPath(mRealUrl);
		//
		mVideoView.setMediaController(new MediaController(PlayActivity.this));
		mVideoView.requestFocus();

		mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
			@Override
			public void onPrepared(MediaPlayer mediaPlayer) {
				// optional need Vitamio 4.0
				mediaPlayer.setPlaybackSpeed(1.0f);
			}
		});

		 mVParser = new VParser(this);  

		if (getIntent().getStringExtra("url") != null) {

			mUrl = getIntent().getStringExtra("url");
			//mUrl = "http://v.iask.com/v_play_ipad.php?vid=121242141";
			new AsyncTask<Object, Void, Video>() {

				@Override
				protected Video doInBackground(Object... params) {
					return mVParser.parse(String.valueOf(params[0]));
				}

				@Override
				protected void onPostExecute(Video result) {
					super.onPostExecute(result);
					String title = result.title;
					if (result != null ) {
						mRealUrl = result.videoUri;
						Log.i("mPlay", "result.videoUri:" + mRealUrl );
					}
					
					//String website = result.videoSiteUri;
					
					if (mRealUrl == null) {
						mRealUrl = "http://222.73.3.75/vkp.tc.qq.com/c0013ghdds8.mp4?vkey=CBA83E4B773DB0DBB3C5010896C3E4938C700EF47AC30134F2FD8C300E3D57B857508AF5A0418D53&br=66&platform=0&fmt=mp4&level=3";
					}
					mRealUrl = "http://222.73.3.75/vkp.tc.qq.com/c0013ghdds8.mp4?vkey=CBA83E4B773DB0DBB3C5010896C3E4938C700EF47AC30134F2FD8C300E3D57B857508AF5A0418D53&br=66&platform=0&fmt=mp4&level=3";
					mVideoView.setVideoPath(mRealUrl);
					//
					mVideoView.setMediaController(new MediaController(PlayActivity.this));
					mVideoView.requestFocus();

					mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
						@Override
						public void onPrepared(MediaPlayer mediaPlayer) {
							// optional need Vitamio 4.0
							mediaPlayer.setPlaybackSpeed(1.0f);
						}
					});
					
				}

			}.execute(mUrl);
			

		}
		

	}

	@SuppressLint("NewApi")
	private String getVideoRealUrl(String href) {

		Video video = new VParser(this).parse(href);
		// String videoTitle = video.title;

		String mRealUrl = null;
		if (video != null) {
			mRealUrl = video.videoUri;
			if (mRealUrl.isEmpty()) {
				return href;
			} else {
				return mRealUrl;
			}
		}

		return href;
	}

    Layout文件,比较简单,VideoView用 io.vov.vitamio.widget.VideoView,列子如下。

  

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".PlayActivity" >


    <ScrollView
        android:id="@+id/login_form"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            style="@style/LoginFormContainer"
            android:orientation="vertical" >

            <io.vov.vitamio.widget.VideoView
                android:id="@+id/surface_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <Button
                android:id="@+id/sign_in_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="播放" />

            <TextView
                android:id="@+id/email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="正在播出"
                android:maxLines="1"
                android:singleLine="true" />

            <TextView
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint=""
                android:maxLines="1"
                android:singleLine="true" />

            
        </LinearLayout>
    </ScrollView>

</merge>
          


 好了 至此简单的播放功能已完成,从开始的UI搭建,到加载服务器视频数据,到解析展现,到最后的播放,本系列只是简单的完成了整个流程,很多细节需要读者自己研究和完善,如果需要一些特殊宫功能建议自定义videoView,其可以继承SurfaceView implements   MediaController.MediaPlayerControl ,具体代码可参考io.vov.vitamio.widget.VideoView。

    

你可能感兴趣的:(安卓TV开发(十) 智能电视开发之在线视频直播)