2017-11-16 6 views
0

OK - Androidスタジオ3.0とYouTube APIバージョン3をプロジェクトで使用しています。 ライブラリ/ jarファイルを(このリンクを介して)プロジェクトに追加しました。 プロジェクトがコンパイルされます。YouTubeプレーヤーアクティビティでエラーが発生しました

私はこのアプリを実行して動画を選択すると、「YouTubeプレーヤーの初期化中にエラーが発生しました」というメッセージが表示されます。これは端末とエミュレータで発生します。

私はlogcatを見て、私は見つけることすべてがIDE自体にこのエラーメッセージが表示され、IDEを見て:

IDE error of YouTube Activity content

そして私もIDEの上部にこれを見ます:それは私が間違って行っているとどのように私は、彼らが選択されている場合、私はYouTubeの動画を再生することができますので、それを修正することができ

error for YouTube modue

は何ですか?

私はここに私のYouTubeのアクティビティファイルのコードを追加しました:

import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Toast; 
import android.widget.VideoView; 

import com.google.android.youtube.player.YouTubeBaseActivity; 
import com.google.android.youtube.player.YouTubeInitializationResult; 
import com.google.android.youtube.player.YouTubePlayer; 
import com.google.android.youtube.player.YouTubePlayerView; 


public class YouTubePlaybackOverlayActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener { 

    private static String ytUrl; 
    public String TAG = YouTubePlaybackOverlayActivity.class.getSimpleName(); 

    public VideoView mVideoView; 

    public LeanbackPlaybackState mPlaybackState = LeanbackPlaybackState.IDLE; 

    public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXX"; 

    private int mPosition = 0; 
    private long mStartTimeMillis; 
    private long mDuration = -1; 
    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    //private GoogleApiClient client; 

    @Override 
    public void onStart() { 
     super.onStart(); 

//  // ATTENTION: This was auto-generated to implement the App Indexing API. 
//  // See https://g.co/AppIndexing/AndroidStudio for more information. 
//  client.connect(); 
//  Action viewAction = Action.newAction(
//    Action.TYPE_VIEW, // TODO: choose an action type. 
//    "PlaybackOverlay Page", // TODO: Define a title for the content shown. 
//    // TODO: If you have web page content that matches this app activity's content, 
//    // make sure this auto-generated web page URL is correct. 
//    // Otherwise, set the URL to null. 
//    Uri.parse("http://host/path"), 
//    // TODO: Make sure this auto-generated app deep link URI is correct. 
//    Uri.parse("android-app://software.blackstone.sunnahstreamtv/http/host/path") 
//  ); 
//  AppIndex.AppIndexApi.start(client, viewAction); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

//  // ATTENTION: This was auto-generated to implement the App Indexing API. 
//  // See https://g.co/AppIndexing/AndroidStudio for more information. 
//  Action viewAction = Action.newAction(
//    Action.TYPE_VIEW, // TODO: choose an action type. 
//    "PlaybackOverlay Page", // TODO: Define a title for the content shown. 
//    // TODO: If you have web page content that matches this app activity's content, 
//    // make sure this auto-generated web page URL is correct. 
//    // Otherwise, set the URL to null. 
//    Uri.parse("http://host/path"), 
//    // TODO: Make sure this auto-generated app deep link URI is correct. 
//    Uri.parse("android-app://software.blackstone.sunnahstreamtv/http/host/path") 
//  ); 
//  AppIndex.AppIndexApi.end(client, viewAction); 
//  client.disconnect(); 
    } 

    /* 
    * List of various states that we can be in 
    */ 
    public enum LeanbackPlaybackState { 
     PLAYING, PAUSED, IDLE 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     /** attaching layout xml **/ 
     //setContentView(R.layout.nabawi_video); 
     //setContentView(R.layout.activity_playback_overlay); 
     setContentView(R.layout.video_playback); 

     /** Initializing YouTube player view **/ 
     YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player); 
     youTubePlayerView.initialize(API_KEY, this); 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     //client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 


// @Override 
// protected void onCreate(Bundle savedInstanceState) { 
//  super.onCreate(savedInstanceState); 
//  setContentView(R.layout.activity_playback_overlay); 
// 
//  loadViews(); 
// } 

    @Override 
    public void onDestroy() { 
     stopPlayback(); 
     super.onDestroy(); 
    } 

    private void loadViews() { 
     mVideoView = (VideoView) findViewById(R.id.youtube_player); 
     mVideoView.setFocusable(false); 
     mVideoView.setFocusableInTouchMode(false); 

     Movie movie = (Movie) getIntent().getSerializableExtra(DetailsActivity.MOVIE); 
     //setVideoPath(movie.getVideoUrl()); 
     setVideoPath(movie.getyTubeID()); 
     ytUrl = movie.getyTubeID(); 
    } 

    public void setVideoPath(String videoUrl) { 
     setPosition(0); 
     mVideoView.setVideoPath(videoUrl); 
     mStartTimeMillis = 0; 
     mDuration = Utils.getDuration(videoUrl); 
    } 

    private void stopPlayback() { 
     if (mVideoView != null) { 
      mVideoView.stopPlayback(); 
     } 
    } 

    private void setPosition(int position) { 
     if (position > mDuration) { 
      mPosition = (int) mDuration; 
     } else if (position < 0) { 
      mPosition = 0; 
      mStartTimeMillis = System.currentTimeMillis(); 
     } else { 
      mPosition = position; 
     } 
     mStartTimeMillis = System.currentTimeMillis(); 
     Log.d(TAG, "position set to " + mPosition); 
    } 

    public int getPosition() { 
     return mPosition; 
    } 

    public void setPlaybackState(LeanbackPlaybackState playbackState) { 
     this.mPlaybackState = playbackState; 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_playback_overlay, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    public void playPause(boolean doPlay) { 
     if (mPlaybackState == LeanbackPlaybackState.IDLE) { 
      /* Callbacks for mVideoView */ 
      setupCallbacks(); 
     } 

     if (doPlay && mPlaybackState != LeanbackPlaybackState.PLAYING) { 
      mPlaybackState = LeanbackPlaybackState.PLAYING; 
      if (mPosition > 0) { 
       mVideoView.seekTo(mPosition); 
      } 
      mVideoView.start(); 
      mStartTimeMillis = System.currentTimeMillis(); 
     } else { 
      mPlaybackState = LeanbackPlaybackState.PAUSED; 
      int timeElapsedSinceStart = (int) (System.currentTimeMillis() - mStartTimeMillis); 
      setPosition(mPosition + timeElapsedSinceStart); 
      mVideoView.pause(); 
     } 
    } 

    public void fastForward() { 
     if (mDuration != -1) { 
      // Fast forward 10 seconds. 
      setPosition(mVideoView.getCurrentPosition() + (10 * 1000)); 
      mVideoView.seekTo(mPosition); 
     } 
    } 

    public void rewind() { 
     // rewind 10 seconds 
     setPosition(mVideoView.getCurrentPosition() - (10 * 1000)); 
     mVideoView.seekTo(mPosition); 
    } 

    private void setupCallbacks() { 

     mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() { 

      @Override 
      public boolean onError(MediaPlayer mp, int what, int extra) { 
       mVideoView.stopPlayback(); 
       mPlaybackState = LeanbackPlaybackState.IDLE; 
       return false; 
      } 
     }); 

     mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
      @Override 
      public void onPrepared(MediaPlayer mp) { 
       if (mPlaybackState == LeanbackPlaybackState.PLAYING) { 
        mVideoView.start(); 
       } 
      } 
     }); 

     mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
      @Override 
      public void onCompletion(MediaPlayer mp) { 
       mPlaybackState = LeanbackPlaybackState.IDLE; 
      } 
     }); 
    } 

    @Override 
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) { 
     Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) { 
     /** add listeners to YouTubePlayer instance **/ 
     player.setPlayerStateChangeListener(playerStateChangeListener); 
     player.setPlaybackEventListener(playbackEventListener); 
     player.setFullscreen(true); 
     player.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS); 
     //player.play(); 


     /** Start buffering **/ 

     for (int i = 0; i < MovieProvider.mItems.size(); i++) { 
      Movie movie = (Movie) getIntent().getSerializableExtra(DetailsActivity.MOVIE); 
      player.loadVideo(movie.getyTubeID()); 
     } 

    } 

    private YouTubePlayer.PlaybackEventListener playbackEventListener = new YouTubePlayer.PlaybackEventListener() { 

     @Override 
     public void onBuffering(boolean arg0) { 
     } 

     @Override 
     public void onPaused() { 
     } 

     @Override 
     public void onPlaying() { 
     } 

     @Override 
     public void onSeekTo(int arg0) { 
     } 

     @Override 
     public void onStopped() { 
     } 

    }; 

    private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() { 

     @Override 
     public void onAdStarted() { 
     } 

     @Override 
     public void onError(YouTubePlayer.ErrorReason arg0) { 
     } 

     @Override 
     public void onLoaded(String arg0) { 
     } 

     @Override 
     public void onLoading() { 
     } 

     @Override 
     public void onVideoEnded() { 
     } 

     @Override 
     public void onVideoStarted() { 
     } 
    }; 
} 

答えて

0

問題は、プロジェクト内のcorroutインデックスでした。 クラスファイルを削除しました。 その後、キャッシュをクリアし、Android Studioを再起動してから、プロジェクトをクリーニングして再構築しました。 Android Studioをもう一度起動してから、目的のクラスファイルとレイアウトファイルを再作成して、プロジェクトが正常にコンパイルされ、すべてが有効です。

時間、アドバイス、アドバイス、およびご意見をいただき、ありがとうございます。

関連する問題