android:设计一个具有选歌功能的音频播放器

本文介绍了一款简易音乐播放器的设计与实现,使用了XML布局文件定义UI界面,包括一个TextView用于显示提示信息,三个CheckBox供用户选择不同的音乐,以及三个ImageButton分别用于控制播放、暂停和停止。通过MainActivity类中的Java代码,实现了对MediaPlayer的操作,根据用户的选择播放不同的音频资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t1"
        android:textSize="26sp"/>
    <CheckBox
        android:id="@+id/check1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_x="10px"
        android:layout_y="180px"
        android:textSize="20sp"
        android:text="@string/one" />
    <CheckBox
        android:id="@+id/check2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_x="10px"
        android:layout_y="210px"
        android:textSize="20sp"
        android:text="@string/two" />
    <CheckBox
        android:id="@+id/check3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_x="10px"
        android:layout_y="210px"
        android:textSize="20sp"
        android:text="@string/three" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <ImageButton
        android:id="@+id/Stop"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_x="30px"
        android:layout_y="100px"
        android:src="@drawable/music_stop"  />
    <ImageButton
        android:id="@+id/Start"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_x="90px"
        android:layout_y="100px"
        android:src="@drawable/music_play"   />
    <ImageButton
        android:id="@+id/Pause"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_x="150px"
        android:layout_y="100px"
        android:src="@drawable/music_pause"  />
    </LinearLayout>

</LinearLayout>

MainActivity.java

package com.example.yanhsama.example4_4;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity{
    CheckBox ch1,ch2,ch3;

    ImageButton mStopButton, mStartButton, mPauseButton;
    MediaPlayer	mMediaPlayer;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMediaPlayer = new MediaPlayer();
        ch1=(CheckBox)findViewById(R.id.check1);
        ch2=(CheckBox)findViewById(R.id.check2);
        ch3=(CheckBox)findViewById(R.id.check3);
        mStopButton  = (ImageButton) findViewById(R.id.Stop);
        mStartButton = (ImageButton) findViewById(R.id.Start);
        mPauseButton = (ImageButton) findViewById(R.id.Pause);
        mStopButton.setOnClickListener(new mStopClick());
        mStartButton.setOnClickListener(new mStartClick());
        mPauseButton.setOnClickListener(new mPauseClick());
    }
    class mStopClick implements View.OnClickListener
        {
            @Override
            public void onClick(View v)
            {
                /* 是否正在播放 */
                if (mMediaPlayer.isPlaying())
                {
                    //重置MediaPlayer到初始状态
                    mMediaPlayer.reset();
                    mMediaPlayer.release();
                }
            }
        }
        class mStartClick implements View.OnClickListener
        {
        public void onClick(View v)
        {
            if(ch1.isChecked())
            {
                try {
                    mMediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.mtest1);
                    mMediaPlayer.start();
                } catch (Exception e) {Log.i("ch1", "res err ....");	}
            }

       else if(ch2.isChecked())
        {
            try {
                mMediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.mtest2);
                mMediaPlayer.start();
            } catch (Exception e) {Log.i("ch2", "res err ....");	}
        }
            else if(ch3.isChecked())
            {
                try {
                    mMediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.mtest3);
                    mMediaPlayer.start();
                } catch (Exception e) {Log.i("ch3", "res err ....");	}
            }
    }
}
        class mPauseClick implements View.OnClickListener
        {
            @Override
            public void onClick(View v)
            {        if (mMediaPlayer.isPlaying())
            {
                /* 暂停 */
                mMediaPlayer.pause();
            }
            else
            {
                /* 开始播放 */
                mMediaPlayer.start();
            }
            }
        }
    }

Strings.xml

<resources>
    <string name="app_name">简易音乐播放器</string>
    <string name="t1">选择音乐进行播放</string>
    <string name="one">后来</string>
    <string name="two">昨日青空</string>
    <string name="three">只要平凡</string>

</resources>

运行效果显示

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值