欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 播放音乐

程序员文章站 2024-01-08 17:02:28
Android 播放音乐xml代码如下:

Android 播放音乐

xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="horizontal"
    tools:context=".MainActivity">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/play"
        android:text="播放"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/stop"
        android:text="暂停"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/replay"
        android:text="重新开始"/>

</LinearLayout>

MainActivity代码如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button play;
    private Button stop;
    private Button replay;
    private MediaPlayer player;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        play = (Button) findViewById(R.id.play);
        stop = (Button) findViewById(R.id.stop);
        replay = (Button) findViewById(R.id.replay);
        play.setOnClickListener(this);
        stop.setOnClickListener(this);
        replay.setOnClickListener(this);
        initVideo();
    }

    private void initVideo(){
        player = new MediaPlayer();
        player = MediaPlayer.create(this, R.raw.music);
        try {
            player.prepare();// 同步
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.play:
                if(!player.isPlaying()){
                    player.start();
                }
                break;
            case R.id.stop:
                if(player.isPlaying()){
                    player.pause();

                }
                break;
            case R.id.replay:
                if (!player.isPlaying()){
                    initVideo();
                    player.start();
                    //player.reset();
                }else {
                    player.stop();
                    initVideo();
                    player.start();
                }
                break;
                default:
                    break;
        }
    }

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

本文地址:https://blog.csdn.net/weixin_44616286/article/details/107451668