我有一个从互联网播放视频的应用程序,当只有一个视频播放器(默认播放器)时,它可以正常工作,但是当有更多视频播放器失败时。所以我想要做的就是在播放视频之前询问播放器的播放情况。我找到了这篇文章
“android-list of installed media players”
但即使我做PackageManager packageManager = getPackageManager();获取应用程序失败的实例
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
videoView = (VideoView) findViewById(R.id.videoView);
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(MediaStore.Video.Media.INTERNAL_CONTENT_URI,"1");
intent.setData(uri);
List<ResolveInfo> playerList;
PackageManager packageManager = getPackageManager();
playerList = packageManager.queryIntentActivities(intent, 0);
////////recoger variables de principal
Bundle extras = getIntent().getExtras();
String url = extras.getString("videoURL");
if(playerList==null){
PlayVideo(url);
}
}
private void PlayVideo(String videoURL){
try{
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(Video.this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(videoURL);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.setOnPreparedListener(new OnPreparedListener(){
public void onPrepared(MediaPlayer mp){
mp.setScreenOnWhilePlaying(true);
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
progressDialog.dismiss();
videoView.start();
}
});
}
catch(Exception e){
progressDialog.dismiss();
System.out.println("Video Play Error :"+e.toString());
finish();
}
}
这是logCat
04-11 10:58:02.309: E/AndroidRuntime(11816): FATAL EXCEPTION: main
04-11 10:58:02.309: E/AndroidRuntime(11816): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.playvideo/com.example.playvideo.Video}: java.lang.IllegalStateException: Unknown URL
04-11 10:58:02.309: E/AndroidRuntime(11816): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-11 10:58:02.309: E/AndroidRuntime(11816): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-11 10:58:02.309: E/AndroidRuntime(11816): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-11 10:58:02.309: E/AndroidRuntime(11816): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-11 10:58:02.309: E/AndroidRuntime(11816): at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 10:58:02.309: E/AndroidRuntime(11816): at android.os.Looper.loop(Looper.java:130)
04-11 10:58:02.309: E/AndroidRuntime(11816): at android.app.ActivityThread.main(ActivityThread.java:3687)
04-11 10:58:02.309: E/AndroidRuntime(11816): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 10:58:02.309: E/AndroidRuntime(11816): at java.lang.reflect.Method.invoke(Method.java:507)
04-11 10:58:02.309: E/AndroidRuntime(11816): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-11 10:58:02.309: E/AndroidRuntime(11816): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-11 10:58:02.309: E/AndroidRuntime(11816): at dalvik.system.NativeStart.main(Native Method)
04-11 10:58:02.309: E/AndroidRuntime(11816): Caused by: java.lang.IllegalStateException: Unknown URL
04-11 10:58:02.309: E/AndroidRuntime(11816): at android.os.Parcel.readException(Parcel.java:1330)
04-11 10:58:02.309: E/AndroidRuntime(11816): at android.os.Parcel.readException(Parcel.java:1276)
04-11 10:58:02.309: E/AndroidRuntime(11816): at android.app.ActivityManagerProxy.getProviderMimeType(ActivityManagerNative.java:2843)
04-11 10:58:02.309: E/AndroidRuntime(11816): at android.content.ContentResolver.getType(ContentResolver.java:215)
04-11 10:58:02.309: E/AndroidRuntime(11816): at android.content.Intent.resolveType(Intent.java:3268)
04-11 10:58:02.309: E/AndroidRuntime(11816): at android.content.Intent.resolveTypeIfNeeded(Intent.java:3290)
04-11 10:58:02.309: E/AndroidRuntime(11816): at android.app.ContextImpl$ApplicationPackageManager.queryIntentActivities(ContextImpl.java:2112)
04-11 10:58:02.309: E/AndroidRuntime(11816): at com.example.playvideo.Video.onCreate(Video.java:43)
04-11 10:58:02.309: E/AndroidRuntime(11816): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-11 10:58:02.309: E/AndroidRuntime(11816): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-11 10:58:02.309: E/AndroidRuntime(11816): ... 11 more
非常感谢您提前
答案 0 :(得分:0)
我所做的是强制通过intent-filter
与媒体播放器播放视频这是代码
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="video/*" />
<data android:mimeType="application/sdp" />
<data android:pathPattern=".*3gp" />
<data android:pathPattern=".*3GP" />
<data android:pathPattern=".*mp4" />
<data android:pathPattern=".*MP4" />
</intent-filter>
它对我有用,希望它适用于所有