如何在所有活动和片段中使用 MediaPlayer 的单个实例?
Posted
技术标签:
【中文标题】如何在所有活动和片段中使用 MediaPlayer 的单个实例?【英文标题】:How to use single instance of MediaPlayer across all activities and fragments? 【发布时间】:2018-04-10 12:57:51 【问题描述】:我有几个显示来自 SD 卡的歌曲的活动和片段,当我点击其中任何一个时,它就会播放。问题是我必须在每个活动和片段中创建一个新的媒体播放器实例。因此,如果我在一项活动中播放一首歌曲并在另一项活动中播放另一首歌曲,这两首歌曲将同时播放。我阅读了关于单例类和使媒体播放器静态的信息,但我需要更多信息。下面是我的代码。
public class ArtistSongAlbumSong extends AppCompatActivity
ArrayList<SongInfoModel> ArtistSongAlbumSongList = new ArrayList<>();
RecyclerView recyclerView_artistalbumsongs;
ArtistSongAlbumSongAdapter artistSongAlbumSongAdapter;
private static MediaPlayer player = new MediaPlayer();
Context isContext;
private int currentIndex;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_artistsongallsong);
recyclerView_artistalbumsongs = findViewById(R.id.recyclerView_artistSongAllSong);
isContext = ArtistSongAlbumSong.this;
Long albumid = getIntent().getExtras().getLong("album_id");
LinearLayoutManager aslinearLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView_artistalbumsongs.setLayoutManager(aslinearLayoutManager);
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
Cursor cursor = getApplicationContext().getContentResolver().query(uri, null, selection, null, null);
if (cursor != null)
if (cursor.moveToFirst())
do
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String nameArtist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
Long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
Long newAlbumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumid);
if (newAlbumId.equals(albumid))
SongInfoModel s6 = new SongInfoModel(name, nameArtist, duration, data, albumArtUri);
ArtistSongAlbumSongList.add(s6);
while (cursor.moveToNext());
cursor.close();
Collections.sort(ArtistSongAlbumSongList, new Comparator<SongInfoModel>()
@Override
public int compare(SongInfoModel lhs, SongInfoModel rhs)
return lhs.getSongName().compareTo(rhs.getSongName());
);
artistSongAlbumSongAdapter = new ArtistSongAlbumSongAdapter(getApplicationContext(), ArtistSongAlbumSongList);
artistSongAlbumSongAdapter.onItemClickListener(new ArtistSongAlbumSongAdapter.ArtistSongAlbumSongListener()
@Override
public void onClickListener(SongInfoModel songInfoModel, int position, RelativeLayout relativeLayout, View view)
MainActivity.setsongText(songInfoModel);
MainActivity.ButtonPlay();
MainActivity.PauseImage();
changeSelectedSong(position);
prepareSong(songInfoModel);
@Override
public void onLongClickListener(SongInfoModel songInfoModel, int position, RelativeLayout relativeLayout, View view)
);
recyclerView_artistalbumsongs.setAdapter(artistSongAlbumSongAdapter);
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
@Override
public void onPrepared(MediaPlayer mediaPlayer)
togglePlay(mediaPlayer);
);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
@Override
public void onCompletion(MediaPlayer mediaPlayer)
onSongCompletion1(mediaPlayer);
);
public void prepareSong(SongInfoModel song)
player.reset();
try
player.setDataSource(song.getData());
player.prepareAsync();
catch (IOException e)
e.printStackTrace();
private void togglePlay(MediaPlayer mp)
if (mp.isPlaying())
mp.stop();
mp.reset();
else
mp.start();
MainActivity.Handler(mp);
private void changeSelectedSong(int index)
// artistSongAlbumSongAdapter.notifyItemChanged(artistSongAlbumSongAdapter.getSelectedPosition());
currentIndex = index;
artistSongAlbumSongAdapter.setSelectedPosition(currentIndex);
// artistSongAlbumSongAdapter.notifyItemChanged(currentIndex);
private void onSongCompletion1(MediaPlayer mediaPlayer)
if(currentIndex + 1 < ArtistSongAlbumSongList.size())
SongInfoModel next = ArtistSongAlbumSongList.get(currentIndex +1);
changeSelectedSong(currentIndex + 1);
prepareSong(next);
MainActivity.setsongText(next);
else
SongInfoModel next1 = ArtistSongAlbumSongList.get(0);
changeSelectedSong(0);
prepareSong(ArtistSongAlbumSongList.get(0));
MainActivity.setsongText(next1);
单例类:
public class MyMediaPlayer extends MediaPlayer
private static MyMediaPlayer mpclass ;
private MyMediaPlayer()
public static MyMediaPlayer getInstance()
if (mpclass == null)
synchronized (MyMediaPlayer.class)
if (mpclass == null)
mpclass = new MyMediaPlayer();
return mpclass;
【问题讨论】:
【参考方案1】:您需要按照以下步骤操作
1) 创建一个播放音频的服务并在此处创建 Media Player 实例 2)在活动中绑定它(它可以是你的基本活动) 3)在活动中使用服务引用来获取媒体播放器实例并完成。在所有片段的活动中使用此实例
【讨论】:
我不打算使用服务。那不好吗?你能告诉我另一个比服务更简单的解决方案吗? 是的,它也可以在没有服务的情况下完成。如果媒体播放器在您的基本活动中创建实例。您也可以在片段中的所有活动中访问该实例 我为媒体播放器创建了一个单例类。上面贴的代码。但问题是我已经在所有活动和片段中实现了 setOnCompletionListeners() 但只有最新实现的 setOnCompletionListener() 有效。我不知道为什么 在基类中做同样的事情并提供回调(使用覆盖方法)子类。然后你就可以得到你想要的:) 但是如果我在基类中实现 setOnCompletionListeners(),我将如何将 arrayList、对象和所有其他东西传递给它?以上是关于如何在所有活动和片段中使用 MediaPlayer 的单个实例?的主要内容,如果未能解决你的问题,请参考以下文章
选择片段 A 时如何在 ViewPager 上不一起启动所有片段