Android StudioService音乐播放器

@TOC效果图代码片段主类public class MainActivity extends AppCompatActivity { ImageView img; TextView name; TextView singer; ListView lv; MyService fuwu; List<Music> list = new...
摘要由CSDN通过智能技术生成

@TOC

效果图

在这里插入图片描述

代码片段

主类

public class MainActivity extends AppCompatActivity {
   
    ImageView img;
    TextView name;
    TextView singer;
    ListView lv;
    MyService fuwu;
    List<Music> list = new ArrayList<>();
    int count = 0;
    Music music;
    ServiceConnection connection = new ServiceConnection() {
   
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
   
            fuwu = ((MyService.MyBinder) service).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
   

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        lv = findViewById(R.id.lv);
        img = findViewById(R.id.img);
        name = findViewById(R.id.name);
        singer = findViewById(R.id.singer);

        Intent intent = new Intent(this, MyService.class);
        bindService(intent, connection, BIND_AUTO_CREATE);

        getMusic();

        MyAdapter myAdapter = new MyAdapter(MainActivity.this, list);
        lv.setAdapter(myAdapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   
                fuwu.stop();
                try {
   
                    fuwu.play(list, position);
                } catch (IOException e) {
   
                    e.printStackTrace();
                }
                count = position;
                music = list.get(position);
                img.setImageBitmap(music.getBitmap());
                name.setText(music.getName());
                singer.setText(music.getSinger());
            }
        });
    }

    public void startMusic(View view) {
   
        fuwu.pause();
    }

    public void after(View view) throws IOException {
   
        if (count == 0) {
   
            count = list.size() - 1;
        } else {
   
            count--;
  • 0
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值