4

I have a activity called VideoActivity.Below the youtubeplayerview there is a list of videos.On the first time the video is loading.When i click on a item then go to another activity and then return to that activity, it does not play. May i know the reason ?

Here is the code

public class VideoActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

public Video video;
private RecyclerView recyclerVideos;
private static final int RECOVERY_DIALOG_REQUEST = 1;
private YouTubePlayerView youTubeView;


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

    recyclerVideos = (RecyclerView) findViewById(R.id.recyclerVideos);

    Intent i = getIntent();
    video = (Video) i.getSerializableExtra("video");

    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    recyclerVideos.setLayoutManager(layoutManager);

    new VideoAsyncTask().execute();

 }

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

    if (errorReason.isUserRecoverableError()) {
        errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
    } else {
        String errorMessage = String.format(
                getString(R.string.error_player), errorReason.toString());
        Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
    }
}

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

    if (!wasRestored) {
        player.cueVideo(video.Videoid);
        player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
    }
}


@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(Constants.DEVELOPER_KEY, this);
    }
}

private YouTubePlayer.Provider getYouTubePlayerProvider() {
    return (YouTubePlayerView) findViewById(R.id.youtube_view);
}

private class VideoAsyncTask extends AsyncTask<Void, Void, Video> {

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected Video doInBackground(Void... params) {

        try {
         return   webservice();

        } catch (Exception e1) {
            e1.printStackTrace();
            return null;
        }
    }

    protected void onPostExecute(final Video video) {
        try {
            if (video == null) {
                return;
            }

            VideosAdapter videoAdapter = new VideosAdapter(video.videos,VideoActivity.this);
            recyclerVideos.setAdapter(videoAdapter);
    youTubeView.initialize(Constants.DEVELOPER_KEY, VideoActivity.this);

        } catch (Throwable ignored) {}
    }
}
}
mob_web_dev
  • 2,342
  • 1
  • 20
  • 42

5 Answers5

1

Yes.I solved the issue.

Step 1 : add the youtubeview to linearlayout

Step 2 : on onResume remove the view and add the youtubeview to linearlayout again when return to activity

Step 3 : stop the youtube video in back press

mob_web_dev
  • 2,342
  • 1
  • 20
  • 42
  • Could you please add more detail on how you implement this. I tried your step but it still failed to work. Thank you. – Duc Le Nov 28 '16 at 15:46
  • @DucLe : Hey I created a LinearLayout in xml and added the youtubeview to LinearLayout and then initialized it.If i again come to this activity from another activity then in onResume I removed the view and add it again. – mob_web_dev Nov 29 '16 at 05:25
  • I can't make it work. Can you show some code please? I tried adding the YouTubePlayerView programmatically and also from xml. Video still don't load. – stavros.3p Jan 12 '20 at 19:11
0
@Override
public void onResume(){
super.onResume();
try {
        if (video == null) {
            return;
        }

       YouTubePlayer.loadVideo(String videoId, int timeMillis);

    } catch (Throwable ignored) {}
}
sasikumar
  • 12,540
  • 3
  • 28
  • 48
0

try finishing the activity when you leave it , or initializing the youtubeplay on resume instead of oncreate

@Override
public void onResume(){
    super.onResume();
    new VideoAsyncTask().execute();}
yanivtwin
  • 617
  • 8
  • 32
0

Before leaving activity/fragment where the YouTubePlayerView is do this:

youtubePlayer.pause();
youtubePlayer.release();
stavros.3p
  • 2,244
  • 6
  • 20
  • 37
0

Example of my manifest

<application
    android:name=".UGNEWS24"
    android:allowBackup="true"
    android:debuggable="true"
    android:hardwareAccelerated="true"
    android:icon="@drawable/ugnews24_rounded_logo"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:roundIcon="@drawable/ugnews24_rounded_logo"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:ignore="HardcodedDebugMode">
    <activity android:name=".activity.WebViewActivity"></activity>
    <activity android:name=".SearchResultsActivity" />
MUHINDO
  • 788
  • 6
  • 10