0
私のフラグメントのjavaとxmlとmainActivity javaとxmlのいずれかに曲のリストを追加するにはどうすればいいですか?ストレージから曲のリストを追加し、それを私のフラグメントの1つに表示する方法
この曲を
1- Song.java
public class Song {
private long id;
private String title;
private String artist;
private long duration;
public Song(long songID, String songTitle, String songArtist) {
id=songID;
title=songTitle;
artist=songArtist;
}
public long getId() {
return id;
}
public String getTitle() {
return title;
}
public String getArtist() {
return artist;
}
}
2- MusicService.java
public class MusicService extends Service implements
MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener,
MediaPlayer.OnCompletionListener {
////////////////////////////////// Declarations ////////////////////////////////////////////////////
//media player
private MediaPlayer player;
//song list
private ArrayList<Song> songs;
//current position
private int songPosn;
//binder
private final IBinder musicBind = new MusicBinder();
//title of current song
private String songTitle="";
//notification id
private static final int NOTIFY_ID=1;
//shuffle flag and random
private boolean shuffle=false;
private Random rand;
public void onCreate(){
//create the service
super.onCreate();
//initialize position
songPosn=0;
//random
rand=new Random();
//create player
player = new MediaPlayer();
//initialize
initMusicPlayer();
}
public void initMusicPlayer(){
//set player properties
player.setWakeMode(getApplicationContext(),
PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
//set listeners
player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
player.setOnErrorListener(this);
}
//pass song list
public void setList(ArrayList<Song> theSongs){
songs = theSongs;
}
//binder
public class MusicBinder extends Binder {
MusicService getService() {
return MusicService.this;
}
}
//activity will bind to service
@Override
public IBinder onBind(Intent intent) {
return musicBind;
}
//release resources when unbind
@Override
public boolean onUnbind(Intent intent){
player.stop();
player.release();
return false;
}
//////////////////////////////////////// Play a song ///////////////////////////////////////////
public void playSong(){
//play
player.reset();
//get song
Song playSong = songs.get(songPosn);
//get title
songTitle = playSong.getTitle();
//get id
long currSong = playSong.getId();
//set uri
Uri trackUri = ContentUris.withAppendedId(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
currSong);
//set the data source
try{
player.setDataSource(getApplicationContext(), trackUri);
}
catch(Exception e){
Log.e("MUSIC SERVICE", "Error setting data source", e);
}
player.prepareAsync();
}
////////////////////////////////////////////////////////////////////////////////////////////////
//set the song
public void setSong(int songIndex){
songPosn=songIndex;
}
///////////////////////////Completion,Prepared,Error////////////////////////////////////////////
@Override
public void onCompletion(MediaPlayer mp) {
//check if playback has reached the end of a track
if(player.getCurrentPosition()>0){
mp.reset();
playNext();
}
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.v("MUSIC PLAYER", "Playback Error");
mp.reset();
return false;
}
@Override
public void onPrepared(MediaPlayer mp) {
//start playback
mp.start();
//notification
Intent notIntent = new Intent(this, MainActivity.class);
notIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendInt = PendingIntent.getActivity(this, 0,
notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(this);
builder.setContentIntent(pendInt)
.setSmallIcon(R.drawable.play)
.setTicker(songTitle)
.setOngoing(true)
.setContentTitle("Playing")
.setContentText(songTitle);
Notification not = builder.build();
startForeground(NOTIFY_ID, not);
}
////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////playback methods///////////////////////////////////////////////
public int getPosn(){
return player.getCurrentPosition();
}
public int getDur(){
return player.getDuration();
}
public boolean isPng(){
return player.isPlaying();
}
public void pausePlayer(){
player.pause();
}
public void seek(int posn){
player.seekTo(posn);
}
public void go(){
player.start();
}
//skip to previous track
public void playPrev(){
songPosn--;
if(songPosn<0) songPosn=songs.size()-1;
playSong();
}
//skip to next
public void playNext(){
if(shuffle){
int newSong = songPosn;
while(newSong==songPosn){
newSong=rand.nextInt(songs.size());
}
songPosn=newSong;
}
else{
songPosn++;
if(songPosn>=songs.size()) songPosn=0;
}
playSong();
}
//toggle shuffle
public void setShuffle(){
if(shuffle) shuffle=false;
else shuffle=true;
}
@Override
public void onDestroy() {
stopForeground(true);
}
}
3- SongAdapter.java
を加えることに関連するいくつかの他のJavaクラスでありますpublic class SongAdapter extends BaseAdapter {
//song list and layout
private ArrayList<Song> songs;
private LayoutInflater songInf;
//constructor
public SongAdapter(Context c, ArrayList<Song> theSongs){
songs = theSongs;
songInf=LayoutInflater.from(c);
}
@Override
public int getCount() {
return songs.size();
}
@Override
public Song getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//map to song layout
LinearLayout songLay = (LinearLayout)songInf.inflate(R.layout.song, parent, false);
//get title and artist views
TextView songView = (TextView)songLay.findViewById(R.id.song_title);
TextView artistView = (TextView)songLay.findViewById(R.id.song_artist);
//get song using position
Song currSong = songs.get(position);
//get title and artist strings
songView.setText(currSong.getTitle());
artistView.setText(currSong.getArtist());
//set position as tag
songLay.setTag(position);
return songLay;
}
}