diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index eefed6c..a9278df 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -51,8 +51,10 @@ - + \ No newline at end of file diff --git a/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.kt b/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.kt index 51c88da..94d5cac 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.kt +++ b/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.kt @@ -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 diff --git a/app/src/main/java/com/frank/ffmpeg/activity/AudioPlayActivity.kt b/app/src/main/java/com/frank/ffmpeg/activity/AudioPlayActivity.kt new file mode 100644 index 0000000..199bc0b --- /dev/null +++ b/app/src/main/java/com/frank/ffmpeg/activity/AudioPlayActivity.kt @@ -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() + } + +} diff --git a/app/src/main/res/drawable-xhdpi/ic_pause.png b/app/src/main/res/drawable-xhdpi/ic_pause.png new file mode 100644 index 0000000..dedce6a Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_pause.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_play.png b/app/src/main/res/drawable-xhdpi/ic_play.png new file mode 100644 index 0000000..72dc5a4 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_play.png differ diff --git a/app/src/main/res/layout/activity_audio_play.xml b/app/src/main/res/layout/activity_audio_play.xml new file mode 100644 index 0000000..ea40270 --- /dev/null +++ b/app/src/main/res/layout/activity_audio_play.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file