Before play video ask about the video player to play

*爱你&永不变心* 提交于 2019-12-20 04:42:08

问题


I have an app that plays video from internet, it works fine when there´s only one video player (the default one) but when there are more it fails. So what I want to do is before play the video ask about the player to play. I found this post

"android-list of installed media players"

but even if I do PackageManager packageManager = getPackageManager(); to get the instance the app fails

This is the code

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();
         } 

    }

And this is the 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

Thank you very much in advance


回答1:


What I did is to force to play de video with media player via intent-filter

this is the code

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

It works for me and hope it works for all



来源:https://stackoverflow.com/questions/15944631/before-play-video-ask-about-the-video-player-to-play

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!