how to upload a video to youtube from my app andro

2019-08-30 12:38发布

问题:

I am trying to upload video to youtube from my app. I found many references to do this but all those first open youtube app on device then uploads video form that app and I want to upload directly without opening youtube app.But the references were shown as webview opens through passing the url of youtube.so if any one have idea about this please help me guys.

any simple sample code for this because end time of project.

回答1:

Here is the code which helped me please try it will be useful to you

public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
    Intent intent;
    private static final int SELECT_VIDEO_REQUEST = 1000;
//  static private final String DEVELOPER_KEY = " ";
    static private final String DEVELOPER_KEY ="YOUR API KEY";
    static private final String VIDEO_ID = "qVIwHGI2e1U";
    //static private final String VIDEO_ID ="";
    @Override     
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
    youTubeView.initialize(DEVELOPER_KEY, this);
    Button click = (Button) findViewById(R.id.button2);
     click.setOnClickListener(onClickListener);
    }
   private OnClickListener onClickListener = new OnClickListener() {

                 @Override
                 public void onClick(View v) {
                     intent = new Intent(android.content.Intent.ACTION_PICK);
                     intent.setType( "video/*");
                       startActivityForResult(intent,SELECT_VIDEO_REQUEST);     
                 }};
                @Override
                public void onInitializationFailure(Provider provider, YouTubeInitializationResult error) {
                    Toast.makeText(this, "Oh no! "+error.toString(), Toast.LENGTH_LONG).show();
                }

                @Override
                public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
                    player.loadVideo("VIDEO_ID");

                    //player.loadVideo("");
                }




@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent returnedIntent) {
    if (resultCode == RESULT_OK) {
      switch (requestCode) {
        case SELECT_VIDEO_REQUEST:
          Intent intent = YouTubeIntents.createUploadIntent(this, returnedIntent.getData());
          startActivity(intent);
          break;
      }
    }
    super.onActivityResult(requestCode, resultCode, returnedIntent);
  }

public static String getVideoId() {
    return VIDEO_ID;
}}


回答2:

YouTube Direct Lite for Android shows the best way.