android开发中 自己播放器 MP4文件打开方式选择自己开发app实现步骤。红色重要具体实现方法
/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. */package activiity.date.gfonda.com.fonda;import android.app.Activity;import android.app.AlertDialog;import android.content.Intent;import android.database.Cursor;import android.media.MediaPlayer;import android.net.Uri;import android.os.Bundle;import android.provider.MediaStore;import android.widget.MediaController;import android.widget.VideoView;/* * MainActivity class that loads MainFragment */public class MainActivity extends Activity { /** * Called when the activity is first created. */ private String videopath; private VideoView videoview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mainactivity); videoview=findViewById(R.id.videoView2); Intent intent = getIntent(); String action = intent.getAction();//action String type = intent.getType();//类型 if (Intent.ACTION_VIEW.equals(action) && type != null && "video/mp4".equals(type)) { // Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM); //如果是媒体类型需要从数据库获取路径 //String filePath=getRealPathFromURI(uri); Uri uri = intent.getData(); String str = getRealPathFromURI(uri);// AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);// builder.setTitle("确认" ) ;// builder.setMessage(str) ;// builder.setPositiveButton("是" , null );// builder.show(); videoview.setVideoPath(str); }else { videopath="file:///android_asset/sp.mp4"; //videoview.setVideoPath(videopath); String uris = "android.resource://" + getPackageName() + "/" + R.raw.sp; // String uri=""; videoview.setVideoPath(uris); } videoview.requestFocus(); //创建MediaController对象 MediaController mediaController = new MediaController(this); videoview.setMediaController(mediaController); videoview.start(); //监听视频播放完的代码 videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mPlayer) { // TODO Auto-generated method stub mPlayer.start(); mPlayer.setLooping(true); } }); } /** * 通过Uri获取文件在本地存储的真实路径 */ private String getRealPathFromURI(Uri contentUri) { String[] proj = {MediaStore.MediaColumns.DATA}; Cursor cursor=getContentResolver().query(contentUri, proj, null, null, null); if(cursor.moveToNext()){ return cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)); } cursor.close(); return null; }}