如何将视频 Link 从 activity 传递到另一个 activity?
How to pass Video Link from an activity to another activity?
我想将视频 link(字符串)从 activity 传递到 Android 中的另一个。我使用 button.setOnClickListener 并将字符串作为 intent.putextra 传递。在第二个 activity 中,我使用 intent.getStringExtra 获取字符串值并将其传递给 MediaItem,但应用程序不是 运行。当我使用硬编码值时,代码工作正常,但我无法从之前的 activity.
中获取值
我的第一个 Activity :
class ButtonActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_button)
val playbutton = findViewById<Button>(R.id.play_video)
val videolink = "https://storage.googleapis.com/exoplayer-test-media-0/BigBuckBunny_320x180.mp4"
playbutton.setOnClickListener {
val intent = Intent(this,MainActivity::class.java)
intent.putExtra("video",videolink)
startActivity(intent)
}
}
}
我的第二个Activity:
class MainActivity : AppCompatActivity(){
private var playerView: PlayerView? = null
private var player: SimpleExoPlayer? = null
private var playWhenReady = true
private var currentWindow = 0
private var playbackPosition: Long = 0
private var flag :Boolean = false
private val videolink = intent.getStringExtra("video")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
playerView = findViewById(R.id.video_view)
val videolayout = findViewById<RelativeLayout>(R.id.video_layout)
PlayerView.SHOW_BUFFERING_ALWAYS
val btFullscreen = findViewById<ImageView>(R.id.bt_fullscreen)
btFullscreen.setOnClickListener{
if (flag){
btFullscreen.setImageDrawable(resources.getDrawable(R.drawable.ic_fullscreen))
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
videolayout.layoutParams.height = 300
flag = false
}
else{
btFullscreen.setImageDrawable(resources.getDrawable(R.drawable.ic_fullscreen_exit))
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
flag = true
}
}
}
public override fun onStart() {
super.onStart()
if (Util.SDK_INT > 23) {
initializePlayer()
}
}
public override fun onResume() {
super.onResume()
hideSystemUi()
if (Util.SDK_INT <= 23 || player == null) {
initializePlayer()
}
}
public override fun onPause() {
super.onPause()
if (Util.SDK_INT <= 23) {
releasePlayer()
}
}
public override fun onStop() {
super.onStop()
if (Util.SDK_INT > 23) {
releasePlayer()
}
}
private fun initializePlayer() {
player = SimpleExoPlayer.Builder(this).build()
playerView!!.player = player
val mediaItem = MediaItem.fromUri(videolink!!)
player!!.setMediaItem(mediaItem)
player!!.playWhenReady = playWhenReady
player!!.seekTo(currentWindow, playbackPosition)
player!!.prepare()
playerView!!.keepScreenOn = true
}
private fun releasePlayer() {
if (player != null) {
playbackPosition = player!!.currentPosition
currentWindow = player!!.currentWindowIndex
playWhenReady = player!!.playWhenReady
player!!.release()
player = null
}
}
@SuppressLint("InlinedApi")
private fun hideSystemUi() {
playerView!!.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)
}
}
尝试在您的 onCreate 方法中执行此操作
intent.getStringExtra("video")
试试这个:
class MainActivity : AppCompatActivity(){
private var playerView: PlayerView? = null
private var player: SimpleExoPlayer? = null
private var playWhenReady = true
private var currentWindow = 0
private var playbackPosition: Long = 0
private var flag :Boolean = false
private var videolink = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
videolink = intent.getStringExtra("video")
playerView = findViewById(R.id.video_view)
val videolayout = findViewById<RelativeLayout>(R.id.video_layout)
PlayerView.SHOW_BUFFERING_ALWAYS
val btFullscreen = findViewById<ImageView>(R.id.bt_fullscreen)
btFullscreen.setOnClickListener{
if (flag){
btFullscreen.setImageDrawable(resources.getDrawable(R.drawable.ic_fullscreen))
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
videolayout.layoutParams.height = 300
flag = false
}
else{
btFullscreen.setImageDrawable(resources.getDrawable(R.drawable.ic_fullscreen_exit))
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
flag = true
}
}
}
public override fun onStart() {
super.onStart()
if (Util.SDK_INT > 23) {
initializePlayer()
}
}
public override fun onResume() {
super.onResume()
hideSystemUi()
if (Util.SDK_INT <= 23 || player == null) {
initializePlayer()
}
}
public override fun onPause() {
super.onPause()
if (Util.SDK_INT <= 23) {
releasePlayer()
}
}
public override fun onStop() {
super.onStop()
if (Util.SDK_INT > 23) {
releasePlayer()
}
}
private fun initializePlayer() {
player = SimpleExoPlayer.Builder(this).build()
playerView!!.player = player
val mediaItem = MediaItem.fromUri(videolink!!)
player!!.setMediaItem(mediaItem)
player!!.playWhenReady = playWhenReady
player!!.seekTo(currentWindow, playbackPosition)
player!!.prepare()
playerView!!.keepScreenOn = true
}
private fun releasePlayer() {
if (player != null) {
playbackPosition = player!!.currentPosition
currentWindow = player!!.currentWindowIndex
playWhenReady = player!!.playWhenReady
player!!.release()
player = null
}
}
@SuppressLint("InlinedApi")
private fun hideSystemUi() {
playerView!!.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)
}
}
我想将视频 link(字符串)从 activity 传递到 Android 中的另一个。我使用 button.setOnClickListener 并将字符串作为 intent.putextra 传递。在第二个 activity 中,我使用 intent.getStringExtra 获取字符串值并将其传递给 MediaItem,但应用程序不是 运行。当我使用硬编码值时,代码工作正常,但我无法从之前的 activity.
中获取值我的第一个 Activity :
class ButtonActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_button)
val playbutton = findViewById<Button>(R.id.play_video)
val videolink = "https://storage.googleapis.com/exoplayer-test-media-0/BigBuckBunny_320x180.mp4"
playbutton.setOnClickListener {
val intent = Intent(this,MainActivity::class.java)
intent.putExtra("video",videolink)
startActivity(intent)
}
}
}
我的第二个Activity:
class MainActivity : AppCompatActivity(){
private var playerView: PlayerView? = null
private var player: SimpleExoPlayer? = null
private var playWhenReady = true
private var currentWindow = 0
private var playbackPosition: Long = 0
private var flag :Boolean = false
private val videolink = intent.getStringExtra("video")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
playerView = findViewById(R.id.video_view)
val videolayout = findViewById<RelativeLayout>(R.id.video_layout)
PlayerView.SHOW_BUFFERING_ALWAYS
val btFullscreen = findViewById<ImageView>(R.id.bt_fullscreen)
btFullscreen.setOnClickListener{
if (flag){
btFullscreen.setImageDrawable(resources.getDrawable(R.drawable.ic_fullscreen))
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
videolayout.layoutParams.height = 300
flag = false
}
else{
btFullscreen.setImageDrawable(resources.getDrawable(R.drawable.ic_fullscreen_exit))
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
flag = true
}
}
}
public override fun onStart() {
super.onStart()
if (Util.SDK_INT > 23) {
initializePlayer()
}
}
public override fun onResume() {
super.onResume()
hideSystemUi()
if (Util.SDK_INT <= 23 || player == null) {
initializePlayer()
}
}
public override fun onPause() {
super.onPause()
if (Util.SDK_INT <= 23) {
releasePlayer()
}
}
public override fun onStop() {
super.onStop()
if (Util.SDK_INT > 23) {
releasePlayer()
}
}
private fun initializePlayer() {
player = SimpleExoPlayer.Builder(this).build()
playerView!!.player = player
val mediaItem = MediaItem.fromUri(videolink!!)
player!!.setMediaItem(mediaItem)
player!!.playWhenReady = playWhenReady
player!!.seekTo(currentWindow, playbackPosition)
player!!.prepare()
playerView!!.keepScreenOn = true
}
private fun releasePlayer() {
if (player != null) {
playbackPosition = player!!.currentPosition
currentWindow = player!!.currentWindowIndex
playWhenReady = player!!.playWhenReady
player!!.release()
player = null
}
}
@SuppressLint("InlinedApi")
private fun hideSystemUi() {
playerView!!.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)
}
}
尝试在您的 onCreate 方法中执行此操作
intent.getStringExtra("video")
试试这个:
class MainActivity : AppCompatActivity(){
private var playerView: PlayerView? = null
private var player: SimpleExoPlayer? = null
private var playWhenReady = true
private var currentWindow = 0
private var playbackPosition: Long = 0
private var flag :Boolean = false
private var videolink = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
videolink = intent.getStringExtra("video")
playerView = findViewById(R.id.video_view)
val videolayout = findViewById<RelativeLayout>(R.id.video_layout)
PlayerView.SHOW_BUFFERING_ALWAYS
val btFullscreen = findViewById<ImageView>(R.id.bt_fullscreen)
btFullscreen.setOnClickListener{
if (flag){
btFullscreen.setImageDrawable(resources.getDrawable(R.drawable.ic_fullscreen))
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
videolayout.layoutParams.height = 300
flag = false
}
else{
btFullscreen.setImageDrawable(resources.getDrawable(R.drawable.ic_fullscreen_exit))
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
flag = true
}
}
}
public override fun onStart() {
super.onStart()
if (Util.SDK_INT > 23) {
initializePlayer()
}
}
public override fun onResume() {
super.onResume()
hideSystemUi()
if (Util.SDK_INT <= 23 || player == null) {
initializePlayer()
}
}
public override fun onPause() {
super.onPause()
if (Util.SDK_INT <= 23) {
releasePlayer()
}
}
public override fun onStop() {
super.onStop()
if (Util.SDK_INT > 23) {
releasePlayer()
}
}
private fun initializePlayer() {
player = SimpleExoPlayer.Builder(this).build()
playerView!!.player = player
val mediaItem = MediaItem.fromUri(videolink!!)
player!!.setMediaItem(mediaItem)
player!!.playWhenReady = playWhenReady
player!!.seekTo(currentWindow, playbackPosition)
player!!.prepare()
playerView!!.keepScreenOn = true
}
private fun releasePlayer() {
if (player != null) {
playbackPosition = player!!.currentPosition
currentWindow = player!!.currentWindowIndex
playWhenReady = player!!.playWhenReady
player!!.release()
player = null
}
}
@SuppressLint("InlinedApi")
private fun hideSystemUi() {
playerView!!.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)
}
}