安卓简易音乐播放器

大概的布局是这样的,进度条是SeekBar`


下面是一个activity用来控制上面那个布局文件

package com.ryg.ofo2;

import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/21.
 */
public class PlayActivity extends AppCompatActivity {
    static MediaPlayer mPlayer;
    private Boolean b;
    public static List<String> list;
    public static List<String> title;
    public static List<String> singer;
    public static int index;
    Animation loadAnimation;
    private ImageView touxiang;
    private ImageView song_menu;
    public static TextView song_title;
    public static ImageView playing;
    private List<String> album;
    private ImageView repeat;
    private int code;
    public static SeekBar mSeekBar;
    final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 1) {
                int cur = mPlayer.getCurrentPosition();
                mSeekBar.setProgress(cur);

                Message msg01 = handler.obtainMessage();
                msg01.what = 1;
                handler.sendMessageDelayed(msg01, 1000);

            }
            return false;
        }
    });

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        touxiang = (ImageView) findViewById(R.id.touxiang);
        song_menu = (ImageView) findViewById(R.id.song_menu);
        song_title = (TextView) findViewById(R.id.song_title);
        playing = (ImageView) findViewById(R.id.btn_01);
        repeat = (ImageView) findViewById(R.id.repeat);
        mSeekBar = (SeekBar) findViewById(R.id.seekbar01);
        //获取首选项中index的值
        SharedPreferences s = getSharedPreferences("music", MODE_PRIVATE);
        index = s.getInt("index", 0);
        Toast.makeText(PlayActivity.this, "index:" + index, Toast.LENGTH_SHORT).show();

        //list集合存放音乐文件路径
        list = new ArrayList<>();
        title = new ArrayList<>();
        singer = new ArrayList<>();
        album = new ArrayList<>();
        getmusic();
        gettitle();
        getsinger();
        getalbum();
        getpic();//获取专辑图片

        //设置歌曲名称和歌手
        song_title.setText(title.get(index) + " : " + singer.get(index));
        mPlayer = new MediaPlayer();
        b = true;
        try {
            mPlayer.setDataSource(list.get(index));
            mPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //手动调整进度条改变播放位置
        mSeekBar.setMax(mPlayer.getDuration());
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    mPlayer.seekTo(progress);
                }

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        //让进度条跟随歌曲播放进度滑动


        //播放
        playing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start();
            }
        });
        //下一首
        findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                next();
            }
        });
        //上一首
        findViewById(R.id.last).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                last();
            }
        });

        //循环模式
        code = 0;//0表示顺序播放
        repeat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.setLooping(true);
                code = 1;//1表示单曲循
                Toast.makeText(PlayActivity.this, "单曲循环", Toast.LENGTH_SHORT).show();
            }
        });
        //歌曲列表跳转
        song_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent in = new Intent(PlayActivity.this, SonglistActivity.class);
                startActivity(in);
            }
        });


    }

    private void last() {
        if (index == 0) {
            index = list.size();
        }
        index--;
        mPlayer.reset();
        try {
            mPlayer.setDataSource(list.get(index));
            mPlayer.prepare();
            mSeekBar.setMax(mPlayer.getDuration());
        } catch (IOException e) {
            e.printStackTrace();
        }
        mPlayer.start();
        playing.setImageResource(R.mipmap.isplaying);
        song_title.setText(title.get(index) + " : " + singer.get(index));
        b = false;
    }

    private void next() {
        index++;
        if (index > list.size() - 1) {
            index = 0;
        }

        mPlayer.reset();
        try {
            mPlayer.setDataSource(list.get(index));
            mPlayer.prepare();
            mSeekBar.setMax(mPlayer.getDuration());
        } catch (IOException e) {
            e.printStackTrace();
        }
        mPlayer.start();
        playing.setImageResource(R.mipmap.isplaying);
        song_title.setText(title.get(index) + "  : " + singer.get(index));
        b = false;

    }

    private void getpic() {
        //??????????
    }

    //获取专辑名称?
    private void getalbum() {
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) {
            int pathIndex = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM);
            String path = cursor.getString(pathIndex);
            album.add(path);

        }
    }

    //获取歌手姓名
    private void getsinger() {
        ContentResolver contenresolver = getContentResolver();
        Cursor cursorsinger = contenresolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        while (cursorsinger.moveToNext()) {
            int pathIndex = cursorsinger.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST);
            String path = cursorsinger.getString(pathIndex);
            singer.add(path);
        }
    }

    //用url的方式获取手机上的音乐
    public void getmusic() {
        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) {
            int pathIndex = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);
            String path = cursor.getString(pathIndex);
            list.add(path);
        }
        cursor.close();
    }

    //获取标题
    public void gettitle() {
        ContentResolver contentResolver = getContentResolver();
        Cursor cursortitle = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        while (cursortitle.moveToNext()) {
            int pathIndex = cursortitle.getColumnIndex(MediaStore.Audio.AudioColumns.TITLE);
            String path = cursortitle.getString(pathIndex);
            title.add(path);
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPlayer.pause();
        //用首选项储存index的值
        SharedPreferences mshare = getSharedPreferences("music", MODE_PRIVATE);
        SharedPreferences.Editor edit = mshare.edit();
        edit.putInt("index", index);
        edit.commit();
    }

    public void start() {
        if (b && (!mPlayer.isPlaying())) {
            mPlayer.start();
            //设置进度条跟随音乐播放而变化
            Message message = handler.obtainMessage();
            message.what = 1;
            handler.sendMessageDelayed(message, 1000);
            mSeekBar.setMax(mPlayer.getDuration());


            int a = (mPlayer.getDuration() / 1000) / 60;//分
            int seconds = (mPlayer.getDuration() / 1000) % 60;//秒
            Toast.makeText(PlayActivity.this, "歌曲时长:" + a + "分" + seconds + "秒", Toast.LENGTH_SHORT).show();

            playing.setImageResource(R.mipmap.isplaying);
//                    song_title.setText(title.get(index)+" : "+singer.get(index));
            loadAnimation = AnimationUtils.loadAnimation(PlayActivity.this, R.anim.rotate);
            touxiang.startAnimation(loadAnimation);
            Toast.makeText(PlayActivity.this, "开始", Toast.LENGTH_SHORT).show();
            b = false;

        } else {
            mPlayer.pause();
            touxiang.clearAnimation();
            playing.setImageResource(R.mipmap.start);
            Toast.makeText(PlayActivity.this, "暂停", Toast.LENGTH_SHORT).show();
            b = true;
        }


    }
}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值