2
SD 카드의 노래를 표시하는 여러 가지 활동 및 단편이 있으며 그 중 하나를 클릭하면 재생됩니다. 문제는 이들 각각의 활동과 단편에서 미디어 플레이어의 새로운 인스턴스를 만들어야한다는 것입니다. 따라서 한 활동에서 노래를 연주하고 다른 활동에서 다른 노래를 연주하면 두 노래가 동시에 연주됩니다. 싱글 톤 클래스에 대해 읽었고 미디어 플레이어를 정적으로 만들었지 만 더 많은 정보가 필요합니다. 아래는 제 코드입니다.모든 활동 및 조각에서 MediaPlayer의 단일 인스턴스를 사용하는 방법은 무엇입니까?
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;
}
tbh라는 서비스를 사용할 계획이 아닙니다. 그게 나쁜가요? 그리고 서비스보다 간단한 다른 솔루션을 나에게 말할 수 있습니까? – Rektirino
예, 서비스 없이도 가능합니다. 미디어 플레이어가베이스 활동에 있다면 인스턴스를 만듭니다. 조각의 모든 활동에서 해당 인스턴스에 액세스 할 수 있습니다. –
미디어 플레이어 용 싱글 톤 클래스를 만들었습니다. 위 코드가 게시되었습니다. 하지만 문제는 모든 activiites 및 조각 setOnCompletionListeners() 구현 한 있지만 최신 구현 된 setOnCompletionListener() 작동합니다. 이유가 무엇인지 모르겠다 – Rektirino