1

Hello fellow programmers,

I'm experiencing some issues regarding my Android App, i'm currently working on. For this purpose, I only need to mention that I have two Activities (One is called MainActivity.class and the second is called FilterActivity.class).

The purpose of my MainActiviy class is to display a movie (Genres, year, rating etc) + a trailer of the specifik video.

In the OnCreate method for MainActiviy, im initializing the YouTubePlayerView (since I want a random movie to pop up as soon as you open the application).

The purpose of my FilterActivity class is to choose some specfik search criterias for a movie.

I'm opening FilterActivity from MainActivity like this:

public void openFilter(){
         Intent askIntent = new Intent(this, FilterActivity.class);
         startActivityForResult(askIntent, 1); }

And in my FilterActivity im sending the information from a newly created movie like this:

movieIntent.putExtra("url", a.getUrl());
             movieIntent.putExtra("title", a.getTitle());
             movieIntent.putExtra("rating", (String.valueOf(a.getRating())));
             movieIntent.putExtra("plot", a.getDesc());
             movieIntent.putExtra("year", (String.valueOf(a.getYear())));
             movieIntent.putExtra("genre", a.getGenres());
             setResult(RESULT_OK, movieIntent);
             finish(); 

And this is how I fetch data from MainActivity:

protected void onActivityResult(int requestCode, int resultCode, final Intent data){
    if(resultCode == RESULT_OK){
        titleView.setText(data.getStringExtra("title"));
        ratingView.setText(data.getStringExtra("rating"));
        plotView.setText(data.getStringExtra("plot"));
        yearView.setText(data.getStringExtra("year"));
        genreView.setText(data.getStringExtra("genre"));
        url = data.getStringExtra("url");     }

This is basically what I need to show. (This is all works by the way): I'm getting a newly created movie and the criterias match.

However, in the OnActivityResult, I can't get my YoutubePlayerView to re-load the video with the specific URL. The old video is still there, and playable. I have checked and I am indeed getting a new URL from the FilterActivity.

The only way I'm coming around this issue is by basically reloading the activity, and then (since im creating a random movie in my OnCreate method), the criteria don't match.

Any suggestions would be appreciated!

Sincerely

RamPrakash
  • 1,687
  • 3
  • 20
  • 25
yuggmeister
  • 87
  • 2
  • 8

1 Answers1

0

In onActivityResult, release current player and re-initialize YoutubePlayerView :

@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
    if (resultCode == RESULT_OK) {
        mVideoId = getVideoId(data.getStringExtra("url"));
        mPlayer.release();
        mYoutubeplayerView.initialize(mApiKey, this);
    }
}

A complete example of MainActivity is :

public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

    String mVideoId = "5xVh-7ywKpE";

    String mApiKey = "YOUR_API_KEY";

    YouTubePlayerView mYoutubeplayerView;

    YouTubePlayer mPlayer;

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

        mYoutubeplayerView = (YouTubePlayerView) findViewById(R.id.player);

        mYoutubeplayerView.initialize(mApiKey, this);

        Button button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openFilter();
            }
        });

    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                        YouTubePlayer youTubePlayer, boolean b) {
        mPlayer = youTubePlayer;
        mPlayer.loadVideo(mVideoId);
    }

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


    @Override
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
        if (resultCode == RESULT_OK) {
            mVideoId = getVideoId(data.getStringExtra("url"));
            mPlayer.release();
            mYoutubeplayerView.initialize(mApiKey, this);
        }
    }

    private String getVideoId(String url) {
        String pattern = "(?<=watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*";

        Pattern compiledPattern = Pattern.compile(pattern);
        Matcher matcher = compiledPattern.matcher(url);

        if (matcher.find()) {
            return matcher.group();
        }
        return "";
    }

    public void openFilter() {
        Intent askIntent = new Intent(this, FilterActivity.class);
        startActivityForResult(askIntent, 1);
    }
}

Note that I've used this post to extract Youtube videoId from url path

Community
  • 1
  • 1
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159