Android两种播放视频的方法(SurfaceView、MediaPlayer、SeekBar)和(VideoView、MediaController)等两种方法。

一、第一种方法

(使用SufaceView\MediaPlayer\SekBar实现视频播放)

activity_main


<LinearLayoutxmlns: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"tools:context="com.example.android_playmusic.MainActivity">
<SeekBarandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:id="@+id/seekbar" /><ImageButtonandroid:layout_width="wrap_content"droid:layout_height="wrap_content"android:src="@android:drawable/ic_media_play"android:onClick="playmusic" />LinearLayout>



MainActivity
 
 

package com.example.android_playmusic;import android.media.MediaPlayer;import android.net.Uri;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;publicclassMainActivityextends AppCompatActivity {private SeekBar seekbar; private MediaPlayer mediaPlayer; private SurfaceView sfv_movie_surface; @OverrideprotectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_movies); seekbar= (SeekBar) findViewById(R.id.); sfv_movie_surface = (SurfaceView) findViewById(R.id.sfv_movie_surface);//给进度条设置滑动监听 seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @OverridepublicvoidonProgressChanged(SeekBar seekBar, int i, boolean b) { } @OverridepublicvoidonStartTrackingTouch(SeekBar seekBar) { } @OverridepublicvoidonStopTrackingTouch(SeekBar seekBar) { int progress = seekBar.getProgress(); //在当前位置播放 mediaPlayer.seekTo(progress); } }); } publicvoidplaymusic(View view) { final ImageButton imageButton = (ImageButton) view; if (mediaPlayer == null) { //01.播放内存中的音频//mediaPlayer = MediaPlayer.create(this, R.raw.f);//播放内存卡中文件 mediaPlayer = new MediaPlayer(); try { mediaPlayer.setDataSource(this,Uri.parse("file://mnt/sdcard/storage/emulated/0/12.mp4"));//准备 mediaPlayer.prepare(); } catch (IOException e) { e.printStackTrace(); } //把捕捉到的画面放到SurfaceView mediaPlayer.setDisplay(sfv_movie_surface.getHolder()); mediaPlayer.start();imageButton.setImageResource(android.R.drawable.ic_media_pause);//获取音乐的总时长int duration = mediaPlayer.getDuration(); //将进度条设置最大值为:音乐的总时长 seekbar.setMax(duration); //启动线程new MyThread().start(); } elseif (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 { @Overridepublicvoidrun() { super.run();while (seekbar.getProgress() <= seekbar.getMax()) { //获取音乐当前播放的位置int position = mediaPlayer.getCurrentPosition(); seekbar.setProgress(position); } } }}

二、第二种方法(使用VideoView、MediaControl)
activity_main
<LinearLayoutxmlns: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"tools:context="com.zking.android_playmusic.MainActivity">

<LinearLayoutxmlns: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"tools:context="com.example.android_playmusic.MainActivity">
<VideoView 
android:layout_width="match_parent"  
android:layout_height="match_parent"  
android:id="@+id/vv_vidao_view"  
/>
LinearLayout>

MainActivity

package com.zking.playmusic;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.MediaController;import android.widget.VideoView;publicclassMediacontrolleActivity extends AppCompatActivity {private VideoView vv_vidao_view; @OverrideprotectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mediacontrolle); vv_vidao_view = (VideoView) findViewById(R.id.vv_vidao_view);//设置播放的来源 vv_vidao_view.setVideoPath("file://mnt/sdcard/storage/emulated/0/12.mp4");//实例化多媒体控制器 MediaController mediaController=new MediaController(this); mediaController.setMediaPlayer(vv_vidao_view); vv_vidao_view.setMediaController(mediaController); }}










 



<LinearLayoutxmlns: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"tools:context="com.example.android_playmusic.MainActivity">
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.MediaController; import android.widget.VideoView; public classMediacontrolleActivity extends AppCompatActivity { private VideoView vv_vidao_view; @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_mediacontrolle); vv_vidao_view = (VideoView) findViewById(R.id.vv_vidao_view); //设置播放的来源 vv_vidao_view.setVideoPath( "file://mnt/sdcard/storage/emulated/0/12.mp4" ); //实例化多媒体控制器 MediaController mediaController= new MediaController( this ); mediaController.setMediaPlayer(vv_vidao_view); vv_vidao_view.setMediaController(mediaController); }}

你可能感兴趣的:(Android,SurfaceView,MediaPlayer,SeekBar,VideoView,MediaController,Android)