Android項目實現(xiàn)視頻播放器
本文實例為大家分享了Android項目實現(xiàn)視頻播放器的具體代碼,供大家參考,具體內(nèi)容如下
VideoView控件是播放視頻用的,借助它可以完成一個簡易的視頻播放器。
①在activity_main.xml中編寫相應(yīng)的控件
<?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:layout_width='match_parent' android:layout_height='match_parent' tools:context='.MainActivity'> <ImageView android: android:layout_width='80dp' android:layout_height='80dp' android:layout_alignParentBottom='true' android:layout_centerHorizontal='true' android:layout_marginBottom='150dp' android:src='http://www.gepszalag.com/bcjs/@android:drawable/ic_media_play' /> <VideoView android: android:layout_width='match_parent' android:layout_height='match_parent' /></RelativeLayout>
②在MainActivity實現(xiàn)SeekBar.OnSeekChangeListener接口與SurfaceHolder.Callback接口,并重寫這兩個接口中對應(yīng)的方法,在這些方法中實現(xiàn)播放視頻的。
package com.example.videoview;import android.media.MediaPlayer;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.ImageView;import android.widget.MediaController;import android.widget.VideoView;import androidx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity implements View.OnClickListener { private VideoView videoView; private MediaController controller; ImageView iv_play; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); videoView = (VideoView) findViewById(R.id.videoview); iv_play = (ImageView) findViewById(R.id.bt_play); //拼出在資源文件夾下的視頻文件路徑String字符串 String url = 'android.resource://' + getPackageName() + '/' + R.raw.video; //字符串解析成Uri Uri uri = Uri.parse(url); //設(shè)置videoview的播放資源 videoView.setVideoURI(uri); //VideoView綁定控制器 controller = new MediaController(this); videoView.setMediaController(controller); iv_play.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_play: play(); break; } } // 播放視頻 private void play() { if (videoView != null && videoView.isPlaying()) { iv_play.setImageResource(android.R.drawable.ic_media_play); videoView.stopPlayback(); return; } videoView.start(); iv_play.setImageResource(android.R.drawable.ic_media_pause); videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { iv_play.setImageResource(android.R.drawable.ic_media_play); } }); }}
③修改清單文件,設(shè)置屬性screenOrientation為橫向。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. .Net Core和RabbitMQ限制循環(huán)消費的方法2. jsp網(wǎng)頁實現(xiàn)貪吃蛇小游戲3. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明4. ASP.NET MVC遍歷驗證ModelState的錯誤信息5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向7. asp中response.write("中文")或者js中文亂碼問題8. PHP設(shè)計模式中工廠模式深入詳解9. CSS hack用法案例詳解10. 將properties文件的配置設(shè)置為整個Web應(yīng)用的全局變量實現(xiàn)方法
