您的位置:首页 > 移动开发 > Android开发

VideoView视频播放器的再打开

2015-06-05 10:04 543 查看
布局文件只有一个VideoView,不再给出。

/*-------------MainActivity.java-----------*/
package com.example.videoviewtest;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends Activity {

File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/test2.mp4");
if(file.exists()){
videoView.setVideoPath(file.getAbsolutePath());
videoView.setMediaController(mediaController);
mediaController.setMediaPlayer(videoView);
videoView.requestFocus();
}
// 用于判读这个Activity是用户点击app图标启动的,还是通过点击视频文件后弹出的打开方式打开的。
if(getIntent().getData() != null){
videoView.setVideoURI(getIntent().getData());
}
Log.d(TAG, "onCreate");
}

@Override
protected void onStart() {
super.onStart();
Log.d(TAG,"onstart");
};

@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}

@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
};

@Override
protected void onStop(){
super.onStop();
Log.d(TAG, "onStop");
}

@Override
protected void onRestart(){
super.onRestart();
Log.d(TAG, "onRestart");
}
/*当Activity的launchMode为singleTop时,在已经有Activity的情况下,点击app或用它打开视频文件把它显示出来(re-launch),会触发onNewInent()*/
@Override
protected void onNewIntent(Intent intent) {
Log.d(TAG, "onNewIntent");
// 获取文件的uri
videoView.setVideoURI(intent.getData());
super.onNewIntent(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
/**
注意:需要用到sdcard,联网等权限时,先配置manifest,省的出现莫名其妙的错误。
*/
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.videoviewtest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
// 打开后缀为mp4的视频文件。
<intent-filter >
<action  android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="video/mp4"/>
</intent-filter>
</activity>
</application>

</manifest>


启动app后,按home键。再按app,或用它打开一个视频文件都会出现一下信息:

“`


多说几句:1.刚发现关闭屏幕也会onPause(),onStop()

2.在手机的最近任务里重启已存在的Activity,不会触发onNewIntent.

只有onRestart(),onStart(),onResume().
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android开发