Kotlin MainActivity 导入未注册

问题描述 投票:0回答:0

我们正在尝试添加 youtube 视频的缩略图,并通过代码将其链接到 youtube 应用程序。我们已将所有实现同步到 build.grade。 (app) 文件,但导入不起作用并显示为红色。有没有可能我们使用的是不再有效的旧进口产品?

我们已经尝试查找进口商品,但没有任何结果。我们尝试重新键入导入,而不是将其复制粘贴到以前可以工作但现在不起作用的 IDE 中。代码中有更多错误,但我相信这是因为导入没有通过,所以其中一些工具将无法工作。

添加到 build.grade (app) 的实现

implementation 'com.google.api-client:google-api-client:1.31.3'

implementation 'com.google.apis:google-api-services-youtube:v3-rev20220225-1.31.3'

implementation 'com.github.bumptech.glide:glide:4.12.0'

主要活动代码

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport
import com.google.api.client.json.JsonFactory
import com.google.api.client.json.jackson2.JacksonFactory
import com.google.api.services.youtube.YouTube
import com.google.api.services.youtube.model.SearchListResponse
import com.google.api.services.youtube.model.SearchResult
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class MainActivity : AppCompatActivity() {

    private lateinit var imageView: ImageView
    private lateinit var youtube: YouTube

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        imageView = findViewById(R.id.imageView)

        // Initialize YouTube API client
        youtube = YouTube.Builder(
            GoogleNetHttpTransport.newTrustedTransport(),
            JacksonFactory.getDefaultInstance(),
            null
        ).setApplicationName(getString(R.string.app_name)).build()

        // Load the thumbnail of the video with the given ID
        loadThumbnail("VIDEO_ID_HERE")
    }

    private fun loadThumbnail(videoId: String) {
        GlobalScope.launch {
            val thumbnailUrl = getThumbnailUrl(videoId)

            withContext(Dispatchers.Main) {
                // Load thumbnail into ImageView using Glide library
                Glide.with(this@MainActivity)
                    .load(thumbnailUrl)
                    .into(imageView)

                // Set click listener on ImageView to open the YouTube app
                imageView.setOnClickListener {
                    val intent = Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:$videoId"))
                    intent.putExtra("VIDEO_ID", videoId)
                    startActivity(intent)
                }
            }
        }
    }

    private suspend fun getThumbnailUrl(videoId: String): String {
        // Search for the video with the given ID
        val searchResponse = youtube.search().list("id").apply {
            q = videoId
            type = "video"
            fields = "items(id(videoId))"
            maxResults = 1
        }.execute()

        // Get the first search result, which should be the video we're looking for
        val searchResult: SearchResult = (searchResponse.items as List<SearchResult>)[0]

        // Get the URL of the default thumbnail for the video
        return "https://img.youtube.com/vi/${searchResult.id.videoId}/default.jpg"
    }
}
android kotlin youtube-api youtube-data-api
© www.soinside.com 2019 - 2024. All rights reserved.