用videoview对一个路径下的文件随机播放

最近在公司实习做的视频播放,记录一下。直接上代码


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    >
    <VideoView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:id="@+id/video_view"
        />


</FrameLayout>




import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;


import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;


public class MainActivity extends AppCompatActivity {


    private VideoView mVideoView;
    private int mLastPlayedTime;
    private final String LAST_PLAYED_TIME = "LAST_TIME";
    //static int pos = 1;
    private int count = 0;
    private List<String> listFileName = new ArrayList<String>();
    private String path = Environment.getExternalStorageDirectory().getPath() + "/video/";
    Random r = new Random();


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();


        setContentView(R.layout.activity_main);
        mVideoView = (VideoView) findViewById(R.id.video_view);
        MediaController controller = new MediaController(this);
        mVideoView.setMediaController(controller);
        controller.setMediaPlayer(mVideoView);
        getAllFilename(path, listFileName);
        //pos=listFileName.get();
        if (listFileName.get(count) != null) {
            playFristVideo();
            mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override//播放完毕之后触发的方法
                public void onCompletion(MediaPlayer mediaPlayer) {
                    playVideo();
                    if(count==(listFileName.size()-1)){
                        Log.d("MAO", "重新进入第一个视频播放");
                        playFristVideo();
                    }
                    else {
                        playVideo();
                    }
                }
            });
            mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
                    exit();
                    return true;
                }
            });


        }
    }


    private void playFristVideo() {
        mVideoView.setVideoURI(Uri.parse(path+listFileName.get(0)));
        mVideoView.start();
        mVideoView.requestFocus();
    } 
public void playVideo() {
            Toast.makeText(this, listFileName.get(count), Toast.LENGTH_LONG).show();
            Log.d("MAO", listFileName.get(count));
            count = r.nextInt(listFileName.size());
            Uri uri = Uri.parse(path + listFileName.get(count));
            mVideoView.setVideoURI(uri);
            mVideoView.start();
            mVideoView.requestFocus();
        }




    public void getAllFilename(String path, List<String> fileName) {
        File file = new File(path);
        File[] files = file.listFiles();
        String[] names = file.list();
        if (names != null)
            fileName.addAll(Arrays.asList(names));
        for (File a : files) {
            if (a.isDirectory()) {
                getAllFilename(a.getName().toString(), fileName);
            }
        }
    }


    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
            default:
                return super.onOptionsItemSelected(item);
        }


    }


    private void exit() {
        Toast.makeText(this, "播放视频错误", Toast.LENGTH_SHORT).show();
        finish();
    }




    protected void onPause() {
        super.onPause();
        mVideoView.pause();
        mLastPlayedTime = mVideoView.getCurrentPosition();


    }




    protected void onResume() {
        super.onResume();
        mVideoView.start();
        if (mLastPlayedTime > 0) {
            mVideoView.seekTo(mLastPlayedTime);
        }
    }




    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(LAST_PLAYED_TIME, mVideoView.getCurrentPosition());
    }




    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        mLastPlayedTime = savedInstanceState.getInt(LAST_PLAYED_TIME);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值