3.9 练习

public class Music {
    
    

    private String title;
    private String artist;
    private String duration;
    private String data;
    private String size;

    public Music() {
    
    
    }

    public Music(String title, String artist, String duration, String data, String size) {
    
    
        this.title = title;
        this.artist = artist;
        this.duration = duration;
        this.data = data;
        this.size = size;
    }

    public String getTitle() {
    
    
        return title;
    }

    public void setTitle(String title) {
    
    
        this.title = title;
    }

    public String getArtist() {
    
    
        return artist;
    }

    public void setArtist(String artist) {
    
    
        this.artist = artist;
    }

    public String getDuration() {
    
    
        return duration;
    }

    public void setDuration(String duration) {
    
    
        this.duration = duration;
    }

    public String getData() {
    
    
        return data;
    }

    public void setData(String data) {
    
    
        this.data = data;
    }

    public String getSize() {
    
    
        return size;
    }

    public void setSize(String size) {
    
    
        this.size = size;
    }

    @Override
    public String toString() {
    
    
        return "Music{" +
                "title='" + title + '\'' +
                ", artist='" + artist + '\'' +
                ", duration='" + duration + '\'' +
                ", data='" + data + '\'' +
                ", size='" + size + '\'' +
                '}';
    }
}





public class MusicAdapter extends BaseAdapter {
    
    

    private List<Music> lists;
    private Context context;

    public MusicAdapter(List<Music> list, Context context) {
    
    
        this.lists = list;
        this.context = context;
    }

    @Override
    public int getCount() {
    
    
        return lists.size();
    }

    @Override
    public Object getItem(int i) {
    
    
        return lists.get(i);
    }

    @Override
    public long getItemId(int i) {
    
    
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
    
    

        ViewHolder holder = null;
        if (view == null){
    
    
            holder = new ViewHolder();
            view = View.inflate(context, R.layout.song_layout,null);
            holder.text_artist = view.findViewById(R.id.text_artist);
            holder.text_duration = view.findViewById(R.id.text_duration);
            holder.text_title = view.findViewById(R.id.text_title);
            view.setTag(holder);
        }else {
    
    
            holder = (ViewHolder) view.getTag();
        }
        holder.text_title.setText(lists.get(i).getTitle());
        holder.text_duration.setText(lists.get(i).getDuration());
        holder.text_artist.setText(lists.get(i).getArtist());
        return view;
    }

    private class ViewHolder{
    
    
        private TextView text_title,text_artist,text_duration;
    }

}





public class MusicUtil {
    
    

    public static List<Music> getMusic(Context context){
    
    

        List<Music> lists = new ArrayList<>();
        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

        ContentResolver contentResolver = context.getContentResolver();

        Cursor cursor = contentResolver.query(uri, null, null, null, null);
        if (cursor != null){
    
    
            while (cursor.moveToNext()){
    
    

                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                String size = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));
                Music music = new Music(title, artist, duration, data, size);
                lists.add(music);

            }
            cursor.close();
            return lists;
        }

        return null;

    }

}





public class zhengze  {
    
    

    public static boolean isgod(String path){
    
    

        Pattern compile = Pattern.compile( "[a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}");

        Matcher matcher = compile.matcher(path);

        return matcher.matches();

    }
}





<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Main4Activity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9">
    </ListView>

    <SeekBar
        android:id="@+id/seek"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/button_start"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="播放"
            android:textSize="25dp"
            android:onClick="click"
            />

        <Button
            android:id="@+id/button_pause"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="暂停"
            android:textSize="25dp"
            android:onClick="click"/>

        <Button
            android:id="@+id/button_last"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="上一首"
            android:textSize="25dp"
            android:onClick="click"/>

        <Button
            android:id="@+id/button_next"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="下一首"
            android:textSize="25dp"
            android:onClick="click"/>

    </LinearLayout>

</LinearLayout>

猜你喜欢

转载自blog.csdn.net/weixin_46367373/article/details/104763714
3.9