add audio player component

pull/190/head
xufuji456 4 years ago
parent afb268ea5a
commit f7b39c42a3
  1. 4
      app/src/main/AndroidManifest.xml
  2. 7
      app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.kt
  3. 119
      app/src/main/java/com/frank/ffmpeg/activity/AudioPlayActivity.kt
  4. BIN
      app/src/main/res/drawable-xhdpi/ic_pause.png
  5. BIN
      app/src/main/res/drawable-xhdpi/ic_play.png
  6. 73
      app/src/main/res/layout/activity_audio_play.xml

@ -51,8 +51,10 @@
<activity
android:name=".activity.ProbeFormatActivity"
android:screenOrientation="portrait" />
<activity android:name=".activity.AudioEffectActivity"
<activity
android:name=".activity.AudioEffectActivity"
android:screenOrientation="portrait" />
<activity android:name=".activity.AudioPlayActivity"/>
</application>
</manifest>

@ -1,6 +1,8 @@
package com.frank.ffmpeg.activity
import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Environment
import android.os.Handler
import android.os.Message
@ -177,7 +179,10 @@ class AudioHandleActivity : BaseActivity() {
}
R.id.btn_play_audio//use AudioTrack to play audio
-> {
Thread { AudioPlayer().play(srcFile) }.start()
// Thread { AudioPlayer().play(srcFile) }.start()
val audioIntent = Intent(this@AudioHandleActivity, AudioPlayActivity::class.java)
audioIntent.data = Uri.parse(srcFile)
startActivity(audioIntent)
return
}
R.id.btn_play_opensl//use OpenSL ES to play audio

@ -0,0 +1,119 @@
package com.frank.ffmpeg.activity
import android.annotation.SuppressLint
import android.media.MediaPlayer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.text.TextUtils
import android.util.Log
import android.widget.ImageView
import android.widget.SeekBar
import android.widget.TextView
import com.frank.ffmpeg.R
import com.frank.ffmpeg.util.TimeUtil
class AudioPlayActivity : AppCompatActivity() {
companion object {
private val TAG = AudioPlayActivity::class.java.simpleName
private const val MSG_TIME = 123
private const val MSG_DURATION = 234
}
private var txtTitle: TextView? = null
private var txtArtist: TextView? = null
private var txtTime: TextView? = null
private var txtDuration: TextView? = null
private var audioBar: SeekBar? = null
private lateinit var audioPlayer:MediaPlayer
private val mHandler: Handler = @SuppressLint("HandlerLeak")
object : Handler() {
override fun handleMessage(msg: Message?) {
super.handleMessage(msg)
when (msg?.what) {
MSG_TIME -> {
audioBar?.progress = audioPlayer.currentPosition
txtTime?.text = TimeUtil.getVideoTime(audioPlayer.currentPosition.toLong())
sendEmptyMessageDelayed(MSG_TIME, 1000)
}
MSG_DURATION -> {
val duration = msg.obj as Int
txtDuration?.text = TimeUtil.getVideoTime(duration.toLong())
audioBar?.max = duration
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_audio_play)
initView()
initAudioPlayer()
}
private fun initView() {
txtTitle = findViewById(R.id.txt_title)
txtArtist = findViewById(R.id.txt_artist)
txtTime = findViewById(R.id.txt_time)
txtDuration = findViewById(R.id.txt_duration)
val btnPlay: ImageView = findViewById(R.id.img_play)
btnPlay.setOnClickListener {
if (isPlaying()) {
audioPlayer.pause()
btnPlay.setImageResource(R.drawable.ic_play)
} else {
audioPlayer.start()
btnPlay.setImageResource(R.drawable.ic_pause)
}
}
audioBar = findViewById(R.id.audio_bar)
audioBar?.setOnSeekBarChangeListener(object :SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
if (!fromUser) return
audioBar?.progress = progress
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
audioPlayer.seekTo(audioBar?.progress!!)
}
})
}
private fun initAudioPlayer() {
val path = intent.data?.path
Log.e(TAG, "path=$path")
if (TextUtils.isEmpty(path)) return
audioPlayer = MediaPlayer()
audioPlayer.setDataSource(path)
audioPlayer.prepareAsync()
audioPlayer.setOnPreparedListener {
Log.e(TAG, "onPrepared...")
audioPlayer.start()
val duration = audioPlayer.duration
mHandler.obtainMessage(MSG_TIME).sendToTarget()
mHandler.obtainMessage(MSG_DURATION, duration).sendToTarget()
}
}
private fun isPlaying() :Boolean {
return audioPlayer.isPlaying
}
override fun onStop() {
super.onStop()
audioPlayer.stop()
audioPlayer.release()
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
android:textColor="@color/colorPrimary"
android:textSize="18sp"/>
<TextView
android:id="@+id/txt_artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txt_title"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:textColor="@color/colorPrimary"
android:textSize="18sp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list_lyrics"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<RelativeLayout
android:id="@+id/layout_control"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp">
<SeekBar
android:id="@+id/audio_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"/>
<TextView
android:id="@+id/txt_time"
android:layout_below="@id/audio_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@id/audio_bar"
android:textColor="@color/colorPrimary"/>
<TextView
android:id="@+id/txt_duration"
android:layout_below="@+id/audio_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@id/audio_bar"
android:textColor="@color/colorPrimary"/>
<ImageView
android:id="@+id/img_play"
android:layout_below="@+id/audio_bar"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_pause"
android:contentDescription="@string/audio_play" />
</RelativeLayout>
</RelativeLayout>
Loading…
Cancel
Save