1

我有一个从互联网播放视频的应用程序,当只有一个视频播放器(默认播放器)时它可以正常工作,但当有更多视频播放器时它会失败。所以我想做的是在播放视频之前询问要播放的播放器。我找到了这篇文章

已安装媒体播放器的 android-list

但即使我做 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

非常感谢您提前

4

1 回答 1

0

我所做的是通过意图过滤器强制使用媒体播放器播放视频

这是代码

<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>

它适用于我,希望它适用于所有人

于 2013-04-16T10:16:34.980 回答