MediaPlayer 播放音频文件

 

/**
 * 音频播放
 */
public class AudioPlayActivity extends BaseActivity {
    List audioList = new ArrayList<>();
    @BindView(R.id.classifty)
    DeaultHeaderLayout classifty;
    @BindView(R.id.listview)
    ListView listview;
    AudioListAdapter audioListAdapter = null;
    MediaPlayer mediaPlayer = null;
    @BindView(R.id.title1)
    TextView title;
    @BindView(R.id.a)
    Button btn_play;
    @BindView(R.id.b)
    Button btn_pause;
    @BindView(R.id.c)
    Button btn_replay;
    @BindView(R.id.d)
    Button btn_stop;
    String audioPath = null;
    private String path="http://files.17173.com/ddr/music/gfmusic/heart_song.mp3";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio_play);
        ButterKnife.bind(this);
        initAdapter();
//        audioList = getIntent().getStringArrayListExtra("audio");
        for (int i=0;i<5;i++){
            audioList.add(path);
        }
        if (null != audioList && audioList.size() > 0) {
            audioListAdapter.updata(audioList);
        }
    }

    private void initAdapter() {
        audioListAdapter = new AudioListAdapter(this);
        listview.setAdapter(audioListAdapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                stop();
                audioPath = audioList.get(i);
                title.setText("播放地址: " +audioPath);
            }
        });
    }


    @OnClick({R.id.a, R.id.b, R.id.c, R.id.d})
    public void onViewClicked(View view) {
        if (null == audioPath)
            return;
        switch (view.getId()) {
            case R.id.a:
                play();
                break;
            case R.id.b:
                pause();
                break;
            case R.id.c:
                replay();
                break;
            case R.id.d:
                stop();
                break;
        }
    }

    /**
     * 播放音乐
     */

    protected void play() {
        try {
            mediaPlayer = new MediaPlayer();
            // 设置指定的流媒体地址
            mediaPlayer.setDataSource(audioPath);
            // 设置音频流的类型
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            // 通过异步的方式装载媒体资源
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    // 装载完毕 开始播放流媒体
                    mediaPlayer.start();
                    Toast.makeText(AudioPlayActivity.this, "开始播放", Toast.LENGTH_SHORT).show();
                    // 避免重复播放,把播放按钮设置为不可用
                    btn_play.setEnabled(false);
                }
            });
            // 设置循环播放
            // mediaPlayer.setLooping(true);
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    // 在播放完毕被回调
                    btn_play.setEnabled(true);
                }
            });

            mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    // 如果发生错误,重新播放
                    replay();
                    return false;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "播放失败", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 暂停
     */

    protected void pause() {
        if (null == mediaPlayer)
            return;
        if (btn_pause.getText().toString().trim().equals("继续")) {
            btn_pause.setText("暂停");
            mediaPlayer.start();
            Toast.makeText(this, "继续播放", Toast.LENGTH_SHORT).show();
            return;
        }

        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            btn_pause.setText("继续");
            Toast.makeText(this, "暂停播放", Toast.LENGTH_SHORT).show();
        }
    }


    /**
     * 重新播放
     */

    protected void replay() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.seekTo(0);
            Toast.makeText(this, "重新播放", Toast.LENGTH_SHORT).show();
            btn_pause.setText("暂停");
            return;
        }
        play();
    }

    /**
     * 停止播放
     */

    protected void stop() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
            btn_play.setEnabled(true);
            Toast.makeText(this, "停止播放", Toast.LENGTH_SHORT).show();
        }
    }

    @Override

    protected void onDestroy() {
        // 在activity结束的时候回收资源
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        super.onDestroy();
    }
}
/**
     * XML activity_audio_play
     */




    
    
    


        



        

            

 

你可能感兴趣的:(MediaPlayer 播放音频文件)