Android多媒體教程之播放視頻的四種方法
本文主要給大家介紹的是關(guān)于Android播放視頻的四種方法,分享出來供大家參考學(xué)習(xí),下面來一起看看詳細(xì)的介紹:
一、通過intent的方式,調(diào)用系統(tǒng)自帶的播放器
Uri uri = Uri.parse("/storage/emulated/0/DCIM/Camera/20170521_200117.mp4");
//調(diào)用系統(tǒng)自帶的播放器
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "/storage/emulated/0/DCIM/Camera/20170521_200117.mp4");
startActivity(intent);
二、使用VideoView
布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_video_play_by_vv" android:layout_width="match_parent" android:layout_height="match_parent"> <VideoView android:id="@+id/video_view" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>
Activity
public class VideoPlayByVVActivity extends AppCompatActivity {
private VideoView mVideoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉 title
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //設(shè)置全屏
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); //設(shè)置屏幕常亮
setContentView(R.layout.activity_video_play_by_vv);
mVideoView = (VideoView) findViewById(R.id.video_view);
init();
}
private void init() {
String path = "/storage/emulated/0/DCIM/Camera/20170521_200117.mp4";
Uri uri = Uri.parse(path);
mVideoView.setVideoPath(path);
mVideoView.start();
mVideoView.requestFocus();
}
}
三、MediaPlayer + SurfaceView
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_video_play_by_sur" android:layout_width="match_parent" android:layout_height="match_parent"> <SurfaceView android:id="@+id/surface_view" android:layout_width="180dp" android:layout_height="wrap_content"/> <LinearLayout android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="40dp"> <Button android:id="@+id/stop" android:text="stop" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <Button android:id="@+id/play" android:text="play" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <Button android:id="@+id/pasue" android:text="pasue" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> </LinearLayout> </RelativeLayout>
Activity
public class VideoPlayBySurActivity extends AppCompatActivity implements View.OnClickListener {
private SurfaceView mSurfaceView;
private MediaPlayer mMediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_play_by_sur);
mSurfaceView = (SurfaceView) findViewById(R.id.surface_view);
findViewById(R.id.stop).setOnClickListener(this);
findViewById(R.id.pasue).setOnClickListener(this);
findViewById(R.id.play).setOnClickListener(this);
init();
}
private void init() {
mMediaPlayer = new MediaPlayer();
mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
play();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.stop:
stop();
break;
case R.id.play:
if(!mMediaPlayer.isPlaying()){
play();
}
break;
case R.id.pasue:
pasue();
break;
}
}
public void stop(){
if(mMediaPlayer.isPlaying()){
mMediaPlayer.stop();
}
}
public void pasue(){
if(mMediaPlayer.isPlaying()){
mMediaPlayer.pause();
}else{
mMediaPlayer.start();
}
}
public void play(){
String path = "/storage/emulated/0/DCIM/Camera/20170521_200117.mp4";
try {
mMediaPlayer.reset();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//設(shè)置需要播放的視頻
mMediaPlayer.setDataSource(this, Uri.parse(path));
//把視頻畫面輸出到SurfaceView
mMediaPlayer.setDisplay(mSurfaceView.getHolder());
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
四、 MediaPlayer + TextureView
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_video_play_by_textrue_view" android:layout_width="match_parent" android:layout_height="match_parent"> <TextureView android:id="@+id/texture_view" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:id="@+id/video_image" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/all_darkbackground"/> </RelativeLayout>
Activity
public class VideoPlayByTextrueViewActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener, MediaPlayer.OnInfoListener, MediaPlayer.OnBufferingUpdateListener {
private TextureView mTextureView;
private ImageView mImageVideo;
private Surface mSurface;
private MediaPlayer mMediaPlayer;
private static String path = "/storage/emulated/0/DCIM/Camera/20170521_200117.mp4";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_play_by_textrue_view);
mTextureView = (TextureView) findViewById(R.id.texture_view);
mImageVideo = (ImageView) findViewById(R.id.video_image);
init();
}
private void init() {
mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
mSurface = new Surface(surfaceTexture);
Log.e("tag", "---- onSurfaceTextureAvailable");
play();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
Log.e("tag", "---- onSurfaceTextureSizeChanged");
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mTextureView=null;
mSurface=null;
mMediaPlayer.stop();
mMediaPlayer.release();
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
});
}
public void play(){
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(getApplicationContext(), Uri.parse(path));
mMediaPlayer.setSurface(mSurface);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnInfoListener(this);
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onPrepared(MediaPlayer mp) {
mImageVideo.setVisibility(View.GONE);
mMediaPlayer.start();
}
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
return false;
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
}
}
參考文章
Android 5.0(Lollipop)中的SurfaceTexture,TextureView, SurfaceView和GLSurfaceView
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- android使用surfaceview+MediaPlayer播放視頻
- Android ViewPager中顯示圖片與播放視頻的填坑記錄
- Android中使用TextureView播放視頻
- Android編程實(shí)現(xiàn)播放視頻時(shí)切換全屏并隱藏狀態(tài)欄的方法
- Android仿搜狐視頻、微視等列表播放視頻功能
- Android編程實(shí)現(xiàn)播放視頻的方法示例
- Android 播放視頻常見問題小結(jié)
- Android DragVideo實(shí)現(xiàn)播放視頻時(shí)任意拖拽的方法
- Android仿新浪微博/QQ空間滑動(dòng)自動(dòng)播放視頻功能
- android surfaceView實(shí)現(xiàn)播放視頻功能
相關(guān)文章
Android內(nèi)存泄漏檢測(cè)工具LeakCanary
在Android的性能優(yōu)化中,內(nèi)存優(yōu)化是必不可少的點(diǎn),而內(nèi)存優(yōu)化最重要的一點(diǎn)就是解決內(nèi)存泄漏的問題,在Android的內(nèi)存泄漏分析工具也不少,比如PC端的有:AndroidStudio自帶的Android?Profiler、MAT等工具;手機(jī)端也有,就是我們今天要介紹的LeakCanary2023-04-04
Android編程判斷網(wǎng)絡(luò)是否可用及調(diào)用系統(tǒng)設(shè)置項(xiàng)的方法
這篇文章主要介紹了Android編程判斷網(wǎng)絡(luò)是否可用及調(diào)用系統(tǒng)設(shè)置項(xiàng)的方法,涉及Android針對(duì)網(wǎng)絡(luò)連接的判定及屬性設(shè)置的調(diào)用,需要的朋友可以參考下2016-03-03
Android4.4 訪問外部存儲(chǔ)詳解及實(shí)例
這篇文章主要介紹了Android4.4 訪問外部存儲(chǔ)詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android 使用地圖時(shí)的權(quán)限請(qǐng)求方法
今天小編就為大家分享一篇Android 使用地圖時(shí)的權(quán)限請(qǐng)求方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Android高手進(jìn)階教程(二十二)之Android中幾種圖像特效處理的集錦匯總!!
本篇文章主要介紹了Android中幾種圖像特效處理,比如圓角,倒影,還有就是圖片縮放,Drawable轉(zhuǎn)化為Bitmap,Bitmap轉(zhuǎn)化為Drawable等,有需要的可以了解一下。2016-11-11
android開機(jī)自啟動(dòng)原理與實(shí)現(xiàn)案例(附源碼)
完成一下步驟后,啟動(dòng)一次程序,完成注冊(cè)。等下次手機(jī)開機(jī)時(shí),該軟件即會(huì)自動(dòng)啟動(dòng),具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06
android Socket實(shí)現(xiàn)簡(jiǎn)單聊天功能以及文件傳輸
這篇文章主要介紹了android Socket實(shí)現(xiàn)簡(jiǎn)單聊天功能以及文件傳輸,非常具有實(shí)用價(jià)值,有需要的朋友可以參考下。2017-02-02

