2016-07-28 149 views
-1

我嘗試獲取YouTube視頻網址的id。這裏有一個這樣的網址的例子:如何從URL使用YouTube獲取YouTube視頻ID

https://www.youtube.com/watch?v=Iz6aR-WATYY 

我怎樣才能得到這個視頻的ID?

這是video_Adapter

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.MyViewHolder> { 

private List<Video> videoList; 
private Context context; 
String videourl; 
Intent intent; 

public class MyViewHolder extends RecyclerView.ViewHolder { 
    public TextView title; 
    public ImageView image, videonextarrowImage; 
    public TextView desc; 
    public TextView videourl; 

    public MyViewHolder(View view) { 
     super(view); 
     title = (TextView) view.findViewById(R.id.video_title); 
     image = (ImageView) view.findViewById(R.id.video_imageView); 
     desc = (TextView) view.findViewById(R.id.video_desc); 
     videourl = (TextView) view.findViewById(R.id.video_url); 
     videonextarrowImage = (ImageView) view.findViewById(R.id.video_NextArrow); 
     view.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent i = new Intent(context, Video_Activity.class); 
       context.startActivity(i); 
      } 
     }); 

    } 
} 


public VideoAdapter(List<Video> videoList, Context context) { 
    this.videoList = videoList; 
    this.context = context; 
} 

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

@Override 
public void onBindViewHolder(MyViewHolder holder, final int position) { 
    final Video video = videoList.get(position); 
    holder.title.setText(video.getTitle()); 
    holder.videourl.setText(video.getVideourl()); 
    //holder.image.setImageResource(R.drawable.more_selector); 
    holder.desc.setText(video.getDesc()); 
    holder.videonextarrowImage.setImageResource(R.drawable.nextbutton); 
    context = holder.image.getContext(); 
    Picasso.with(context).load("http://bitstobyte.in/upload/" + video.getImage()).placeholder(R.drawable.play).error(R.drawable.startyoutubevideo).resize(50, 50).into(holder.image); 

    holder.title.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      String Title = video.getTitle(); 
      String Desc = video.getDesc(); 

      String[] links = new String[]{video.getVideourl()}; 
      String[] ids = new String[links.length]; 
      for (int i = 0; i < links.length; i++) { 
       ids[i] = links[i].split("v=")[1]; 
      } 

      intent = new Intent(context, Video_Activity.class); 
      intent.putExtra("Video_title", Title); 
      intent.putExtra("Vide_desc", Desc); 
      intent.putExtra("Video_ID", ids); 
      context.startActivity(intent); 

      Toast.makeText(context, "You Clicked " + video.getTitle(), Toast.LENGTH_LONG).show(); 

     } 

    }); 
} 

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

}

這是video.java

public class Video extends VideoResponse{ 

public String title; 
public String image; 
public String desc; 
private String videourl; 

public Video(){ 

} 
public Video(String title, String image, String desc,String videourl) { 
    this.title = title; 
    this.image = image; 
    this.videourl = videourl; 
    this.desc = desc; 
} 
public String getTitle() { 
    return title; 
} 

public void setTitle(String name) { 
    this.title = name; 
} 

public String getImage() { 
    return image; 
} 

public void setImage(String image) { 
    this.image = image; 
} 

public String getDesc() { 
    return desc; 
} 

public void setDesc(String desc) { 
    this.desc = desc; 
} 
public String getVideourl() { 
    return videourl; 
} 

public void setVideourl(String videourl) { 
    this.videourl = videourl; 
} 

}

這是video_activity.java

public class Video_Activity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener { 

TextView textView1, textView2; 
ScrollView scrollView; 
WebView webView; 
VideoView videoView; 
private MediaController mController; 
private Uri uriYouTube; 
private YouTubePlayerView youTubeView; 
private static final int RECOVERY_DIALOG_REQUEST = 1; 
Video video; 
public String videourl; 
// Google Console APIs developer key 
// Replace this key with your's 
public static final String DEVELOPER_KEY = "AIzaSyC0rjDLc0jkYY0Z66QgOnkNmkOAyOqpqnI"; 

// YouTube video id 
//public static final String YOUTUBE_VIDEO_CODE = "yExScIwJftE"; 
String[] ids; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_video_); 

    getSupportActionBar().setTitle(Html.fromHtml("<font color='#fdfafa'> Video View </font>")); 

    final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha); 
    getSupportActionBar().setHomeAsUpIndicator(upArrow); 
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    YouTubePlayerSupportFragment frag = (YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(R.id.youtube_view); 
    if (frag == null) { 
     frag = YouTubePlayerSupportFragment.newInstance(); 
     getSupportFragmentManager().beginTransaction().add(R.id.youtube_view, frag).commit(); 
    } 

    frag.initialize(DEVELOPER_KEY, this); 

    Intent intent = getIntent(); 
    if (null != intent) { 
     String Video_Title = intent.getStringExtra("Video_title"); 
     String Video_Desc = intent.getStringExtra("Vide_desc"); 
     ids = intent.getStringArrayExtra("Video_ID"); 
     //Log.i("Video_Url", " " + YouTube_VideoPlayer); 

     textView1 = (TextView) findViewById(R.id.activity_video_Title); 
     textView1.setText(Video_Title); 

     textView2 = (TextView) findViewById(R.id.activity_video_desc); 
     textView2.setText(Video_Desc); 

     scrollView = (ScrollView) findViewById(R.id.Scroller_Id_video); 
     scrollView.post(new Runnable() { 
      @Override 
      public void run() { 
       scrollView.smoothScrollTo(0, textView2.getBottom()); 
      } 
     }); 
    } 
} 

    @Override 
    public void onInitializationFailure (YouTubePlayer.Provider provider,YouTubeInitializationResult errorReason){ 

     if (errorReason.isUserRecoverableError()) { 
      errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show(); 
     } 
     else 
     { 
      //String YouTube_VidePlayer = getIntent().getStringExtra("Video_Player"); 
      String YouTube_VideoPlayer = String.format(getString(R.string.error_player), errorReason.toString()); 
      Toast.makeText(this, YouTube_VideoPlayer, Toast.LENGTH_LONG).show(); 
     } 

    } 
    @Override 
    public void onInitializationSuccess (YouTubePlayer.Provider provider,YouTubePlayer player,boolean wasRestored){ 

     if (!wasRestored) { 
      // loadVideo() will auto play video 
      // Use cueVideo() method, if you don't want to play it automatically 

      //player.cueVideo(YouTube_VideoPlayer); 

      player.cueVideo(String.valueOf(ids)); 

      // Hiding player controls 
      //player.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS); 
     } 
    } 
    @Override 
    protected void onActivityResult (int requestCode, int resultCode, Intent data){ 
     if (requestCode == RECOVERY_DIALOG_REQUEST) { 
      // Retry initialization if user performed a recovery action 
      getYouTubePlayerProvider().initialize(Video_Config.DEVELOPER_KEY, this); 
     } 
    } 
private YouTubePlayer.Provider getYouTubePlayerProvider() { 
    return (YouTubePlayerView) findViewById(R.id.youtube_view); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    if(item.getItemId() == android.R.id.home){ 
     finish(); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

}

+0

簡單地使用正則表達式表達... – appersiano

回答

1

您可以使用String.split(正則表達式)

例如:

String s = "https://www.youtube.com/watch?v=Iz6aR-WATYY"; 
String id = s.split("v=")[1]; 
+0

我從服務器獲取許多視頻鏈接.... –

+0

@srikanthkanth你已經問過如何從字符串中提取id,而不是如何編寫一個循環。 –

+0

PLZ澄清我的疑惑....我從我的應用程序li​​stView中的服務器得到了很多視頻鏈接.......如何獲得身份證,並傳遞到下一個活動 –

相關問題