Android视频播放(一)

这篇博客介绍了在Android中使用VideoView+MediaController进行视频播放时遇到的问题,即小分辨率视频无法填充整个屏幕。为解决这个问题,作者提供了自定义VideoView的方案,包括5个关键文件:TTPlayer.class、activity_ttplayer.xml、CustomVideoView.class、PlayWindowVideoV.class和activity_play_window_video_v.xml。通过自定义VideoView,可以实现视频的全屏播放效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android 视频播放有三种方法:

1. 调用系统的播放器

2. SurfaceView + MediaPlayer

3. VideoView + MidiaController


每种方法本质是一样的,就是实现的方法有所不同,首先介绍VideoView + MediaController的方法:

先说缺点,如果用系统默认的VideoView, 播放视频的时候,一些小分辨率的视频无法填充到整个窗口,这就导致,屏幕一侧会有一条白边。视频无法填充到整个屏幕。

所以,如果你对这个很在意,那么只能自定义一个VideoView,下面我来介绍如何实现。


整个工程有这么5个文件:

TTPlayer.class        // APK的主界面, 里面之定义了3个按钮,其中一个按钮是用来启动另外一个activity来播放视频,其他2个在本例中没有用。

activity_ttplayer.xml        // TTPlayer.class 的布局

CustomVideoView.class        // 自定义的VidwoView, 用来实现视频的全屏

PlayWindowVideoV.class        //承载视频播放的Activity,我们自定义的VideoView要画在这上面。

activity_play_window_video_v.xml        //PlayWindowVideoV.class 的布局文件

AndroidManifest.xml        //不用说了



下面贴代码:

TTPlayer.class

package com.goafter.testtemp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class TTPlayer extends AppCompatActivity {

    Button btnPlay, btnPause, btnStop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ttplayer);
        btnPlay = (Button) findViewById(R.id.btnPlay);
        btnPause = (Button) findViewById(R.id.btnPause);
        btnStop = (Button) findViewById(R.id.btnStop);
        btnPlay.setOnClickListener(new myclicklistener());
        btnPause.setOnClickListener(new myclicklistener());
        btnStop.setOnClickListener(new myclicklistener());

    }

    class myclicklistener implements View.OnClickListener {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.btnPlay:
                    Intent intent = new Intent(TTPlayer.this, PlayWindow.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("uri", "file:///sdcard/video1.mp4");
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.btnPause:
                    Intent intentv = new Intent(TTPlayer.this, PlayWindowVideoV.class);
                    Bundle bundlev = new Bundle();
                    bundlev.putString("uri", "file:///sdcard/video1.mp4");
                    intentv.putExtras(bundlev);
                    startActivity(intentv);
                    break;
                default:
                    break;
            }
        }
    }

}




activity_ttplayer.xml  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.goafter.testtemp.TTPlayer">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnPlay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SurfaceView" />

        <Button
            android:id="@+id/btnPause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="VideoView" />

        <Button
            android:id="@+id/btnStop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="STOP" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


    </LinearLayout>




</LinearLayout>


CustomVideoView.class ( 这段代码是网上摘抄的)

package com.goafter.testtemp;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.VideoView;


public class CustomVideoView extends VideoView {
    private int mVideoWidth;
    private int mVideoHeight;

    public CustomVideoView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public CustomVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public CustomVideoView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        // Log.i("@@@@", "onMeasure");

        //下面的代码是让视频的播放的长宽是根据你设置的参数来决定

        int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
        int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }
}



PlayWindowVideoV.class
package com.goafter.testtemp;


import android.content.Intent;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;

public class PlayWindowVideoV extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE;
        window.setAttributes(params);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_window_video_v);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        CustomVideoView videoV = (CustomVideoView) findViewById(R.id.videoV);
        videoV.setVideoPath(bundle.getString("uri"));
        MediaController mController = new MediaController(this);
        videoV.setMediaController(mController);
        videoV.start();


    }




}



activity_play_window_video_v.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.goafter.testtemp.PlayWindowVideoV">
    <com.goafter.testtemp.CustomVideoView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/videoV"/>

</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值