单击recyclerview中的listitem更改youtube视频

时间:2018-11-14 05:55:15

标签: android kotlin

我在我的Android应用中使用YoutubeplayerApi 我所做的是在播放器下方有一个youtubeplayerView和recycler视图 我要实现的功能是,一旦用户单击回收者视图的项目视图,youtubeplayer应该播放该特定视频,但是我无法实现

任何帮助将不胜感激

activity_music.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".UI.MusicActivity">

    <com.google.android.youtube.player.YouTubePlayerView
            android:layout_width="450dp"
            android:layout_height="384dp"
            android:id="@+id/youtube_player"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginTop="24dp"
    />
    <android.support.v7.widget.RecyclerView
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:id="@+id/songs_recycler_view"
            app:layout_constraintTop_toBottomOf="@+id/youtube_player"/>

</android.support.constraint.ConstraintLayout>

MusicActivity.kt

package com.example.ashish.batmn.UI

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.widget.Toast
import com.example.ashish.batmn.Adapters.SongsAdapter
import com.example.ashish.batmn.Config
import com.example.ashish.batmn.Models.Songs
import com.example.ashish.batmn.R
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.YouTubeStandalonePlayer
import kotlinx.android.synthetic.main.activity_music.*

class MusicActivity : YouTubeBaseActivity(), YouTubePlayer.OnInitializedListener {

    val songsList = listOf<Songs>(
        Songs("Girls Like You","cBVGlBWQzuc","${Config.IMAGE_BASE_URL}cBVGlBWQzuc${Config.IMAGE_PIC_END}"),
        Songs("The National - Fake Empire","KehwyWmXr3U","${Config.IMAGE_BASE_URL}KehwyWmXr3U${Config.IMAGE_PIC_END}"),
        Songs("Halka Halka","nZhLM-FeV9A","${Config.IMAGE_BASE_URL}nZhLM-FeV9A${Config.IMAGE_PIC_END}"),
        Songs("Camila Cabello - Real Friends","66rxB7_zzs8","${Config.IMAGE_BASE_URL}66rxB7_zzs8${Config.IMAGE_PIC_END}"),
        Songs("Darya","wHHCO29mqiA","${Config.IMAGE_BASE_URL}wHHCO29mqiA${Config.IMAGE_PIC_END}"),
        Songs("Naina Da Kya Kasoor","BJWTzYPWINw","${Config.IMAGE_BASE_URL}BJWTzYPWINw${Config.IMAGE_PIC_END}")

    )

    override fun onInitializationSuccess(provider: YouTubePlayer.Provider?, player: YouTubePlayer?, wasRestored: Boolean) {
        if (!wasRestored){
            player!!.cueVideo(Config.VIDEO_CODE)
            player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT)
        }
    }



    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if(requestCode  ==  1){
            getYoutubePlayerProvider().initialize(Config.VIDEO_CODE,this)
        }
    }

    override fun onInitializationFailure(p0: YouTubePlayer.Provider?, errorResult: YouTubeInitializationResult?) {
        if (errorResult!!.isUserRecoverableError){
            errorResult.getErrorDialog(this,1).show()
        }
        else{
            Toast.makeText(this,"Some unforseen  error has occured",Toast.LENGTH_SHORT).show()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_music)
        youtube_player.initialize(Config.API_KEY,this)
        val songAdapter = SongsAdapter(songsList,this){

             // what should be added in here?



        }
        songs_recycler_view.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        songs_recycler_view.adapter = songAdapter


    }

    fun getYoutubePlayerProvider():YouTubePlayer.Provider{
        return youtube_player
    }
}

Songs.kt //模型类

package com.example.ashish.batmn.Models

class Songs(val mSongName:String, val mVideoCode:String,val mSongPic:String) {
}

1 个答案:

答案 0 :(得分:0)

首先在onInitializationSuccess中将玩家实例设置为全局字段。然后将一个onClickListener添加到listview。之后,在onClick方法中,使用您要播放的视频ID调用player.setCue()方法。最后,使用player.play()播放视频。