gpt4 book ai didi

java - 如何使用MediaPlayer将MediaController添加到音乐列表?

转载 作者:行者123 更新时间:2023-12-03 01:38:01 25 4
gpt4 key购买 nike

我是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();位置不正确,或者我错过了一些方法,或者问题更加普遍。谢谢你的帮助。

最佳答案

您没有附加MediaController!
试试这个:

//you can use any view like ListView (I mean listSongs) in this case instead of mView
controller.setAnchorView(mView);

关于java - 如何使用MediaPlayer将MediaController添加到音乐列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50456936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com