我是android开发的新手。我想用原始文件夹创建一个音乐播放器。我创建了一个包含音乐列表的ListView并设置了MediaPlayer。它有效,单击ListView项目时播放音乐。现在我想添加MediaController来控制播放这些音乐(暂停,播放下一个和上一个等)。阅读有关此主题的许多教程,包括https://code.tutsplus.com/tutorials/create-a-music-player-on-android-project-setup--mobile-22764?ec_unit=translation-info-language。
我写了一些代码来添加MediaController,但它没有在屏幕上显示。 Logcat中没有任何错误。
这是MainActivity
public class MainActivity extends Activity implements MediaController.MediaPlayerControl{
ListView listSongs;
ArrayList<Song> arrayList;
ArrayAdapter<Song> mAdapter;
MediaPlayer mPlayer;
private MediaController controller;
@Override
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_music_category);
listSongs = findViewById(R.id.list_songs);
arrayList = new ArrayList<>();
arrayList.add(new Song("Song1", R.raw.song1));
arrayList.add(new Song("Song2", R.raw.song2));
mAdapter = new ArrayAdapter<>(this, R.layout.text_view_forlist, arrayList);
listSongs.setAdapter(mAdapter);
mPlayer = new MediaPlayer();
controller = new MediaController(this);
controller.setMediaPlayer(this);
listSongs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(mPlayer != null){
mPlayer.release();
}
mPlayer = MediaPlayer.create(MainActivity.this, arrayList.get(position).getmSongResourceId());
mPlayer.start();
controller.show();
}
});
}
@Override
public void start() {
mPlayer.start();
}
@Override
public void pause() {
mPlayer.pause();
}
@Override
public int getDuration() {
return mPlayer.getDuration();
}
@Override
public int getCurrentPosition() {
return mPlayer.getCurrentPosition();
}
@Override
public void seekTo(int pos) {
mPlayer.seekTo(pos);
}
@Override
public boolean isPlaying() {
return mPlayer.isPlaying();
}
@Override
public int getBufferPercentage() {
return 0;
}
@Override
public boolean canPause() {
return true;
}
@Override
public boolean canSeekBackward() {
return true;
}
@Override
public boolean canSeekForward() {
return true;
}
@Override
public int getAudioSessionId() {
return 0;
}
@Override
protected void onStop() {
super.onStop();
controller.hide();
mPlayer.stop();
mPlayer.release();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//the MediaController will hide after 3 seconds - tap the screen to make it appear again
controller.show();
return true;
}
}
和activity_music_category.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
<ListView
android:id="@+id/list_songs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="@android:color/transparent"
android:padding="0dp"
android:paddingEnd="10dp"
android:paddingStart="10dp" >
</ListView>
单击ListView项目时,如何在底部显示MediaController?我想也许controller.show();
可能不对,或者我错过了一些方法或问题更全球化。谢谢你的帮助。
答案 0 :(得分:0)
你没有附加MediaController! 试试这个:
//you can use any view like ListView (I mean listSongs) in this case instead of mView
controller.setAnchorView(mView);