Android项目开发之音乐播放器

前言

    新开系列,项目开发。写这个系列的博客只是为了丰富自己对应用层的理解,所以决定用周末的时间来仿一些APP,这篇博客只是初始版本,后面持续更新,附GitHub地址。


主要功能

    最终目的是实现一个类似网易云音乐的APP


代码实现

布局文件,目前主要写的是播放界面测试代码,后续会重新替换控件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.example.app.MainActivity">

    <SeekBar
        android:id="@+id/sb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sb"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_play"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="暂停"
            android:layout_margin="5dp"/>

        <Button
            android:id="@+id/btn_stop"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="停止"
            android:layout_margin="5dp"/>

        <Button
            android:id="@+id/btn_next"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="下一首"
            android:layout_margin="5dp"/>
    </LinearLayout>

    <Button
        android:id="@+id/btn_exit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="退出应用"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

MainActivity.java

package com.example.app;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;

import android.os.Handler;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

import com.example.service.MusicService;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnStart;
    private Button btnStop;
    private Button btnExit;
    private MusicService musicService;
    private SeekBar seekBar;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        bindMusicService();
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    musicService.mediaPlayer.seekTo(seekBar.getProgress());
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            //update progress
            seekBar.setProgress(musicService.mediaPlayer.getCurrentPosition());
            seekBar.setMax(musicService.mediaPlayer.getDuration());
            handler.postDelayed(runnable,100);
        }
    };

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            musicService = ((MusicService.MusicBinder) service).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            musicService = null;
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        handler.postDelayed(runnable,100);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_play:
                if(musicService != null) {
                    if (musicService.mediaPlayer.isPlaying()) {
                        musicService.mediaPlayer.pause();
                        btnStart.setText("播放");
                    } else {
                        musicService.mediaPlayer.start();
                        btnStart.setText("暂停");
                    }
                }
                break;
            case R.id.btn_stop:
                if(musicService != null){
                    musicService.mediaPlayer.stop();
                    btnStart.setText("播放");
                }
                break;
            case R.id.btn_next:
                break;
            case R.id.btn_exit:
                handler.removeCallbacks(runnable);
                unbindService(serviceConnection);
                stopService(intent);
                this.finish();
                break;
        }
    }

    /**
     * init view
     */
    private void initView() {
        btnStart = findViewById(R.id.btn_play);
        btnStop = findViewById(R.id.btn_stop);
        btnExit = findViewById(R.id.btn_exit);
        btnStart.setOnClickListener(this);
        btnStop.setOnClickListener(this);
        btnExit.setOnClickListener(this);

        seekBar = findViewById(R.id.sb);

    }

    /**
     * bind service
     */
    private void bindMusicService() {
        intent = new Intent(MainActivity.this , MusicService.class);
        startService(intent);
        bindService(intent ,serviceConnection ,BIND_AUTO_CREATE);
    }
}

MusicService.java

package com.example.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

import com.example.app.R;

public class MusicService extends Service {

    public final IBinder binder = new MusicBinder();
    public MediaPlayer mediaPlayer;

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayer = MediaPlayer.create(this , R.raw.mymusic);
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                stopSelf();
            }
        });
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(!mediaPlayer.isPlaying()){
            mediaPlayer.start();
        }
        return START_STICKY;
    }

    public class MusicBinder extends Binder{

        public MusicService getService(){
            return MusicService.this;
        }
    }
}

总结

    目前此app的功能还很简单,只是实现了简单的播放功能,后面继续完善,GitHub地址:

Use Git or checkout:https://github.com/PlutoGre/MyApplication4.git
zip下载:https://codeload.github.com/PlutoGre/MyApplication4/zip/master
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值