日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android tv box ---- 插入u盘直接播放指定文件夹中的视频

發布時間:2024/3/13 编程问答 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android tv box ---- 插入u盘直接播放指定文件夹中的视频 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

思路:
1.監聽u盤插入廣播
2.遍歷指定目錄下的所有文件,找到視頻文件
3.將符合條件的文件加入播放集合中
4.循環播放
num one:
配置AndroidManifest.xml,注冊一個靜態廣播

<receiver android:name="com.android.app.cus.usbdiskplay.MyUsbReceiver" ><intent-filter><action android:name="android.intent.action.MEDIA_MOUNTED" /><action android:name="android.intent.action.MEDIA_REMOVED" /><action android:name="android.intent.action.MEDIA_EJECT" /><data android:scheme="file" /></intent-filter></receiver>

num two:
實現廣播接收者

public class MyUsbReceiver extends BroadcastReceiver {private String TAG="MyUsbReceiver";ArrayList<String> as = new ArrayList<String>();private boolean isMovieSuffix(Context context,String fileName) {//判斷是否是視頻文件String name=fileName.toLowerCase();String[] suffixs = context.getResources().getStringArray(R.array.video_type_suffix);for (String string : suffixs) {if (name.endsWith(string)) {return true;}}return false;}/*** @param action get the receiver* @param uri get the usb's uri* @param testMovieName get the test movie's name*/@Overridepublic void onReceive(Context context, Intent intent) {final Context mContext = context;String action = intent.getAction();Uri uri = intent.getData();final String path = uri.getPath();final String featureFilePath=path+"/"+context.getResources().getString(R.string.feature_file_name);if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {Log.d(TAG,"~~~~~~~~~~~~~~~~~~~media mounted 111111");new Thread() {public void run() {File file = new File(featureFilePath);if (file.exists()&&file.isDirectory()) {String[] files = file.list(); for(int i = 0;i<files.length;i++){String s = files[i];if(isMovieSuffix(mContext,s)){as.add(featureFilePath+"/"+s);}} Intent intent=new Intent(mContext, MainActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.putStringArrayListExtra("ff",as);mContext.startActivity(intent);}else{Log.d(TAG, featureFilePath+" is not exist.");}}}.start();}}} array.xml<string-array name="video_type_suffix"><item>.rmvb</item><item>.mp4</item><item>.avi</item><item>.wmv</item><item>.mkv</item><item>.m2ts</item><item>.3gp</item><item>.rm</item></string-array>

num three:
使用videoview播放as中存放的視頻。
—1

main.xml 就一個videoView控件<VideoView android:id="@+id/videoView1"android:layout_width="match_parent"android:layout_height="match_parent" /> mainactivity.javapublic class MainActivity extends Activity implements OnCompletionListener,OnErrorListener {private final String TAG="MainActivity";private VideoView videoView;private ArrayList<String> as;private Intent intent;private int i=0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);intent = getIntent();as = intent.getStringArrayListExtra("ff");requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);initView();}private void initView() {if (as != null) {videoView = (VideoView) findViewById(R.id.videoView1);videoView.setMediaController(new MediaController(this));videoView.setOnCompletionListener(this);videoView.setOnErrorListener(this);videoView.setVideoURI(Uri.parse(as.get(0)));videoView.start(); }}@Overridepublic void onCompletion(MediaPlayer arg0) {//循環播放處理Log.d(TAG, "onCompletion");i++;if(i<as.size()){videoView.setVideoURI(Uri.parse((as.get(i))));videoView.start();}else if(i == as.size()){i = 0;videoView.setVideoURI(Uri.parse((as.get(i))));videoView.start();}}@Overridepublic boolean onError(MediaPlayer mediaPlayer, int arg1, int arg2) {Log.d(TAG, "onError");mediaPlayer.pause();mediaPlayer.stop();finish();return false;}@Overrideprotected void onStop() {Log.d(TAG, "onStop");videoView.pause();super.onStop();}}

so easy!!!!

總結

以上是生活随笔為你收集整理的android tv box ---- 插入u盘直接播放指定文件夹中的视频的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。