How to implement video view in Kotlin?

Answered
730 views.

How to implement video view in Kotlin?

Add Comment
2 Answer(s)
Best answer
582 views.

JAVA

 

 
package com.example.renown.kotlin
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.VideoView
class VideoPlay : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 setContentView(R.layout.activity_video_view)
 val video=findViewById(R.id.video) as VideoView
 video.setVideoPath(
 "http://www.ebookfrenzy.com/android_book/movie.mp4");
video.start();
 }
}
 

 

 

XML

 

 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
 android:layout_height="match_parent" tools:context="com.example.renown.kotlin.VideoPlay">
<VideoView
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:id="@+id/video"/>
</LinearLayout>
 

 

Permissions

 

 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
 
583 views.

How to play local directory video

on May 26, 2017.
Add Comment
429 views.

JAVA

 

 
package com.example.renown.kotlin
import android.content.Intent
import android.database.Cursor
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.support.v4.content.CursorLoader
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.VideoView
import java.util.ArrayList
import java.util.HashSet
/**
 * Created by Renown on 26-05-2017.
 */
class Videpppp : AppCompatActivity() {
 internal var listView: ListView = null!!
 internal var video: VideoView = null!!
 override fun onCreate(savedInstanceState: Bundle?) {
 super.onCreate(savedInstanceState)
 setContentView(R.layout.activity_video_view)
 video = findViewById(R.id.video2) as VideoView
 listView = findViewById(R.id.list) as ListView
 val arrayAdapter = ArrayAdapter(this@Videpppp, android.R.layout.select_dialog_item, allMedia)
 listView.adapter = arrayAdapter
 listView.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
 Log.i("sna", listView.getItemAtPosition(position).toString())
 listView(listView.getItemAtPosition(position).toString())
 /*Intent intent=new Intent(Videpppp.this,VideoPlay.class);
 intent.putExtra("path",listView.getItemAtPosition(position).toString().trim());
 startActivity(intent);*/
 }
 }
private fun listView(itemAtPosition: Any) {
/* video.setVideoPath(itemAtPosition.toString());*/
 val uri = Uri.parse(itemAtPosition.toString())
 // "http://www.ebookfrenzy.com/android_book/movie.mp4"
 video.setVideoURI(uri)
 video.start()
 }
val allMedia: ArrayList<String>
 get() {
 val videoItemHashSet = HashSet<String>()
 val projection = arrayOf(MediaStore.Video.VideoColumns.DATA, MediaStore.Video.Media.DISPLAY_NAME)
 val cursor = this@Videpppp.contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null, null, null)
 try {
 cursor!!.moveToFirst()
 do {
 videoItemHashSet.add(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)))
 } while (cursor.moveToNext())
cursor.close()
 } catch (e: Exception) {
 e.printStackTrace()
 }
val downloadedList = ArrayList(videoItemHashSet)
 return downloadedList
 }
}

 

XML

 

 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
 android:layout_height="match_parent" tools:context="com.example.renown.kotlin.VideoPlay"
 android:orientation="vertical">
 <VideoView
 android:layout_width="200dp"
 android:layout_height="200dp"
 android:id="@+id/video2"
 android:layout_gravity="center"/>
<ListView
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:id="@+id/list"/>
</LinearLayout>
 

 

permissions

 

 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.renown.kotlin">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
 
<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=".Videpppp">
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 
<category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
</application>
 
</manifest>
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.