2016-06-10 28 views
1

Ich habe ListView, es enthält die Anzahl der Videos. Ich möchte Video spielen, wenn auf bestimmten Video klicken und halten auch wenn Benutzer klicken Sie auf einem anderen Video zur gleichen Listview ohne Anruf jede andere Tätigkeit, habe ich versucht, aber Absturz der AnwendungWie man Video in der gleichen ListView spielt, wenn man auf ein bestimmtes Element klickt

E/ActivityThread: Activity example.com.videolistplaydemo.PlayVideoApi has leaked ServiceConnection [email protected] that was originally bound here 
                      android.app.ServiceConnectionLeaked: Activity example.com.videolistplaydemo.PlayVideoApi has leaked ServiceConnection [email protected] that was originally bound here 
                       at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:974) 
                       at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:868) 
                       at android.app.ContextImpl.bindService(ContextImpl.java:1762) 
                       at android.app.ContextImpl.bindService(ContextImpl.java:1751) 
                       at android.content.ContextWrapper.bindService(ContextWrapper.java:480) 
                       at com.google.android.youtube.player.internal.r.e(Unknown Source) 
                       at com.google.android.youtube.player.YouTubeThumbnailView.initialize(Unknown Source) 
                       at example.com.videolistplaydemo.PlayVideoApi$VideoListAdapter.onBindViewHolder(PlayVideoApi.java:225) 
                       at example.com.videolistplaydemo.PlayVideoApi$VideoListAdapter.onBindViewHolder(PlayVideoApi.java:190) 
                       at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471) 
                       at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504) 
                       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741) 
                       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617) 
                       at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994) 
                       at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390) 
                       at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353) 
                       at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1180) 
                       at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1031) 
                       at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:4061) 
                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791) 
                       at android.view.Choreographer.doCallbacks(Choreographer.java:591) 
                       at android.view.Choreographer.doFrame(Choreographer.java:560) 
                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777) 
                       at android.os.Handler.handleCallback(Handler.java:725) 
                       at android.os.Handler.dispatchMessage(Handler.java:92) 
                       at android.os.Looper.loop(Looper.java:137) 
                       at android.app.ActivityThread.main(ActivityThread.java:5283) 
                       at java.lang.reflect.Method.invokeNative(Native Method) 
                       at java.lang.reflect.Method.invoke(Method.java:511) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
                       at dalvik.system.NativeStart.main(Native Method) 

Mein Code unten ist, Bitte helfen Sie mir !!!

public class PlayVideoApi extends YouTubeBaseActivity implements 
     YouTubePlayer.OnInitializedListener { 
    private RecyclerView listVideo; 
    private ArrayList<NotificationBean> arrTemp; 
    private NotificationBean mNotificationBean; 
    private ArrayList<NotificationBean> mNotificationBeanArrayList = new ArrayList<>(); 
    private VideoListAdapter mAdapter; 
    private static final int RECOVERY_DIALOG_REQUEST = 1; 
    private String KEY = "AIzaSyACDeBPdqn3DlJ-HqvFMrrym2MTYDk7ar0"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.play_video_api); 

     listVideo = (RecyclerView) findViewById(R.id.list); 
     getData(PlayVideoApi.this, 0, "4", 3); 
    } 

    private void getData(Context context, final int posts_per_page, final String type, final int subcategoryid) { 

     RequestQueue queue = Volley.newRequestQueue(context); 
     StringRequest sr = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 
       JSONArray jArray; 
       JSONArray jsonArray = null; 
       JSONObject jsonObject = null; 
       try { 
        jArray = new JSONArray(response); 
        JSONObject jObj = jArray.getJSONObject(0); 
        int code = jObj.getInt("code"); 
        if (code == 0) { 
         Log.d("allcount:: :: ::", String.valueOf(jObj.optInt("allcount"))); 
         if (jObj.has("result")) { 
          jsonArray = jObj.getJSONArray("result"); 
          Log.d("EVENTLIST ARRAY=", jsonArray.length() + ""); 

          if (jsonArray != null && jsonArray.length() > 0) { 

           arrTemp = new ArrayList<>(); 
           for (int i = 0; i < jsonArray.length(); i++) { 

            jsonObject = jsonArray.getJSONObject(i); 
            mNotificationBean = new NotificationBean(); 
            mNotificationBean.id = jsonObject.getString("id"); 
            mNotificationBean.permalink = jsonObject.getString("permalink"); 
            mNotificationBean.image = jsonObject.getString("image"); 
            mNotificationBean.title = jsonObject.getString("title"); 
            mNotificationBean.videotype = jsonObject.getString("videotype"); 
            mNotificationBean.videoid = jsonObject.getString("videoid"); 
            mNotificationBean.desc = jsonObject.getString("desc"); 
            mNotificationBean.author_url = jsonObject.getString("author_url"); 
            mNotificationBean.like = jsonObject.getString("like"); 
            mNotificationBean.unlike = jsonObject.getString("unlike"); 
            mNotificationBean.comment = jsonObject.getInt("comment"); 
            mNotificationBeanArrayList.add(mNotificationBean); 
            Log.v("ArrayList Size:: :: ", Integer.toString(mNotificationBeanArrayList.size())); 

            Log.v("Videoooooooooooooooooooo", mNotificationBean.videoid); 
            listVideo.setHasFixedSize(true); 
            //to use RecycleView, you need a layout manager. default is LinearLayoutManager 
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(PlayVideoApi.this); 
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
            listVideo.setLayoutManager(linearLayoutManager); 
            mAdapter = new VideoListAdapter(PlayVideoApi.this, mNotificationBeanArrayList); 
            listVideo.setAdapter(mAdapter); 
           } 
           Log.v("mUsers Size:: :: ", Integer.toString(mNotificationBeanArrayList.size())); 
          } 
         } 
        } else { 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Toast.makeText(PlayVideoApi.this, "Check internet connection", Toast.LENGTH_LONG).show(); 
      } 
     }) { 

      @Override 

      protected Map<String, String> getParams() { 
       Map<String, String> params = new HashMap<String, String>(); 
       params.put("posts_per_page", Integer.toString(posts_per_page)); 
       params.put("type", type); 
       return params; 
      } 

      @Override 
      public Map<String, String> getHeaders() throws AuthFailureError { 
       Map<String, String> params = new HashMap<String, String>(); 
       params.put("Content-Type", "application/x-www-form-urlencoded"); 
       return params; 
      } 
     }; 
     sr.setRetryPolicy(new DefaultRetryPolicy(
       15000, 
       DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
       DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 

     queue.add(sr); 

    } 

    @Override 
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) { 
     if (!wasRestored) { 

      // loadVideo() will auto play video 
      // Use cueVideo() method, if you don't want to play it automatically 
      youTubePlayer.loadVideo(Config.YOUTUBE_VIDEO_CODE); 

      // Hiding player controls 
      youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS); 
     } 
    } 

    @Override 
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { 
     if (youTubeInitializationResult.isUserRecoverableError()) { 
      youTubeInitializationResult.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show(); 
     } else { 
      String errorMessage = String.format(
        getString(R.string.error_player), youTubeInitializationResult.toString()); 
      Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show(); 
     } 
    } 

    public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.VideoInfoHolder> { 

     //these ids are the unique id for each video 
     //String[] VideoID = {"P3mAtvs5Elc", "nCgQDjiotG0", "P3mAtvs5Elc"}; 
     Context ctx; 
     ArrayList<NotificationBean> mNotificationBeanArrayList; 

     public VideoListAdapter(Context context, ArrayList<NotificationBean> mNotificationBeanArrayList) { 
      this.ctx = context; 
      this.mNotificationBeanArrayList = mNotificationBeanArrayList; 
     } 

     @Override 
     public VideoInfoHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_list_row, parent, false); 
      return new VideoInfoHolder(itemView); 
     } 

     @Override 
     public void onBindViewHolder(final VideoInfoHolder holder, final int position) { 


      final YouTubeThumbnailLoader.OnThumbnailLoadedListener onThumbnailLoadedListener = new YouTubeThumbnailLoader.OnThumbnailLoadedListener() { 
       @Override 
       public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) { 

       } 

       @Override 
       public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) { 
        youTubeThumbnailView.setVisibility(View.VISIBLE); 
        holder.relativeLayoutOverYouTubeThumbnailView.setVisibility(View.VISIBLE); 
       } 
      }; 
      Log.v("Keyyyyyyyyyyyyyy", Config.DEVELOPER_KEY); 
      holder.youTubeThumbnailView.initialize(Config.DEVELOPER_KEY, new YouTubeThumbnailView.OnInitializedListener() { 
       @Override 
       public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) { 

        youTubeThumbnailLoader.setVideo(mNotificationBeanArrayList.get(position).getVideoid()); 
        youTubeThumbnailLoader.setOnThumbnailLoadedListener(onThumbnailLoadedListener); 
       } 

       @Override 
       public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) { 
        //write something for failure 
       } 
      }); 
     } 

     @Override 
     public int getItemCount() { 
      return mNotificationBeanArrayList.size(); 
     } 

     public class VideoInfoHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 

      protected RelativeLayout relativeLayoutOverYouTubeThumbnailView; 
      YouTubeThumbnailView youTubeThumbnailView; 
      protected ImageView playButton; 

      public VideoInfoHolder(View itemView) { 
       super(itemView); 
       playButton = (ImageView) itemView.findViewById(R.id.btnYoutube_player); 
       playButton.setOnClickListener(this); 
       relativeLayoutOverYouTubeThumbnailView = (RelativeLayout) itemView.findViewById(R.id.relativeLayout_over_youtube_thumbnail); 
       youTubeThumbnailView = (YouTubeThumbnailView) itemView.findViewById(R.id.youtube_thumbnail); 
      } 

      @Override 
      public void onClick(View v) { 
//    Bundle args = new Bundle(); 
//    args.putString("URL", videoUrl); 
       Intent intent = YouTubeStandalonePlayer.createVideoIntent((Activity) ctx, Config.DEVELOPER_KEY, String.valueOf(mNotificationBeanArrayList.get(getLayoutPosition()))); 
       ctx.startActivity(intent); 
      } 
     } 
    } 
} 
+0

Ich versuchte https://github.com/danylovolokh/VideoPlayerManager es ändern mit funktioniert gut, um Videos von Assets zu erhalten, aber Wie spielt man Videos mit Url? –

Antwort

0

ich es gelöst https://github.com/danylovolokh/VideoPlayerManager Gerade

public AssetVideoItem(String url, VideoPlayerManager<MetaData> videoPlayerManager, Picasso imageLoader, int imageResource) { 
     super(videoPlayerManager); 
     Url = url; 
     mImageLoader = imageLoader; 
     mImageResource = imageResource; 
    } 

in AssetVideoItem und

public static AssetVideoItem createItemFromUrl(String url, int imageResource, Activity activity, VideoPlayerManager<MetaData> videoPlayerManager) throws IOException { 
     return new AssetVideoItem(url, videoPlayerManager, Picasso.with(activity),imageResource); 
    } 
and use in main class 
mList.add(ItemFactory.createItemFromUrl("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4", R.drawable.video_sample_1_pic, mActivity, mVideoPlayerManager)); 

in ItemFactory

Verwandte Themen