如何跳过媒体播放器正在播放的歌曲

时间:2018-07-02 14:03:54

标签: android kotlin android-mediaplayer

我希望媒体播放器可以跳过这首歌曲(如果以前正在播放)。 如果我要重置媒体播放器,那么它不会跳过而是整个应用程序都会重置。每当调用此活动时,我的媒体播放器就会重置。我希望我的媒体播放器只能在规定的条件下重置。这是我重写的onActivityCreated方法,在其中实现了该部分以在应用程序中播放歌曲:

    override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    Statified.favouriteContent = BeatsDatabase(Statified.myActivity)
    Statified.currentSongHelper = CurrentSongHelper()
    Statified.currentSongHelper?.isPlaying = true
    Statified.currentSongHelper?.isLoop = false
    Statified.currentSongHelper?.isShuffle = false


    var path: String? = null
    var _songTitle: String? = null
    var _songArtist: String? = null
    var songId: Long = 0
    try {
        path = arguments?.getString("path")
        _songTitle = arguments?.getString("songTitle")
        _songArtist = arguments?.getString("songArtist")
        songId = arguments!!.getInt("songID").toLong()
        Statified.currentPosition = arguments!!.getInt("position")
        Statified.fetchSongs = arguments?.getParcelableArrayList("songData")
        if (Statified.currentSongHelper?.songId != songId) {
            Statified.currentSongHelper?.songPath = path
            Statified.currentSongHelper?.songTitle = _songTitle
            Statified.currentSongHelper?.songArtist = _songArtist
            Statified.currentSongHelper?.songId = songId
            Statified.currentSongHelper?.currentPosition = Statified.currentPosition
            Staticated.updateTextViews(Statified.currentSongHelper?.songTitle as String, Statified.currentSongHelper?.songArtist as String)
        }


    } catch (e: Exception) {
        e.printStackTrace()
    }


    var fromfavBottomBar = arguments?.get("FavBottomBar") as? String
    if (fromfavBottomBar != null) {
        Statified.mediaPlayer = FavouriteFragment.Statified.mediaPlayer
    } else {
        Statified.mediaPlayer = MediaPlayer()
        Statified.mediaPlayer?.setAudioStreamType(AudioManager.STREAM_MUSIC)
        try {
            Statified.mediaPlayer?.setDataSource(Statified.myActivity, Uri.parse(path))
            Statified.mediaPlayer?.prepare()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        Statified.mediaPlayer?.start()
    }
    Staticated.processInformation(Statified.mediaPlayer as MediaPlayer)

    if (Statified.currentSongHelper?.isPlaying as Boolean) {
        //Statified.mediaPlayer?.pause()
        //Statified.playPauseImageButton?.setBackgroundResource(R.drawable.pause_icon)
    } else {
        Statified.mediaPlayer?.start()
        Statified.playPauseImageButton?.setBackgroundResource(R.drawable.pause_icon)
    }
    Statified.mediaPlayer?.setOnCompletionListener {
        Staticated.onSongComplete()
    }

    clickHandler()


    var visualizationHandler = DbmHandler.Factory.newVisualizerHandler(Statified.myActivity as Context, 0)
    Statified.audioVisualization?.linkTo(visualizationHandler)


    var prefsForShuffle = Statified.myActivity?.getSharedPreferences(Staticated.MY_PREFS_SHUFFLE, Context.MODE_PRIVATE)
    var isShuffleAllowed = prefsForShuffle?.getBoolean("feature", false)
    if (isShuffleAllowed as Boolean) {
        Statified.currentSongHelper?.isShuffle = true
        Statified.currentSongHelper?.isLoop = false
        Statified.shuffleImageButton?.setBackgroundResource(R.drawable.shuffle_icon)
        Statified.loopImageButton?.setBackgroundResource(R.drawable.loop_white_icon)

    } else {
        Statified.currentSongHelper?.isShuffle = false
        Statified.shuffleImageButton?.setBackgroundResource(R.drawable.shuffle_white_icon)
    }
    var prefsForLoop = Statified.myActivity?.getSharedPreferences(Staticated.MY_PREFS_LOOP, Context.MODE_PRIVATE)
    var isLoopAllowed = prefsForLoop?.getBoolean("feature", false)
    if (isLoopAllowed as Boolean) {
        Statified.currentSongHelper?.isShuffle = false
        Statified.currentSongHelper?.isLoop = true
        Statified.shuffleImageButton?.setBackgroundResource(R.drawable.shuffle_white_icon)
        Statified.loopImageButton?.setBackgroundResource(R.drawable.loop_icon)

    } else {
        Statified.currentSongHelper?.isLoop = false
        Statified.shuffleImageButton?.setBackgroundResource(R.drawable.shuffle_white_icon)
    }
    if ((Statified.favouriteContent?.checkIfIdExists(Statified.currentSongHelper?.songId?.toInt() as Int) as Boolean)) {
        Statified.fab?.setBackgroundResource(R.drawable.favorite_on)
    } else {
        Statified.fab?.setBackgroundResource(R.drawable.favorite_off)
    }

}

0 个答案:

没有答案