Using Media in App Development

  • Replies:1
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Aug 3, 2018 8:42:43 PM via Website

In my Application, I have a MusicPlayer. This MusicPlayer plays Music even if the App or Activity isn't visible. When I go back with the Android Back Button and reopen the Activity to pause the Music or to start another song it seems to be a new MusicPlayer Object. So that I can't control the old one. Do I just reopen the activity the wrong way or do I have to somehow call this objects? I read something of MediaBrowserService but I don't get it, how I implement this other way

My MusicPlayer:

class MusicPlayer : AppCompatActivity() {
private var songAdapter : RecyclerView.Adapter<MusicRecyclerSongAdapter.ViewHolder>? = null
private var albumAdapter: RecyclerView.Adapter<MusicRecyclerAlbumAdapter.ViewHolder>? = null
val context:Context = this
//prepare variables for late init
lateinit var songUri:ArrayList<String>
lateinit var mplayer:MediaPlayer
lateinit var album_name:ArrayList<String>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_music_player)
//Create Recycler View and get Adapter
val recyclerViewMusic: RecyclerView = findViewById(R.id.musicList)
recyclerViewMusic.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false)
albumAdapter = MusicRecyclerAlbumAdapter(context)
//Getting values from Compainon Object
album_name = GetAlbum.albums
//Apply Adapter to recyclerview and change Backgroundcolor
recyclerViewMusic.adapter = albumAdapter
recyclerViewMusic.setBackgroundColor(Color.BLUE)
fun startSong(position: Int){
if(songAdapter == null) {
songAdapter = MusicRecyclerSongAdapter(context, album_name[position])
//Getting the Location of the Songs
songUri = GetMusic.uris
recyclerViewMusic.adapter = songAdapter
}else if(::mplayer.isInitialized){
//Testing if no song is playing already
if(!mplayer.isPlaying) {
mplayer.setDataSource(songUri[position])
mplayer.prepare()
mplayer.start()
}else{
mplayer.reset()
mplayer.setDataSource(songUri[position])
mplayer.prepare()
mplayer.start()
}
}else{
mplayer= MediaPlayer.create(context,Uri.parse(songUri[position]))
musicprogressbar.max = mplayer.duration
mplayer.start()
Timer().scheduleAtFixedRate(object: TimerTask() {
override fun run() {
musicprogressbar.progress = mplayer.currentPosition
}
},10,100)
}
}
recyclerViewMusic.addOnItemTouchListener(
RecyclerItemClickListener(context, recyclerViewMusic, object : RecyclerItemClickListener.OnItemClickListener {
override fun onItemClick(view: View, position: Int) {
//Change to Song selection
startSong(position)
}
override fun onLongItemClick(view: View, position: Int) {
//Change to Song selection
startSong(position)
}
}
)
)
musicprogressbar.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener{
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
if(fromUser) {
mplayer.seekTo(progress)
}
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
if(::mplayer.isInitialized) {
mplayer.pause()
}
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
if(::mplayer.isInitialized) {
mplayer.start() }
}
}
)
}
fun musicControls(view: View){
//applying functions to buttons
if(::mplayer.isInitialized) {
when (view.id) {
//Pause or Start Music
R.id.musicStart -> if (!mplayer.isPlaying) mplayer.start()
R.id.musicPause -> if (mplayer.isPlaying) mplayer.pause()
/*//Forward/Backward Music
R.id.musicbackward -> {
mplayer.pause()
mplayer.seekTo(mplayer.currentPosition - 5000)
mplayer.start()
}
R.id.musicforward -> {
mplayer.pause()
mplayer.seekTo(mplayer.currentPosition + 5000)
mplayer.start()
}*/
}
}
}
}

And the MainAcitivty Function:

class MainActivity : AppCompatActivity() {
val context = this
private var adapter : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>? = null
private val RECORD_REQUEST_CODE = 101
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//asking for permissions
getPermissions()
//applying the Layout to the RecyclerView
val recyclerView: RecyclerView = findViewById(R.id.applist)
recyclerView.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
adapter = RecyclerAdapter()
recyclerView.adapter = adapter
recyclerView.setBackgroundColor(Color.BLUE)
recyclerView.addOnItemTouchListener(
RecyclerItemClickListener(context, recyclerView, object : RecyclerItemClickListener.OnItemClickListener {
val intent = Intent(context, MusicPlayer::class.java)
override fun onItemClick(view: View, position: Int) {
//Test if Activity already Exists or has to be started
if(intent.resolveActivityInfo(packageManager,0) != null) {
startActivity(intent.setFlags(FLAG_ACTIVITY_REORDER_TO_FRONT))
Log.i("Act","Old")
}else{
//Start the MusicPlayer Activity
startActivity(intent)
Toast.makeText(context, "MusikPlayer gestarte bitte kurz warten", Toast.LENGTH_LONG).show()
}
}
override fun onLongItemClick(view: View, position: Int) {
//Test if Activity already Exists or has to be started
if(intent.resolveActivityInfo(packageManager,0) != null) {
startActivity(intent.setFlags(FLAG_ACTIVITY_REORDER_TO_FRONT))
Log.i("Act","Old")
}else{
//Start the MusicPlayer Activity
startActivity(intent)
Toast.makeText(context, "MusikPlayer gestarte bitte kurz warten", Toast.LENGTH_LONG).show()
}
}
})
)
}
fun getPermissions(){
val permissions = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
if(permissions != PackageManager.PERMISSION_GRANTED){
permissionRequest()
}
}
fun permissionRequest(){
ActivityCompat.requestPermissions(this, arrayOf(
Manifest.permission.READ_EXTERNAL_STORAGE
),RECORD_REQUEST_CODE)
}

}

Reply
  • Forum posts: 211

Aug 14, 2018 2:51:50 PM via Website

have a look on service. i think using service it can be handle

Helpful?
Reply

Recommended articles