Skip to content

Commit

Permalink
fix audio
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Apr 10, 2024
1 parent 48d08e5 commit 7fc85b2
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 68 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@

## 更新日志

### v1.0.6

* 修复视频可能无声音的问题
* 修复视频可能无法播放的问题

### v1.0.5

* 修复频道配置错误时可能崩溃的问题
Expand Down
12 changes: 7 additions & 5 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ fun getVersionName(): String {
}

dependencies {
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
val media3Version = "1.3.0-rc01"
// 19
val media3Version = "1.3.0"
implementation("androidx.media3:media3-ui:$media3Version")

// For media playback using ExoPlayer
Expand All @@ -78,8 +78,8 @@ dependencies {
implementation("androidx.leanback:leanback:1.0.0")
implementation("com.github.bumptech.glide:glide:4.11.0")

// 21:2.9.0 17:2.6.4
val retrofit2Version = "2.6.4"
// 21:2.11.0 17:2.6.4
val retrofit2Version = "2.11.0"
implementation("com.squareup.retrofit2:converter-gson:$retrofit2Version")
implementation ("com.squareup.retrofit2:converter-protobuf:$retrofit2Version")
implementation ("com.squareup.retrofit2:retrofit:$retrofit2Version")
Expand All @@ -90,6 +90,8 @@ dependencies {
implementation("com.google.android.exoplayer:exoplayer-core:$exoplayerVersion")
implementation("com.google.android.exoplayer:exoplayer-hls:$exoplayerVersion")

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.0-RC")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.0-RC")

implementation(files("libs/lib-decoder-ffmpeg-release.aar"))
}
Binary file added app/libs/lib-decoder-ffmpeg-release.aar
Binary file not shown.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
android:name="android.hardware.touchscreen"
android:required="false" />

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/lizongying/mytv0/GroupAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ class GroupAdapter(

fun update(tvGroupModel: TVGroupModel) {
this.tvGroupModel = tvGroupModel
notifyDataSetChanged()
recyclerView.post {
notifyDataSetChanged()
}
}

companion object {
Expand Down
20 changes: 10 additions & 10 deletions app/src/main/java/com/lizongying/mytv0/ListAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.lizongying.mytv0

import android.content.Context
import android.util.Log
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -111,10 +110,7 @@ class ListAdapter(

viewHolder.bindText(tvModel.tv.title)

// maybe null
if (!tvModel.tv.logo.isNullOrBlank()) {
viewHolder.bindImage(tvModel.tv.logo)
}
viewHolder.bindImage(tvModel.tv.logo)
}

override fun getItemCount() = tvListModel.size()
Expand All @@ -125,11 +121,15 @@ class ListAdapter(
binding.textView.text = text
}

fun bindImage(url: String) {
Glide.with(context)
.load(url)
.centerInside()
.into(binding.imageView)
fun bindImage(url: String?) {
if (url.isNullOrBlank()) {
binding.imageView.setImageDrawable(null)
} else {
Glide.with(context)
.load(url)
.centerInside()
.into(binding.imageView)
}
}

fun focus(hasFocus: Boolean) {
Expand Down
106 changes: 93 additions & 13 deletions app/src/main/java/com/lizongying/mytv0/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.lizongying.mytv0

import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.media.AudioManager
import android.os.Build
import android.os.Bundle
import android.os.Handler
Expand All @@ -15,10 +17,12 @@ import android.view.WindowManager
import android.widget.Toast
import androidx.fragment.app.FragmentActivity
import com.lizongying.mytv0.models.TVList
import kotlin.math.abs


class MainActivity : FragmentActivity() {

private var ok = 0
private var playerFragment = PlayerFragment()
private var infoFragment = InfoFragment()
private var channelFragment = ChannelFragment()
Expand Down Expand Up @@ -53,8 +57,23 @@ class MainActivity : FragmentActivity() {
.commitNow()
}

gestureDetector = GestureDetector(this, GestureListener())
gestureDetector = GestureDetector(this, GestureListener(this))

watch()

if (!TVList.setPosition(SP.position)) {
TVList.setPosition(0)
}
}

fun ready() {
ok++
if (ok == 1) {
watch()
}
}

fun watch() {
TVList.listModel.forEach { tvModel ->
tvModel.errInfo.observe(this) { _ ->
if (tvModel.errInfo.value != null
Expand All @@ -70,6 +89,7 @@ class MainActivity : FragmentActivity() {
if (tvModel.ready.value != null
&& tvModel.tv.id == TVList.position.value
) {
playerFragment.play(tvModel)
Log.i(TAG, "info ${tvModel.tv.title}")
infoFragment.show(tvModel)
if (SP.channelNum) {
Expand All @@ -78,10 +98,6 @@ class MainActivity : FragmentActivity() {
}
}
}

if (!TVList.setPosition(SP.position)) {
TVList.setPosition(0)
}
}

override fun onTouchEvent(event: MotionEvent?): Boolean {
Expand All @@ -91,9 +107,13 @@ class MainActivity : FragmentActivity() {
return super.onTouchEvent(event)
}

private inner class GestureListener : GestureDetector.SimpleOnGestureListener() {
private inner class GestureListener(private val context: Context) :
GestureDetector.SimpleOnGestureListener() {

private val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager

override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
Log.i(TAG, "onSingleTapConfirmed showMenu")
showMenu()
return true
}
Expand All @@ -109,18 +129,78 @@ class MainActivity : FragmentActivity() {
velocityX: Float,
velocityY: Float
): Boolean {
if (velocityY > 0) {
if (menuFragment.isHidden && settingFragment.isHidden) {
prev()
if ((e1?.x ?: 0f) > windowManager.defaultDisplay.width / 3
&& (e1?.x ?: 0f) < windowManager.defaultDisplay.width * 2 / 3
) {
if (velocityY > 0) {
if (menuFragment.isHidden && settingFragment.isHidden) {
prev()
}
}
}
if (velocityY < 0) {
if (menuFragment.isHidden && settingFragment.isHidden) {
next()
if (velocityY < 0) {
if (menuFragment.isHidden && settingFragment.isHidden) {
next()
}
}
}

return super.onFling(e1, e2, velocityX, velocityY)
}

override fun onScroll(
e1: MotionEvent?,
e2: MotionEvent,
distanceX: Float,
distanceY: Float
): Boolean {
// 计算手势滑动的方向和距离
val deltaY = e1?.y?.let { e2.y.minus(it) } ?: 0f
val deltaX = e1?.x?.let { e2.x.minus(it) } ?: 0f

// 如果是垂直滑动
if (abs(deltaY) > abs(deltaX)) {
if ((e1?.x ?: 0f) > windowManager.defaultDisplay.width * 2 / 3) {
adjustVolume(deltaY) // 调整音量
}
}

return super.onScroll(e1, e2, distanceX, distanceY)
}

private fun adjustVolume(deltaY: Float) {
val maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
val currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
val deltaVolume = deltaY / 1000 * maxVolume / windowManager.defaultDisplay.height

var newVolume = currentVolume + deltaVolume
if (newVolume < 0) {
newVolume = 0F
} else if (newVolume > maxVolume) {
newVolume = maxVolume.toFloat()
}

audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newVolume.toInt(), 0)

// 可以添加一个toast来显示当前音量
Toast.makeText(context, "Volume: $newVolume / $maxVolume", Toast.LENGTH_SHORT).show()
}

// private fun changeBrightness(deltaBrightness: Float) {
// brightness += deltaBrightness
// if (brightness < 0) {
// brightness = 0f
// } else if (brightness > 1) {
// brightness = 1f
// }
//
// val layoutParams = windowManager.attributes
// layoutParams.screenBrightness = brightness
// windowManager.attributes = layoutParams
//
// // 可以添加一个toast来显示当前亮度
// Toast.makeText(context, "Brightness: $brightness", Toast.LENGTH_SHORT).show()
// }

}

fun play(position: Int) {
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/com/lizongying/mytv0/MenuFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,15 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList
listAdapter.setItemListener(this)

TVList.groupModel.tvGroupModel.observe(viewLifecycleOwner) { _ ->
(activity as MainActivity).watch()

// not first time
if (TVList.groupModel.tvGroupModel.value != null) {
groupAdapter.update(TVList.groupModel)
tvListModel = TVList.groupModel.getTVListModel(TVList.groupModel.position.value!!)
if (tvListModel != null) {
(binding.list.adapter as ListAdapter).update(tvListModel!!)
}
}
}

Expand All @@ -90,8 +95,7 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList
}
}

override fun onItemClicked(tvModel: TVModel) {
TVList.setPosition(tvModel.tv.id)
override fun onItemClicked(tvModel: TVModel) { TVList.setPosition(tvModel.tv.id)
(activity as MainActivity).hideMenuFragment()
}

Expand Down
35 changes: 9 additions & 26 deletions app/src/main/java/com/lizongying/mytv0/PlayerFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ import androidx.media3.datasource.DefaultHttpDataSource
import androidx.media3.datasource.TransferListener
import androidx.media3.exoplayer.DefaultRenderersFactory
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.hls.HlsMediaSource
import androidx.media3.exoplayer.mediacodec.MediaCodecSelector
import androidx.media3.exoplayer.mediacodec.MediaCodecUtil
import com.google.android.exoplayer2.SimpleExoPlayer
import com.lizongying.mytv0.databinding.PlayerBinding
import com.lizongying.mytv0.models.TVList
import com.lizongying.mytv0.models.TVModel


Expand Down Expand Up @@ -65,6 +63,7 @@ class PlayerFragment : Fragment(), SurfaceHolder.Callback {
val renderersFactory = context?.let { DefaultRenderersFactory(it) }
val playerMediaCodecSelector = PlayerMediaCodecSelector()
renderersFactory?.setMediaCodecSelector(playerMediaCodecSelector)
renderersFactory?.setExtensionRendererMode(DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON)

player = context?.let {
ExoPlayer.Builder(it)
Expand Down Expand Up @@ -93,31 +92,16 @@ class PlayerFragment : Fragment(), SurfaceHolder.Callback {
tvModel?.setReady()
}
})

(activity as MainActivity).ready()
Log.i(TAG, "player ready")
ready()
}
})
}

return _binding!!.root
}

fun ready() {
TVList.listModel.forEach { tvModel ->
tvModel.ready.observe(this) { _ ->

// not first time
if (tvModel.ready.value != null
&& tvModel.tv.id == TVList.position.value
&& tvModel.videoUrl.value != null
// && tvModel.videoUrl.value != videoUrl
) {
play(tvModel)
}
}
}
}

@OptIn(UnstableApi::class)
fun play(tvModel: TVModel) {
videoUrl = tvModel.videoUrl.value ?: return
Expand Down Expand Up @@ -162,13 +146,12 @@ class PlayerFragment : Fragment(), SurfaceHolder.Callback {
}
})

val hlsMediaSource = HlsMediaSource.Factory(httpDataSource).createMediaSource(
MediaItem.fromUri(videoUrl)
)

setMediaSource(hlsMediaSource)
// val hlsMediaSource = HlsMediaSource.Factory(httpDataSource).createMediaSource(
// MediaItem.fromUri(videoUrl)
// )
// setMediaSource(hlsMediaSource)

// setMediaItem(MediaItem.fromUri(videoUrl))
setMediaItem(MediaItem.fromUri(videoUrl))
prepare()
}
exoPlayer?.run {
Expand Down Expand Up @@ -259,8 +242,8 @@ class PlayerFragment : Fragment(), SurfaceHolder.Callback {
}
}
})
(activity as MainActivity).ready()
Log.i(TAG, "exoPlayer ready")
ready()
}

override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
Expand Down
Loading

0 comments on commit 7fc85b2

Please sign in to comment.