Android Studio 1-18 使用MediaPlayer完成音乐播放器

这篇博客详细介绍了如何利用Android Studio进行音乐播放器的开发,包括布局设计如activity、listview和通知布局,以及java代码实现,涵盖activity、adapter、Util、Service、provider和entity等关键部分。
摘要由CSDN通过智能技术生成

Android Studio 1-18 使用MediaPlayer完成音乐播放器

布局

activty布局

<?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="vertical"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/lv_main"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9">

    </ListView>

    <SeekBar
        android:id="@+id/seek_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/prev_song"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClick"
            android:text="上一首" />

        <Button
            android:id="@+id/pause_And_Restart"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClick"
            android:text="暂停/继续" />

        <Button
            android:id="@+id/next_song"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClick"
            android:text="下一首" />

        <Button
            android:id="@+id/song_mode"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClick"
            android:text="模式" />

    </LinearLayout>


</LinearLayout>

listview 布局

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

    <!--<ImageView-->
        <!--android:id="@+id/iv_cover"-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:src="@mipmap/ic_launcher" />-->

    <TextView
        android:id="@+id/tv_song"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="歌曲名"
        android:textSize="20sp" />

    <RelativeLayout
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <TextView
            android:id="@+id/tv_singer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_gravity="bottom"
            android:text="歌手"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/tv_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="时长"
            android:textSize="15sp" />

    </RelativeLayout>

</LinearLayout>

通知 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/info_prev_song"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="上一首" />

        <Button
            android:id="@+id/info_pause_And_Restart"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="暂停/继续" />

        <Button
            android:id="@+id/info_next_song"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="下一首" />

    </LinearLayout>

</LinearLayout>

java代码

activity

package com.example.day19ex;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.Toast;

import com.example.day19ex.Util.MusicUtils;
import com.example.day19ex.adapter.AdapterList;
import com.example.day19ex.entity.Song;
import com.example.day19ex.provider.MyReceiver;
import com.example.day19ex.service.MusicService;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
   

    private ListView lvMain;
    private static SeekBar seek_main;
    private Button prevSong, pauseAndRestart, nextSong, songMode;

    private static MusicService.MusicBinder musicBinder;
    private ServiceConnection connection;
    private AdapterList adapterList;
    private List<Song> songList;
    private MyReceiver myReceiver;
    private Timer timer = new Timer();

    //默认为顺序播放模式
    private int playMode = Song.MODE_ORDE;

    public static Handler handler = new Handler(
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值