添加视图计数计数器并使应用程序更有效

时间:2017-09-07 11:27:25

标签: android youtube-data-api android-youtube-api

所以我目前正在设计一个包含两个活动的Android应用程序,MainActivity和viewVideo。我在主活动上有一个按钮,当按下按钮时,我创建了一个从我创建的youtube播放列表中播放随机视频的意图,一旦视频播放完毕,该应用程序将返回主活动。

我希望在主活动上有一个时尚的可滑动计数器,用于显示播放列表中所有视频的总观看次数以及用户从播放列表中观看视频的次数。我希望这样做,当用户退出应用程序时,用户视图存储在某处,并在应用程序重新启动时显示在计数器上。视频也需要很长时间才能加载,因为我的代码效率不高?

这是我的MainActivity代码

public class MainActivity extends AppCompatActivity {
Button btnPlay;
TextView showValue;
public int counter = 0;
private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnPlay = (Button) findViewById(R.id.btnPlay);
    showValue = (TextView) findViewById(R.id.counterValue);

    btnPlay.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            int min = 0;
            int max = 9;
            Random r = new Random();
            int num = r.nextInt(max - min + 1) + min;

            counter++;
            showValue.setText(Integer.toString(counter));

            Intent intent = new Intent(MainActivity.this, viewVideo.class);
            startActivity(intent);
        }
    });
}
}

我的viewVideo活动代码

public class viewVideo extends YouTubeBaseActivity {
YouTubePlayerView mYouTubePlayerView;
YouTubePlayer.OnInitializedListener mOnInitializedListener;
YouTubePlayer.OnFullscreenListener mOnFullscreenListener;
int min = 0;
int max = 9;
Random r = new Random();
int num = r.nextInt(max - min + 1) + min;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_video);
    mYouTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubePlay);

    mOnInitializedListener = new YouTubePlayer.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
            youTubePlayer.loadPlaylist("PLOrsSKa3KlU9Mdai4JsiKrfYF0A9DvoLh",num ,0);
            youTubePlayer.setFullscreen(true);
            youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
            youTubePlayer.setPlayerStateChangeListener(playerStateChangeListener);
        }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
            Toast.makeText(viewVideo.this,"Failured to Initialize!", Toast.LENGTH_LONG).show();
        }
    };
    mYouTubePlayerView.initialize(YouTubeConfig.getApiKey(), mOnInitializedListener);
}

private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
    @Override
    public void onLoading() {
    }
    @Override
    public void onLoaded(String s) {
    }
    @Override
    public void onAdStarted() {
    }
    @Override
    public void onVideoStarted() {
    }
    @Override
    public void onVideoEnded() {
        finish();
        System.exit(0);
    }
    @Override
    public void onError(YouTubePlayer.ErrorReason errorReason) {
    }
};
}

Image of MainActiviy UI (I need the counter on top of the play ad btn)

0 个答案:

没有答案