MediaPlayer播放音乐(本地/在线)

转载请注明出处:http://blog.csdn.net/mr_leixiansheng/article/details/53642166

步骤:

1、布局文件设置播放、暂停、停止3个按钮

2、主程序中新建MedioPlayer对象,在按键监听中实现音乐的播放、暂停、停止

3、声明权限

注意事项:

1、一定要声明权限

2、在开始播放前必须要先prepare();

 

否则会出现(-38,0)错误

代码如下:

1、布局




    

2、功能实现

package com.example.administrator.player;

import android.media.MediaPlayer;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


import java.io.File;
import java.io.IOException;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button play;
    private Button pause;
    private Button stop;
    private MediaPlayer mediaPlayer = new MediaPlayer();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        play = (Button) findViewById(R.id.play);
        pause = (Button) findViewById(R.id.pause);
        stop = (Button) findViewById(R.id.stop);

        play.setOnClickListener(this);
        pause.setOnClickListener(this);
        stop.setOnClickListener(this);
        //初始化播放器
        initPlayer();

    }

    private void initPlayer() {
        try {
            File file = new File(Environment.getExternalStorageDirectory(), "music.mp3");
            mediaPlayer.setDataSource(file.getPath());
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.play:
                if (!mediaPlayer.isPlaying()) {
                    mediaPlayer.start(); // 开始播放
                }
                break;
            case R.id.pause:
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.pause(); // 暂停播放
                }
                break;
            case R.id.stop:
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.reset(); // 停止播放
                    initPlayer();
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }

}

3、注册声明




    

    
        
            
                

                
            
        
    

Kotlin写法:(播放在线音乐)

package com.leixiansheng.kotlintest

import android.media.MediaPlayer
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(),View.OnClickListener{

    private var player: MediaPlayer = MediaPlayer()
    private val url = "http://abv.cn/music/光辉岁月.mp3"        //网络音乐播放地址


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initPlayer()

        initEvent()
    }

    private fun initPlayer() {
        player.setDataSource(url)
        player.prepare()
    }

    private fun initEvent() {
        play.setOnClickListener(this)
        pause.setOnClickListener(this)
        stop.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v.id) {
            play.id -> if (!player.isPlaying) player.start()

            pause.id -> if (player.isPlaying) player.pause()

            stop.id -> {
                if (player.isPlaying){
                    player.reset()
                    initPlayer()
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        player.stop()
        player.release()
    }
}

 

 

 

 

 

 

 

你可能感兴趣的:(安卓初学,小项目开发)