android视频播放简单demo

最近在写一个点播的APP,所以就对流媒体播放研究了一下,下面是一个最简单的视频播放代码



1:layout文件

         <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- 上部 -->

        <VideoView
            android:id="@+id/surface_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" />
 


<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_load"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="快进" />

        <Button
            android:id="@+id/btn_play"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="播放" />

        <Button
            android:id="@+id/btn_pause"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="暂停" />
    </LinearLayout>
</LinearLayout>


2:java播放类



import android.app.Activity;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class VideoViewDemo extends Activity {

    private VideoView mVideoView;

    private boolean flag = true;
    private String path = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/33.avi";

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
      

        setContentView(R.layout.videoview);

        Button btn_pause = (Button) findViewById(R.id.btn_pause);
        Button btn_play = (Button) findViewById(R.id.btn_play);
        mVideoView = (VideoView) findViewById(R.id.surface_view);

        path="http://jobsfe.funshion.com/play/v1/mp4/f80af2799774988214b69094a7d56e4a.mp4";

        mVideoView.setVideoPath(path);
        mVideoView.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH);
        mVideoView.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mVideoView.setSubShown(true);
            }
        });
        mVideoView.setMediaController(new MediaController(this));

        mVideoView.requestFocus();

        // 播放按钮
        btn_play.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                mVideoView.start();

            }
        });
        btn_pause.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                mVideoView.pause();
            }
        });
    }

    @Override
    protected void onStop() {
        this.finish();
        super.onStop();
    }

    private int mLayout = VideoView.VIDEO_LAYOUT_ORIGIN;

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (mVideoView != null)
            mVideoView.setVideoLayout(mLayout, 0);
        super.onConfigurationChanged(newConfig);
    }

}



就这样,别忘了 最后在AndroidManifest.xml配置权限问题,一个简单的流媒体播放 就搞定了

   



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值