出于安全考虑,我想在高速缓存/内部存储器中保存和读取视频。在我的代码中,它第一次显示正在下载的视频,但之后没有视频播放,我什至无法从内部存储中读取视频。
它表明路径是目录而不是文件。
class VideoPlayer:AppCompatActivity() {
lateinit var mfile2:File
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.video_player)
val model=intent.getStringExtra("object")
Toast.makeText(this,model, Toast.LENGTH_SHORT).show()
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
val ProgressBar=findViewById<ProgressBar>(R.id.bufferProgress)
// val options=findViewById<Button>(R.id.button2)
button2.setOnClickListener(){
startActivity(Intent(this,Options::class.java))
}
var contextWrapper: ContextWrapper = ContextWrapper(applicationContext)
var directory: File = filesDir
val mfile3= File(directory,"filename1.mp4")
val videoUri=Uri.parse(mfile3.path)
if(!mfile3.exists()){
val videoUri=Uri.parse(model)
videoView.setMediaController(MediaController(this))
videoView.setVideoURI(videoUri)
videoView.start()
Log.d("in if condition","model of if conditon"+model)
Log.d("in if condition","videoUri of if conditon"+videoUri)
}
else{
val mydir = filesDir
val fileWithinMyDir = File(mydir, "filename1.mp4");
fileWithinMyDir.setReadable(true, false);
val videoResource = fileWithinMyDir.getPath();
val intentUri = Uri.fromFile( File(videoResource));
videoView.setMediaController(MediaController(this))
videoView.setVideoURI(intentUri)
videoView.start()
//Log.d("in else condition","model of if conditon"+model)
Log.d("in else condition",fileWithinMyDir.isFile.toString())
Log.d("in else conditino","checking path "+videoUri)
}
videoView.setOnPreparedListener { mediaPlayer ->
mediaPlayer.setOnInfoListener(OnInfoListener { mp, what, extra ->
if (what == MEDIA_INFO_BUFFERING_END) {
ProgressBar.visibility=View.INVISIBLE
return@OnInfoListener true
} else if (what == MEDIA_INFO_BUFFERING_START) {
ProgressBar.visibility=View.VISIBLE
}
false
})
ProgressBar.visibility=View.INVISIBLE
videoView.start()
}
videoView.setOnClickListener{
Handler().postDelayed({
val actionBar: ActionBar? = supportActionBar
// window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
actionBar?.hide()
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
}, 1000)
}
val actionBar: ActionBar? = supportActionBar
// window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
actionBar?.hide()
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
val completionListener =
OnCompletionListener { mp ->
mp.stop()
startActivity(Intent(this,Options::class.java))
}
videoView.setOnCompletionListener(completionListener)
btnDownload.setOnClickListener{
downfile(model,"VideoDownload")
}
}
fun downfile(urll:String,fileName:String) {
var directory: File = filesDir
mfile2= File(directory,"filename1.mp4")
// mfile2.createNewFile();
Log.d("dowloaded file","the path of downoaded file is "+mfile2.isFile)
var downloadId = PRDownloader.download(urll, mfile2.absolutePath, fileName)
.build()
.setOnStartOrResumeListener(object : OnStartOrResumeListener {
override fun onStartOrResume() {
System.out.println("??????????????????? start")
}
})
.setOnPauseListener(object : OnPauseListener {
override fun onPause() {
}
})
.setOnCancelListener(object : OnCancelListener {
override fun onCancel() {
}
})
.setOnProgressListener(object : OnProgressListener {
override fun onProgress(progress: Progress) {
var per =
(progress.currentBytes.toFloat() / progress.totalBytes.toFloat()) * 100.00
System.out.println("::??????????????????? Per : " + per + " ?? : " + progress.currentBytes + " ?? : " + progress.totalBytes)
}
})
.start(object : OnDownloadListener {
override fun onDownloadComplete() {
System.out.println("??????????????????? complete")
Log.d("onDownloadListener","path of download file"+mfile2)
}
override fun onError(error: com.downloader.Error?) {
System.out.println("??????????????????? error"+error) }
})
System.out.println("??????????????????? called")
}
}