Android studio怎么读取音乐

1、在新建的工程里先新建三个package,分别命名为adapter,entity,fragment;

2、在entity中创建新的Activity文件定义Music的实体类:title、aritist、album、length;
注意:用public修饰在后面可以直接获取,如果用private修饰,则需要使用set、get方法;

3、创建行布局文件,在其中设置两个TextView、一个ImageView并附好id,分别表示歌名、作者和专辑头像;在main布局文件中中使用LinearLayout,设计本地音乐和在线音乐,同时定义ViewPager;

4、在MainActivity中定义在main布局文件中定义好的三种类型的对象:
private TextView localTV;
private TextView onlineTV;
private ViewPager viewPager;
为了方便,我们可以定义一个bindID()方法,然后对其中两个需要用到的值分别绑定main布局文件中的id。
bindID();
private void bindID() {
titleTV = findViewById(R.id.music_title);
aritistTV = findViewById(R.id.music_artist);
}

5、在adapter包中新建Music适配器,获取方法,定义上下文环境context,音乐列表List,使用public方法获取上下文和列表文件。
注意:在第一个方法后要return列表的长度;
@Override
public int getCount() {
return mymusicList.size();
}

接着使用缓存原理对适配器传值赋值:
注意:在最后一个方法里一定要要return view
public View getView(int position, View convertView, ViewGroup viewGroup) {
View view = null;
ViewHoder viewHoder = null;
if (convertView == null) {
view = LayoutInflater.from(context).inflate(R.layout.music_item, null);
viewHoder = new ViewHoder();
viewHoder.titleTV = view.findViewById(R.id.music_item_title);
viewHoder.aristTV = view.findViewById(R.id.music_item_aritst);
viewHoder.albumImaview = view.findViewById(R.id.music_item_bmp);

  view.setTag(viewHoder);
    } else {
        view = convertView;
        viewHoder = (ViewHoder) view.getTag();//强制类型转换
    }
    Mymusic mymusic = mymusicList.get(position);
    viewHoder.titleTV.setText(mymusic.title);
    viewHoder.aristTV.setText(mymusic.aritist + " - " + mymusic.album);

    if(mymusic.albumBmp!=null){      (专辑头像的获取)

        viewHoder.albumImaview.setImageBitmap(mymusic.albumBmp);
    }else{
      viewHoder.albumImaview.setImageResource(R.mipmap.ic_launcher);
    }

    return view;   
}
class ViewHoder {
    TextView titleTV;
    TextView aristTV;
    ImageView albumImaview;
}

}

6、为了实现第一个“本地音乐”和“在线音乐”界面的跳转和滑动,我们需要在fragment文件中创建两个Fragment,分别是LocalFragment和OnlineFragment。定义MusicPager适配器,继承FragmentPagerAdapter方法,然后获取方法,定义
private List fragmentList;
使用构造方法,获取列表文件。
注意:将方法中两个return的值做修改;
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}

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

附带的行布局文件中,因为我们需要使用到“本地音乐”,所以要在fragment_local行布局文件中,设计listview。接着在LocalFragment中定义本地音乐列表:
private ListView localListView;
public List mymusicList;
在MainActivity中将Fragment列表初始化,让主方法实现借口设置监听事件View.OnClickListener方法,对两个页面进行初始化,传值,绑定适配器
localTV.setOnClickListener(this);
onlineTV.setOnClickListener(this);
LocalFragment localFragment = new LocalFragment();
OnlineFragment onlineFragment = new OnlineFragment();
fragmentList.add(localFragment);
fragmentList.add(onlineFragment);
MusicPagerAdapter pagerAdapter = new MusicPagerAdapter(getSupportFragmentManager(),fragmentList);
viewPager.setAdapter(pagerAdapter);
点击事件
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.main_local_tv:
viewPager.setCurrentItem(0); //页面进行滑动时,使用setCurrentItem
break;
case R.id.main_online_tv:
viewPager.setCurrentItem(1);
break;
default:
break;
}
}
在LocalFragment实现
public void onItemClick(AdapterView

Android studio怎么读取音乐

CSDN 社区图书馆,开张营业!
Android studio怎么读取音乐
深读计划,写书评领图书福利~