android 简单音乐播放功能实现

1.Musicplayer Activity 

//歌曲名称本应该是通过Intent传入,不过此处没有前置代码,故直接赋值即可。



import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private AudioManager audioManager = null;
    private MediaPlayer player = null;
    int maxVolume, currentVolume;
    private String songname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);

        ImageButton btn1 = findViewById(R.id.play);
        ImageButton btn2 = findViewById(R.id.pause);
        ImageButton btn3 = findViewById(R.id.stop);
        ImageButton btn4 = findViewById(R.id.volume_plus);
        ImageButton btn5 = findViewById(R.id.volume_decrease);

        TextView te_songname = findViewById(R.id.te_songname);
        songname = "song001";
        te_songname.setText(songname);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (player == null || !player.isPlaying()) {
                    player = MediaPlayer.create(MainActivity.this, R.raw.song001);
                    player.start();
                } else {
                    player.stop();
                    player.release();
                    player = null;
                }
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (player.isPlaying()) {
                    player.pause();
                }

            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (player.isPlaying()) {
                    player.reset();//停止播放
                    //initMediaPlayer();
                }
            }
        });

        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
                currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                Toast.makeText(MainActivity.this, "音量增加,最大音量是:" + maxVolume + ",当前音量" + currentVolume,
                        Toast.LENGTH_SHORT).show();
            }
        });

        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
                currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                Toast.makeText(MainActivity.this, "音量减小,最大音量是:" + maxVolume + ",当前音量" + currentVolume,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}

 2.activity_musicplayer 布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="正在播放:"
            android:textSize="25dp" />

        <TextView
            android:id="@+id/te_songname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="25dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">


        <ImageButton
            android:id="@+id/play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:src="@drawable/bofang" />

        <ImageButton
            android:id="@+id/pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:background="@null"
            android:src="@drawable/zanting" />

        <ImageButton
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:background="@null"
            android:src="@drawable/tingzhi" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="15dp"
        android:gravity="right"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/volume_plus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:src="@drawable/add" />

        <ImageButton
            android:id="@+id/volume_decrease"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@null"
            android:src="@drawable/less" />

    </LinearLayout>
</RelativeLayout>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值