Reputation: 1435
I am making a MusicPlayer app which play the songs available on the device.My problem is when i am playing a song MediaController is not showing. I have tried all the possible ways but MediaController not showing the control with play,next, previous etc.
Here is my code:
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();
}
Please help me to solve this.
Upvotes: 1
Views: 5413
Reputation: 5753
I think the author follows the tutorial to implement a music player refer here: http://code.tutsplus.com/tutorials/create-a-music-player-on-android-user-controls--mobile-22787.
I also meet the same problem at first, but then I found it will show MediaController correctly while you follow the tutorial totally at last.
The key point is that you can't call controller.show() in onCreate(), you must call controller.show() standalone after the activity totally created.
Like the tutorial, you can call controller.show() in the songPicked():
public void songPicked(View view) {
musicService.setSong(Integer.parseInt(view.getTag().toString()));
musicService.playSong();
musicController.show();
}
2015/9/6 update:
today I find another solution if you want to show mediaController in onCreate(), do like this, I has tried and it works:
@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();
}
});
}
Upvotes: 0
Reputation: 2553
In your code you have:
controller.setMediaPlayer(this);
controller.setAnchorView(findViewById(R.id.song_list));
controller.setEnabled(true);
//controller.show();
The thing is, you need to uncomment your code. The .show is required. To avoid the typical "android.view.WindowLeaked" Error, you need to call the above code AFTER the layout has been loaded. Personally I have used the code below:
@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();
}
});
}
Upvotes: 2
Reputation: 7641
I'm showing my code to you for your reference, try this, will surely work :
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();
}
});
}
}
Upvotes: 0