Android 中播放内存中视频

Android播放内存中视频有两种方法:
1.Android中自带的mediaController(媒体控制器)
Android 中播放内存中视频_第1张图片

VideoViewActivity.java

package com.example.videodisplay;

import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoViewActivity extends AppCompatActivity {

    private VideoView vv_video_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_view);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        vv_video_view = (VideoView) findViewById(R.id.vv_video_view);

        //设置播放的来源
        vv_video_view.setVideoPath("file://mnt/sdcard/360Video/sister.mp4");


        //实例化媒体控制器
        MediaController mediaController=new MediaController(this);

        mediaController.setMediaPlayer(vv_video_view);
        vv_video_view.setMediaController(mediaController);
    }



}

activity_video_view.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"
    >


    <VideoView
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:id="@+id/vv_video_view"
        />


LinearLayout>

2.利用mediaPlayer和SurfaceView实现视频播放
Android 中播放内存中视频_第2张图片

MainActivity.java

package com.example.videodisplay;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private MediaPlayer mediaPlayer;
    private SeekBar sb_main_bar;
    private SurfaceView sfv_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sb_main_bar = (SeekBar) findViewById(R.id.sb_main_bar);
        sfv_1 = (SurfaceView) findViewById(R.id.sfv_1);



        //给进度条设置滑动的监听
        sb_main_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress=seekBar.getProgress();
                //在当前位置播放
                mediaPlayer.seekTo(progress);
            }
        });


    }

    public void isPlayOrPause(View view){
        final ImageButton imageButton= (ImageButton) view;

        if(mediaPlayer==null){
            //播放内存中音频
            //mediaPlayer = MediaPlayer.create(this, R.raw.f);

            //播放内存卡中文件
            mediaPlayer=new MediaPlayer();

            //设置音源
            try {
                mediaPlayer.setDataSource(this, Uri.parse("file://mnt/sdcard/360Video/advideo.mp4"));
//                mediaPlayer.setDataSource(this, Uri.parse("http://193.168.4.135/internet.mp3"));

                //准备
                mediaPlayer.prepare();

                //mediaPlayer.prepareAsync();

            } catch (IOException e) {
                e.printStackTrace();
            }
            //设置准备完成的监听
//            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//                @Override
//                public void onPrepared(MediaPlayer mediaPlayer) {
            //下载操作(复制)

            //将媒体播放器捕捉的画面展示到SurfaceView
            mediaPlayer.setDisplay(sfv_1.getHolder());


            mediaPlayer.start();
            imageButton.setImageResource(android.R.drawable.ic_media_pause);

            //获取音乐的总时长
            int duration=mediaPlayer.getDuration();
            //将进度条设置最大值为:音乐的总时长
            sb_main_bar.setMax(duration);

            new MyThread().start();
//                }
//            });

        }else if(mediaPlayer.isPlaying()){
            mediaPlayer.pause();
            imageButton.setImageResource(android.R.drawable.ic_media_play);
        }else{
            mediaPlayer.start();
            imageButton.setImageResource(android.R.drawable.ic_media_pause);
        }
    }

    class MyThread extends Thread{
        @Override
        public void run() {
            super.run();
            while(sb_main_bar.getProgress()<=sb_main_bar.getMax()){
                //获取音乐当前播放的位置
                int position=mediaPlayer.getCurrentPosition();
                sb_main_bar.setProgress(position);
            }
        }
    }
}

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    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"
    android:orientation="vertical"
    tools:context="com.example.videodisplay.MainActivity">

    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/sfv_1"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <SeekBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/sb_main_bar"
            />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@android:drawable/ic_media_play"
            android:onClick="isPlayOrPause"
            />
    LinearLayout>
LinearLayout>

最后必须要加上读取内存的权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

你可能感兴趣的:(Android)