我正在由VideoView和Slider组成的Android应用程序上设置一个Activity。我尝试根据视频的进度在下面滚动文本。
我的解决方案之所以奏效,是因为有一个Thread根据视频的进度更改了所选幻灯片。
我想添加一项改进,以根据用户选择的幻灯片更改视频的进度。
几乎可以使用。在用户滑动文本的那一刻,视频可以很好地前进直到所需的时间。但是线程并行运行出现问题,它会立即替换文本和视频进度。
这是我的代码的一部分(还是草稿):
abstract class VideoStepActivity : AppCompatActivity() {
companion object {
const val PAUSE_TIME_BETWEEN_ACQUISITIONS: Long = 300
}
private lateinit var mViewPager: CustomViewPager
private var mIsRunning: Boolean = true
private var mCurrentStepKey: Int = -1
protected abstract val stepNumber: Int
private lateinit var myVideoView: VideoView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
this.setContentView(R.layout.activity_video_step)
}
protected fun setUpStepResources(videoFileId: Int, stepStringArray: List<Step>) {
val videoPath = "android.resource://${this.packageName}/" + videoFileId
this.myVideoView = this.findViewById(R.id.myVideoView) as VideoView
val mediaController = MediaController(this)
val uri: Uri = Uri.parse(videoPath)
this.myVideoView.setVideoURI(uri)
this.myVideoView.setMediaController(mediaController)
mediaController.setAnchorView(this.myVideoView)
this.mViewPager = this.findViewById(R.id.pager)
val pagerAdapter = this.ScreenSlidePagerAdapter(this.supportFragmentManager, stepStringArray)
this.mViewPager.adapter = pagerAdapter
this.setUpVideoStep(stepStringArray)
stepChecker(0, stepStringArray)
}
private fun setUpVideoStep(stepStringArray: List<Step>) {
this.myVideoView.setOnPreparedListener { mediaPlayer ->
mediaPlayer.isLooping = true
mediaPlayer.start()
this.mIsRunning = true
val duration = this.myVideoView.duration
this.mViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
override fun onPageSelected(position: Int) {
this@VideoStepActivity.mCurrentStepKey = position
this@VideoStepActivity.myVideoView.seekTo(stepStringArray[position].startTime)
}
override fun onPageScrollStateChanged(state: Int) {}
})
Thread(Runnable {
do {
this.mViewPager.post {
val time = this.myVideoView.currentPosition / 1000
this.stepChecker(time, stepStringArray)
}
Thread.sleep(PAUSE_TIME_BETWEEN_ACQUISITIONS)
} while (this.mIsRunning)
}).start()
}
}
private fun stepChecker(time: Int, stepStringArray: List<Step>) {
for (step: Step in stepStringArray) {
if (step.currentTimeIsInRange(time)) {
if (this.mCurrentStepKey != step.stepKey) {
this.mViewPager.currentItem = step.stepKey
this.mCurrentStepKey = step.stepKey
}
}
}
}
public override fun onDestroy() {
this.mIsRunning = false
super.onDestroy()
}
private inner class ScreenSlidePagerAdapter(fragmentManager: FragmentManager, private var stepStringArray: List<Step>) : FragmentStatePagerAdapter(fragmentManager) {
override fun getCount(): Int = this@VideoStepActivity.stepNumber
override fun getItem(position: Int): Fragment = ScreenSlidePageFragment(this.stepStringArray[position].stepString)
}
inner class Step(val stepString: String, val startTime: Int, private val endTime: Int, val stepKey: Int) {
fun currentTimeIsInRange(time: Int): Boolean {
return time in startTime..(endTime - 1)
}
}
}
我有一个与时间相关的文本列表。在其他线程中,每300毫秒,我检查一次视频范围是否可以更改滑块的选定元素。
我尝试使用以下选项根据滑块的选定元素来更改视频的进度: 带有onPageSelected的ViewPager.OnPageChangeListener。
您对我的问题有任何解决方案或改进措施吗?