Skip to content

Commit 80ab513

Browse files
committed
Update ExternalPlayerActivity with service integration and UI controls
1 parent df9d654 commit 80ab513

1 file changed

Lines changed: 43 additions & 64 deletions

File tree

external-player/src/main/java/org/newpipe/externalplayer/ExternalPlayerActivity.kt

Lines changed: 43 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ import android.net.Uri
66
import android.os.Build
77
import android.os.Bundle
88
import android.util.Rational
9-
import android.view.View
9+
import android.view.GestureDetector
10+
import android.view.MotionEvent
1011
import androidx.appcompat.app.AppCompatActivity
11-
import com.google.android.exoplayer2.MediaItem
12-
import com.google.android.exoplayer2.Player.STATE_READY
13-
import com.google.android.exoplayer2.SimpleExoPlayer
12+
import androidx.core.content.ContextCompat
1413
import org.newpipe.externalplayer.databinding.ActivityExternalPlayerBinding
1514

1615
class ExternalPlayerActivity : AppCompatActivity() {
1716

1817
private lateinit var binding: ActivityExternalPlayerBinding
19-
private var player: SimpleExoPlayer? = null
20-
private var playWhenReady = true
21-
private var playbackPosition: Long = 0
18+
private var serviceStarted = false
19+
private val speeds = floatArrayOf(1.0f, 1.25f, 1.5f, 2.0f, 0.5f)
20+
private var speedIndex = 0
2221

2322
override fun onCreate(savedInstanceState: Bundle?) {
2423
super.onCreate(savedInstanceState)
@@ -27,8 +26,31 @@ class ExternalPlayerActivity : AppCompatActivity() {
2726

2827
handleIncomingIntent(intent)
2928

30-
binding.enterPipButton.setOnClickListener {
31-
enterPip()
29+
binding.enterPipButton.setOnClickListener { enterPip() }
30+
binding.speedButton.setOnClickListener {
31+
speedIndex = (speedIndex + 1) % speeds.size
32+
val speed = speeds[speedIndex]
33+
binding.speedButton.text = "${speed}x"
34+
val intent = Intent(this, PlayerService::class.java).apply {
35+
action = PlayerService.ACTION_PLAY
36+
putExtra("speed", speed)
37+
}
38+
ContextCompat.startForegroundService(this, intent)
39+
}
40+
binding.subToggle.setOnClickListener {
41+
val newText = if (binding.subToggle.text == "SUB") "SUB:OFF" else "SUB"
42+
binding.subToggle.text = newText
43+
}
44+
45+
val gestureDetector = GestureDetector(this, object : GestureDetector.SimpleOnGestureListener() {
46+
override fun onScroll(e1: MotionEvent?, e2: MotionEvent?, distanceX: Float, distanceY: Float): Boolean {
47+
return super.onScroll(e1, e2, distanceX, distanceY)
48+
}
49+
})
50+
51+
binding.playerView.setOnTouchListener { _, event ->
52+
gestureDetector.onTouchEvent(event)
53+
false
3254
}
3355
}
3456

@@ -39,37 +61,29 @@ class ExternalPlayerActivity : AppCompatActivity() {
3961
if (Intent.ACTION_VIEW == action || data != null) {
4062
data?.let { uri ->
4163
binding.urlText.text = uri.toString()
42-
initializePlayer(uri.toString())
64+
startServiceWithUri(uri.toString())
4365
}
4466
} else if (Intent.ACTION_SEND == action) {
4567
val text = intent.getStringExtra(Intent.EXTRA_TEXT) ?: intent.getStringExtra(Intent.EXTRA_STREAM)?.toString()
4668
text?.let {
4769
val url = extractUrlFromText(it)
48-
if (url != null) initializePlayer(url)
70+
if (url != null) startServiceWithUri(url)
4971
}
5072
}
5173
}
5274

53-
private fun extractUrlFromText(text: String): String? {
54-
val regex = "(https?://[\w\-._~:/?#[\]@!$&'()*+,;=%]+)".toRegex()
55-
return regex.find(text)?.value
75+
private fun startServiceWithUri(uri: String) {
76+
val intent = Intent(this, PlayerService::class.java).apply {
77+
action = PlayerService.ACTION_SET_URI
78+
putExtra(PlayerService.EXTRA_URI, uri)
79+
}
80+
ContextCompat.startForegroundService(this, intent)
81+
serviceStarted = true
5682
}
5783

58-
private fun initializePlayer(url: String) {
59-
if (player == null) {
60-
player = SimpleExoPlayer.Builder(this).build()
61-
binding.playerView.player = player
62-
}
63-
val mediaItem = MediaItem.fromUri(Uri.parse(url))
64-
player!!.setMediaItem(mediaItem)
65-
player!!.playWhenReady = playWhenReady
66-
player!!.seekTo(playbackPosition)
67-
player!!.prepare()
68-
player!!.addListener(object : com.google.android.exoplayer2.Player.Listener {
69-
override fun onPlaybackStateChanged(state: Int) {
70-
binding.loadingView.visibility = if (state == STATE_READY) View.GONE else View.VISIBLE
71-
}
72-
})
84+
private fun extractUrlFromText(text: String): String? {
85+
val regex = "(https?://[\\w\\-._~:/?#[\\]@!$&'()*+,;=%]+)".toRegex()
86+
return regex.find(text)?.value
7387
}
7488

7589
private fun enterPip() {
@@ -87,39 +101,4 @@ class ExternalPlayerActivity : AppCompatActivity() {
87101
enterPip()
88102
}
89103
}
90-
91-
override fun onPause() {
92-
super.onPause()
93-
player?.let {
94-
playbackPosition = it.currentPosition
95-
playWhenReady = it.playWhenReady
96-
it.playWhenReady = false
97-
}
98-
}
99-
100-
override fun onResume() {
101-
super.onResume()
102-
player?.playWhenReady = playWhenReady
103-
}
104-
105-
override fun onStop() {
106-
super.onStop()
107-
if (!isInPictureInPictureMode) {
108-
releasePlayer()
109-
}
110-
}
111-
112-
override fun onDestroy() {
113-
super.onDestroy()
114-
releasePlayer()
115-
}
116-
117-
private fun releasePlayer() {
118-
player?.let {
119-
playbackPosition = it.currentPosition
120-
playWhenReady = it.playWhenReady
121-
it.release()
122-
player = null
123-
}
124-
}
125104
}

0 commit comments

Comments
 (0)