如何从存储中添加歌曲列表并将其显示在我的片段之一上 [英] How to add list of songs from storage and display it on one of my fragments

查看:59
本文介绍了如何从存储中添加歌曲列表并将其显示在我的片段之一上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在片段Java和xml以及mainActivity Java和xml之一中添加歌曲列表.

How do i add list of songs inside one of my fragment java and xml and mainActivity java and xml.

这是与添加歌曲相关的其他一些Java类

This are some other java classes related to adding the songs

1- Song.java

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

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

3- SongAdapter.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;
}
}

推荐答案

您已经有了适配器,因此现在您需要一个listview来处理数据.

You have adapter so now you need to have a listview to handle the data.

  • 首先,请在您的 Fragment
  • 布局中包括ListView小部件
  • 第二,将适配器设置为您的 listview

您的适配器应使用 viewholder 来提高性能

Your adapter should use viewholder to improve performance

这篇关于如何从存储中添加歌曲列表并将其显示在我的片段之一上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆