Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I know I post similar question already, but I decided to go with simpler version of playing videos.I have most of the code, but the crucial thing is missing.The thing that I want to accomplish is to play video by clicking on the listview items.I'll explain what kind of app I am making. It's about Metallica band, and I have Fragment that shows Albums, and when you click on of the albums, it opens a new Activity with Tracks inside of said activity.And when you click on of the songs, I want to open yt_video of that song. That's it.My main problem is yt_Activity, cause in TracksActivity I know I have to use Intent.Almost forgot, all my data is stored on Firebase, so in Tracks there is id, title and url of every song in particular.I would be grateful to anyone who could tell me how to solve my problem.

public class YT_activity extends YouTubeFailureRecoveryActivity {

    @BindView(R.id.youtube_view) YouTubePlayerView youtube_view;
    @BindView(R.id.lvTracks) ListView lvTracks;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yt);
        ButterKnife.bind(this);

        youtube_view.initialize(Youtube_key.getAPI_KEY(),this);
    }

    @Override
    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        return (YouTubePlayerView) findViewById(R.id.youtube_view);
    }

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

        if (!wasRestored){

        }

    }
}

public abstract class YouTubeFailureRecoveryActivity extends YouTubeBaseActivity
implements YouTubePlayer.OnInitializedListener{


    private static final int RECOVERY_DIALOG_REQUEST = 1;

    @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
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RECOVERY_DIALOG_REQUEST) {
            // Retry initialization if user performed a recovery action
            getYouTubePlayerProvider().initialize(Youtube_key.getAPI_KEY(), this);
        }
    }

    protected abstract YouTubePlayer.Provider getYouTubePlayerProvider();


}


This is some of the code from TracksActivity:

private List<Track> mapTracks(HashMap<String, HashMap> m) {
        List<Track> r = new ArrayList<>();
        Set<String> keys = m.keySet();
        for (String key : keys) {
            HashMap<String, Object> album = m.get(key);
            String id = (String) album.get("id");
            String title = (String) album.get("title");
            final String url = (String) album.get("url");
            Track t = new Track(id, title, url);
            r.add(t);

            lvTracks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                   Intent intent = new Intent(getApplicationContext(),YT_activity.class);
                    intent.putExtra("position", position);
                    intent.putExtra("url",url);
                    startActivity(intent);



                }
            });


        }
            return r;

        }

    }


What I have tried:

I've tryied to find soulution using Google.
Posted
Comments
David Crow 2-Jan-18 9:12am    
What is the URL of the video you want to play?
Member 13593129 2-Jan-18 10:13am    
All my URL's are on Firebase within Tracks(child).Like you can see in a method above lvTracks.setOnItemClickListener.
David Crow 2-Jan-18 10:17am    
Not where, but what. What does the URL itself look like (e.g., https://www.youtube.com/watch?v=6H02UCzHf-U)?

On your device, do you have the YouTube app installed?
Member 13593129 2-Jan-18 10:25am    
It looks like this: "https://www.youtube.com/watch?v=VuDaPSdHQ5o"..
Yes, I have an Youtube app on my device.
David Crow 2-Jan-18 10:29am    
So then why can't you do (something like) this:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:VuDaPSdHQ5o"));    

context.startActivity(intent);    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900