How can I show a MediaController while playing audio in Android?

前端 未结 4 1422
一整个雨季
一整个雨季 2020-11-30 18:15

How can I show a MediaController while playing audio file? I am creating an instance of MediaController and calling its show() method,

4条回答
  •  离开以前
    2020-11-30 18:34

    I copied the code above from user229487 verbatim but couldn't get it to work. Given I'm new to Android dev, I found out the hard way that you need to put an intent as well as a few other things. I also don't exactly understand how the intent will work with a user interface (again I'm new), but will work on it.

    Few things for other noobs like me to get the code above to work (I at least got it to not crash now and play a song :) ):

    • Won't compile without this:

      @Override
      public int getAudioSessionId() {
      return 0;
      }

    • To set audio manually: (change line to:)

      public static final String AUDIO_FILE_NAME = Environment.getExternalStorageDirectory()+"/MusicFolder/Song.mp3";

    • Setup intent
      Add this line: this.getIntent().putExtra(AUDIO_FILE_NAME,AUDIO_FILE_NAME);
      Before this line: audioFile = this.getIntent().getStringExtra(AUDIO_FILE_NAME);

    Should compile and play song now.

提交回复
热议问题