Android:MediaController在播放音频时没有显示。

时间:2014-09-18 03:54:09

标签: android media-player mediacontroller

我正在制作一个播放设备上可用歌曲的MusicPlayer应用程序。我的问题是当我正在播放MediaController没有显示的歌曲时。我已经尝试了所有可能的方法,但MediaController没有显示控件的播放,下一个,上一个等等。

这是我的代码:

      public class MainActivity extends Activity implements MediaPlayerControl{
         @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    setContentView(R.layout.activity_main);
    listView = (ListView)findViewById(R.id.song_list);
    songList = new ArrayList<SongView>();
    getSongList();
    Collections.sort(songList, new Comparator<SongView>(){
          public int compare(SongView a, SongView b){
            return a.getTitle().compareTo(b.getTitle());
          }
        });
    SongAdapter songAdt = new SongAdapter(this, songList);
    listView.setAdapter(songAdt);


    setController();

   }


   private void setController(){
      //set the controller up
    controller = new MusicController(this);
    controller.setPrevNextListeners(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            playNext();
          }
        }, new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            playPrev();
          }
        }); 
    controller.setMediaPlayer(this);
    controller.setAnchorView(findViewById(R.id.song_list));
    controller.setEnabled(true);
    //controller.show();
}
//play next
private void playNext(){
  musicSrv.playNext();
  controller.show(0);
}

//play previous
private void playPrev(){
  musicSrv.playPrev();
  controller.show(0);
}
@Override
protected void onStop() {
    super.onStop();
    controller.hide();

}

请帮我解决这个问题。

3 个答案:

答案 0 :(得分:2)

在您的代码中,您有:

controller.setMediaPlayer(this);
controller.setAnchorView(findViewById(R.id.song_list));
controller.setEnabled(true);
//controller.show();

问题是,您需要取消注释代码。 .show是必需的。为了避免典型的“android.view.WindowLeaked”错误,您需要在加载布局后调用上面的代码。我个人使用下面的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //You need a reference to a layout element. I used the root element
    root = (LinearLayout) findViewById(R.id.root);

    ViewTreeObserver vto = root.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new  ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            //Call your controller set-up now that the layout is loaded
            setController();
        }
    });

}

答案 1 :(得分:0)

我正在向您展示我的代码供您参考,试试这个,肯定会有效:

public class AudioPlayer extends Activity implements OnPreparedListener, MediaController.MediaPlayerControl{
private static final String TAG = "AudioPlayer";

public static final String AUDIO_FILE_NAME = "audioFileName";

private MediaPlayer mediaPlayer;
private MediaController mediaController;
private String audioFile;

private Handler handler = new Handler();

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio_player);

audioFile = this.getIntent().getStringExtra(AUDIO_FILE_NAME);
((TextView)findViewById(R.id.now_playing_text)).setText(audioFile);

mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(this);

mediaController = new MediaController(this);

try {
  mediaPlayer.setDataSource(audioFile);
  mediaPlayer.prepare();
  mediaPlayer.start();
} catch (IOException e) {
  Log.e(TAG, "Could not open file " + audioFile + " for playback.", e);
}

}

@Override
protected void onStop() {
super.onStop();
mediaController.hide();
mediaPlayer.stop();
mediaPlayer.release();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
 //the MediaController will hide after 3 seconds - tap the screen to make it appear again
 mediaController.show();
 return false;
}

//--MediaPlayerControl methods----------------------------------------------------
 public void start() {
  mediaPlayer.start();
 }

public void pause() {
  mediaPlayer.pause();
}

public int getDuration() {
  return mediaPlayer.getDuration();
}

public int getCurrentPosition() {
  return mediaPlayer.getCurrentPosition();
}

public void seekTo(int i) {
 mediaPlayer.seekTo(i);
}

public boolean isPlaying() {
  return mediaPlayer.isPlaying();
}

public int getBufferPercentage() {
  return 0;
}

public boolean canPause() {
  return true;
}

public boolean canSeekBackward() {
  return true;
}

public boolean canSeekForward() {
  return true;
}
//--------------------------------------------------------------------------------

 public void onPrepared(MediaPlayer mediaPlayer) {
 Log.d(TAG, "onPrepared");
 mediaController.setMediaPlayer(this);
 mediaController.setAnchorView(findViewById(R.id.main_audio_view));

 handler.post(new Runnable() {
   public void run() {
     mediaController.setEnabled(true);
     mediaController.show();
   }
 });
}
}

答案 2 :(得分:0)

我认为作者遵循教程来实现音乐播放器,请参阅此处:http://code.tutsplus.com/tutorials/create-a-music-player-on-android-user-controls--mobile-22787

我一开始也遇到了同样的问题,但后来我发现它会在你完全按照教程完成时正确显示MediaController。

关键是你不能在onCreate()中调用controller.show(),你必须在活动完全创建后独立调用controller.show()。

与教程一样,您可以在songPicked()中调用controller.show():

public void songPicked(View view) {
    musicService.setSong(Integer.parseInt(view.getTag().toString()));
    musicService.playSong();
    musicController.show();
}

2015/9/6更新:
今天我找到另一个解决方案,如果你想在onCreate()中显示mediaController,这样做,我已经尝试过它可以工作:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_music_play);

    //....

    // yes, it works!
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            musicController.show();
        }
    });
}