为了账号安全,请及时绑定邮箱和手机立即绑定

Android 实现视频播放

标签:
Android
  1. MainActivity.java
package com.example.videoviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends Activity implements
        MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener,
        MediaPlayer.OnErrorListener {

    private final String TAG = MainActivity.class.getName();

    public VideoView videoView;
    public MediaController mediaController;
    public int videoPosition = 0;
    public ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dialog = new ProgressDialog(this);
        dialog.setTitle("视屏播放器");
        dialog.setMessage("正在加载...");
        dialog.setCancelable(false);

        mediaController = new MediaController(this);
        videoView = (VideoView) findViewById(R.id.videoView);
        videoView.setMediaController(mediaController);

        videoView.setOnCompletionListener(this);
        videoView.setOnPreparedListener(this);
        videoView.setOnErrorListener(this);
    }

    private void loadVideo() {
        Log.d(TAG, "load video");
        dialog.show();
        try {
            //设置视频文件路径
            //videoView.setVideoURI(Uri.parse("android.resource://"+ getPackageName() + "/" + R.raw.bsg));
            videoView.setVideoURI(Uri.parse("file:///sdcard/pxz.mp4"));
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

    //开始播放
    @Override
    protected void onStart() {
        Log.d(TAG, "onStart");
        super.onStart();
        loadVideo();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        Log.d(TAG, "onConfigurationChanged");
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        Log.d(TAG, "Media onCompletion");
        Toast.makeText(MainActivity.this, "播放完成", Toast.LENGTH_LONG)
                .show();
        mp.release();
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        Log.d(TAG, "Media onPrepared");

        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        mp.seekTo(videoPosition);
        if (videoPosition == 0) {
            mp.start();
        } else {
            mp.pause();
        }
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        Log.d(TAG, "Media onError");
        String err = "未知错误";
        switch (what) {
        case MediaPlayer.MEDIA_ERROR_UNKNOWN:
            break;
        case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
            err = "媒体服务终止";
            break;
        default:
            break;
        }
        Toast.makeText(MainActivity.this, err, Toast.LENGTH_LONG).show();
        return true;
    }
}

2 。 activity_main.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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.videoviewdemo.MainActivity" 
    android:orientation="vertical">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal" 
        android:layout_margin="5dp"/>

</LinearLayout>

3.为了实现横屏播放需要在AndroidManifest.xml 文件中设置:

<activity
            android:name=".MainActivity"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
            android:screenOrientation="landscape"
            android:configChanges="orientationscreenSizekeyboardHidden"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

说明:视频资源pxz.mp4 放在sdcard 中

点击查看更多内容
17人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
1.5万
获赞与收藏
8507

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消