Android开发本地及网络Mp3音乐播放器(三)MainActivity(主界面)

MainActivity(主界面)



PS:写这个笔记的的时候,只完成了本地音乐列表的加载。如果需要实现网络的MainActivity,请看后面的笔记。



实现功能

加载本地音乐

Mp3Info.java (vo:value object 值对象,mp3info,set,get方法)

MediaUtils.java(utils 工具类,媒体工具类)

MyMusicListAdapter.java(adapter 适配器,本地音乐列表适配器)

MyMusicListFragment(本地音乐列表”小界面”,顺便推荐一篇文章 -- android之Fragment(官网资料翻译) )

activity_main.xml

my_music_list_layout.xml

item_misic_layout.xml





Mp3Info.java 如下:

package com.iwanghang.drmplayer.vo;

/**
 * Created by iwanghang on 16/4/16.
 */
public class Mp3Info {

    private long id;
    private String title;//歌名
    private String artist;//艺术家
    private String album;//专辑
    private long albumId;//专辑id
    private long duration;//时长
    private long size;//大小
    private String url;//路径
    private int isMusic;//是否为音乐

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    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 getAlbum() {
        return album;
    }

    public void setAlbum(String album) {
        this.album = album;
    }

    public long getAlbumId() {
        return albumId;
    }

    public void setAlbumId(long albumId) {
        this.albumId = albumId;
    }

    public long getDuration() {
        return duration;
    }

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

    public long getSize() {
        return size;
    }

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

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getIsMusic() {
        return isMusic;
    }

    public void setIsMusic(int isMusic) {
        this.isMusic = isMusic;
    }


    @Override
    public String toString() {
        return "Mp3Info{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", artist='" + artist + '\'' +
                ", album='" + album + '\'' +
                ", albumId=" + albumId +
                ", duration=" + duration +
                ", size=" + size +
                ", url='" + url + '\'' +
                ", isMusic=" + isMusic +
                '}';
    }
}

MediaUtils.java 如下:

package com.iwanghang.drmplayer.utils;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import com.iwanghang.drmplayer.vo.Mp3Info;

import java.util.ArrayList;


/**
 * Created by iwanghang on 16/4/16.
 */
public class MediaUtils {

    // 获取专辑封面的Uri
    private static final Uri albumArtUri = Uri
            .parse("content://media/external/audio/albumart");

    /**
     * 根据歌曲id查询歌曲信息
     */
    public static Mp3Info getMp3Info(Context context, long _id){
        System.out.println("MediaUtils.java #0 : " + MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,
                MediaStore.Audio.Media._ID + "=" + _id, null,
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

        Mp3Info mp3Info = null;

        if(cursor.moveToNext()){
            mp3Info = new Mp3Info();
            long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));//音乐id
            String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));//音乐标题
            String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));//艺术家
            String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));//专辑
            long albumid = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));//专辑id
            long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));//时长
            long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));//文件大小
            String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));//文件路径
            int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));//是否为音乐
            if(isMusic != 0){
                mp3Info.setId(id);
                mp3Info.setTitle(title);
                mp3Info.setArtist(artist);
                mp3Info.setAlbum(album);
                mp3Info.setAlbumId(albumid);
                mp3Info.setDuration(duration);
                mp3Info.setSize(size);
                mp3Info.setUrl(url);
                System.out.println("MediaUtils.java #101 : title = " + title + " | artist = " + artist + " | duration = " + duration);
                System.out.println("MediaUtils.java #102 : id = " + id + " | album = " + album + " | size = " + size);
                System.out.println("MediaUtils.java #103 : url = " + url);
            }
        }
        cursor.close();
        return mp3Info;
    }

    /**
     * 用于从数据库中查询歌曲的信息,保存在List当中
     */
    public static long[] getMp3InfoIds(Context context){
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                new String[] {MediaStore.Audio.Media._ID},
                MediaStore.Audio.Media.DURATION + ">=10000", null,
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
        long[] ids = null;
        if(cursor != null){
            ids = new long[cursor.getCount()];
            for (int i = 0;i getMp3Infos(Context context) {
        System.out.println("MediaUtils.java #2 : " + MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,
                MediaStore.Audio.Media.DURATION + ">=10000", null,
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

        ArrayList mp3Infos = new ArrayList();
        System.out.println("MediaUtils.java #3 :cursor.getCount() : " + cursor.getCount());
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToNext();
            Mp3Info mp3Info = new Mp3Info();
            long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));//音乐id
            String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));//音乐标题
            String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));//艺术家
            String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));//专辑
            long albumid = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));//专辑id
            long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));//时长
            long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));//文件大小
            String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));//文件路径
            int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));//是否为音乐
            if (isMusic != 0) {
                mp3Info.setId(id);
                mp3Info.setTitle(title);
                mp3Info.setArtist(artist);
                mp3Info.setAlbum(album);
                mp3Info.setAlbumId(albumid);
                mp3Info.setDuration(duration);
                mp3Info.setSize(size);
                mp3Info.setUrl(url);
                mp3Infos.add(mp3Info);
                System.out.println("MediaUtils.java #401 : title = " + title + " | artist = " + artist + " | duration = " + duration);
                System.out.println("MediaUtils.java #402 : id = " + id + " | album = " + album + " | size = " + size);
                System.out.println("MediaUtils.java #403 : url = " + url);
                System.out.println("MediaUtils.java #404 : mp3Infos = " + mp3Infos.size());
            }
        }
        cursor.close();
        System.out.println("MediaUtils.java #405 : mp3Infos = " + mp3Infos.size());
        return mp3Infos;
    }

    /**
     * 格式化时间,将毫秒转换为分:秒格式
     */
    public static String formatTime(long time){
        String min = time / (1000*60) + "";
        String sec = time % (1000*60) + "";
        if (min.length()<2){
            min = "0" + time / (1000*60) + "";
        }else {
            min = time / (1000*60) + "";
        }
        if (sec.length() == 4){
            sec = "0" + time % (1000*60) + "";
        }else if (sec.length() == 3){
            sec = "00" + time % (1000*60) + "";
        }else if (sec.length() == 2){
            sec = "000" + time % (1000*60) + "";
        }else if (sec.length() == 1) {
            sec = "0000" + time % (1000 * 60) + "";
        }
        return min + ":" + sec.trim().substring(0,2);
    }

}

MyMusicListAdapter.java 如下:

package com.iwanghang.drmplayer.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.iwanghang.drmplayer.R;
import com.iwanghang.drmplayer.utils.MediaUtils;
import com.iwanghang.drmplayer.vo.Mp3Info;

import java.util.ArrayList;

/**
 * Created by iwanghang on 16/4/16.
 */
public class MyMusicListAdapter extends BaseAdapter{

    private Context ctx;
    private ArrayList mp3Infos;

    public MyMusicListAdapter(Context ctx, ArrayList mp3Infos){
        this.ctx = ctx;
        this.mp3Infos = mp3Infos;
        System.out.println("MyMusicListAdapter.java #0 : ctx = " + ctx + ",mp3Infos = " + mp3Infos.size());
    }

    public ArrayList getMp3Infos() {
        System.out.println("MyMusicListAdapter.java #1 : public ArrayList getMp3Infos() {");
        return mp3Infos;
    }

    public void setMp3Infos(ArrayList mp3Infos) {
        System.out.println("MyMusicListAdapter.java #2 : public void setMp3Infos(ArrayList mp3Infos) {");
        this.mp3Infos = mp3Infos;
    }


    @Override
    public int getCount() {
        System.out.println("MyMusicListAdapter.java #3 : public int getCount() {" + mp3Infos.size());
        //return mp3Infos.size();
        return mp3Infos.size();
    }

    @Override
    public Object getItem(int position) {
        System.out.println("MyMusicListAdapter.java #4 : public Object getItem(int position) {");
        return mp3Infos.get(position);
    }

    @Override
    public long getItemId(int position) {
        System.out.println("MyMusicListAdapter.java #5 : public long getItemId(int position) {");
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        System.out.println("MyMusicListAdapter.java #6 : public View getView ");
        ViewHolder vh;
        if(convertView==null){
            convertView = LayoutInflater.from(ctx).inflate(R.layout.item_music_list,null);
            vh = new ViewHolder();
            vh.textView1_title = (TextView) convertView.findViewById(R.id.textView1_title);
            vh.textView2_singer = (TextView) convertView.findViewById(R.id.textView2_singer);
            vh.textView3_time = (TextView) convertView.findViewById(R.id.textView3_time);
            System.out.println("MyMusicListAdapter.java #7 : textView1_title = " + vh.textView1_title);
            convertView.setTag(vh);
        }
        vh = (ViewHolder) convertView.getTag();
        Mp3Info mp3Info = mp3Infos.get(position);
        vh.textView1_title.setText(mp3Info.getTitle());
        vh.textView2_singer.setText(mp3Info.getArtist());
        vh.textView3_time.setText(MediaUtils.formatTime(mp3Info.getDuration()));
        return convertView;
    }

    static class ViewHolder{
        TextView textView1_title;
        TextView textView2_singer;
        TextView textView3_time;
    }
}

//inflate(R.layout.item_music_list,null);

MyMusicListFragment 如下:

package com.iwanghang.drmplayer;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.iwanghang.drmplayer.adapter.MyMusicListAdapter;
import com.iwanghang.drmplayer.utils.MediaUtils;
import com.iwanghang.drmplayer.vo.Mp3Info;

import java.util.ArrayList;

/**
 * Created by iwanghang on 16/4/16.
 */
public class MyMusicListFragment extends Fragment {

    private ListView listView_my_music;
    private ArrayList mp3Infos;
    private MyMusicListAdapter myMusicListAdapter;

    private MainActivity mainActivity;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mainActivity = (MainActivity) context;
    }

    public static MyMusicListFragment newInstance() {
        MyMusicListFragment my = new MyMusicListFragment();
        return my;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_music_list_layout,null);
        listView_my_music = (ListView) view.findViewById(R.id.listView_my_music);
        loadData();
        return view;
//        return listView_my_music;
    }

    /**
     * 加载本地音乐列表
     */
    private void loadData(){
        mp3Infos = MediaUtils.getMp3Infos(mainActivity);
        myMusicListAdapter = new MyMusicListAdapter(mainActivity,mp3Infos);
        listView_my_music.setAdapter(myMusicListAdapter);
    }

    public ListView getListView_my_music() {
        return listView_my_music;
    }

    public ArrayList getMp3Infos() {
        return mp3Infos;
    }

    public MyMusicListAdapter getMyMusicListAdapter() {
        return myMusicListAdapter;
    }

    public MainActivity getMainActivity() {
        return mainActivity;
    }
}

activity_main.xml 如下:





    

    
        

    
        
        
        
        
        
        
        
        

        
            
            
            
            
            
            
            

        
            
            
            
            
            
            
            

        
            
            
            
            
            
            
            

        
            
            
            
            
            
            
            

        
            
            
            
            
            
            
            

        
            
            
            
            
            
            
            
    

my_music_list_layout.xml 如下:




    

    

        

        

            

            
        

        
            
            
        
    



item_misic_layout.xml 如下:




    
    

        

        

    

    




你可能感兴趣的:(Android,Android,Studio,开发本地网络音乐播放器,android,android,studio,界面,网络,音乐)