如何在Android應用中使用Kotlin播放YouTube影片?


此示例演示瞭如何在Android應用程式中使用Kotlin播放YouTube影片。

步驟1 − 在Android Studio中建立一個新專案,轉到檔案 ⇒ 新建專案,並填寫所有必需的詳細資訊以建立新專案。

步驟2 − 將以下程式碼新增到res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:padding="2dp"
   tools:context=".MainActivity">
   <androidx.recyclerview.widget.RecyclerView
      android:id="@+id/recyclerView"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />
</RelativeLayout>

步驟3 − 將以下程式碼新增到src/MainActivity.kt。

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.util.*
class MainActivity : AppCompatActivity() {
   private lateinit var recyclerView:RecyclerView
   private var youtubeVideos = Vector<youTubeVideos>()
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      title = "KotlinApp"
      recyclerView = findViewById(R.id.recyclerView)
      recyclerView.setHasFixedSize(true)
      recyclerView.layoutManager = LinearLayoutManager(this)
      youtubeVideos.add(youTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www" +
      ".youtube.com/embed/X7SiuQxhAjg\" frameborder=\"0\" allowfullscreen></iframe>"))
      youtubeVideos.add(youTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www" +
      ".youtube.com/embed/KyJ71G2UxTQ\" frameborder=\"0\" allowfullscreen></iframe>"))
      youtubeVideos.add(youTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www" +
      ".youtube.com/embed/y8Rr39jKFKU\" frameborder=\"0\" allowfullscreen>lt;/iframe>"))
      youtubeVideos.add(youTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www" +
     ".youtube.com/embed/8Hg1tqIwIfI\" frameborder=\"0\" allowfullscreen></iframe>"))
      youtubeVideos.add(youTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www" +
      ".youtube.com/embed/uhQ7mh_o_cM\" frameborder=\"0\" allowfullscreen></iframe>"))
      val videoAdapter = VideoAdapter(youtubeVideos)
      recyclerView.adapter = videoAdapter
   }
}

步驟4 − 建立一個Java類VideoAdapter.java,並新增以下程式碼。

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebChromeClient
import android.webkit.WebView
import androidx.recyclerview.widget.RecyclerView
class VideoAdapter internal constructor(private val youtubeVideoList: List<youTubeVideos>) :
RecyclerView.Adapter<VideoAdapter.VideoViewHolder>() {
   override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoViewHolder {
      val view = LayoutInflater.from(parent.context).inflate(R.layout.video_view, parent, false)
      return VideoViewHolder(view)
   }
   override fun onBindViewHolder(holder: VideoViewHolder, position: Int) {
      holder.videoWeb.loadData(youtubeVideoList[position].videoUrl!!, "text/html", "utf-8")
   }
   inner class VideoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
      var videoWeb: WebView = itemView.findViewById(R.id.webView)
      init {
         videoWeb.settings.javaScriptEnabled = true
         videoWeb.webChromeClient = object : WebChromeClient() {
         }
      }
   }
   override fun getItemCount(): Int {
      return youtubeVideoList.size
   }
}

步驟5 − 建立一個Java類youTubeVideos.java,並新增以下程式碼 −

class youTubeVideos(var videoUrl: String?) {
}

步驟6 − 建立一個佈局資原始檔(Video_view.xml)並新增以下程式碼。

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/webView"
   android:layout_width="match_parent"
   android:layout_height="180dp">
</WebView>

步驟7 − 將以下程式碼新增到androidManifest.xml。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.q11">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

讓我們嘗試執行您的應用程式。我假設您已將實際的Android移動裝置連線到您的計算機。要從Android Studio執行應用程式,請開啟專案中的一個活動檔案,然後單擊工具欄中的執行播放圖示 圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕。

更新於:2020年8月18日

860 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告