• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Android用Android写个自己的简易音乐播放器

武飞扬头像
Aaron_Liu0730
帮助2

1.需要提前准备的图标

学新通

学新通

学新通

学新通

学新通

学新通

2.往虚拟机里面导入歌曲

学新通

3.写布局文件

  1.  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.  
    xmlns:tools="http://schemas.android.com/tools"
  3.  
    android:layout_width="match_parent"
  4.  
    android:layout_height="match_parent"
  5.  
    tools:context=".MainActivity"
  6.  
    android:orientation="vertical">
  7.  
     
  8.  
    <TextView
  9.  
    android:id="@ id/showMessage"
  10.  
    android:layout_width="match_parent"
  11.  
    android:layout_height="300dp"
  12.  
    android:layout_alignParentLeft="true"
  13.  
    android:layout_alignParentTop="true"
  14.  
    android:layout_marginTop="69dp"
  15.  
    android:gravity="center"
  16.  
    android:text="播放信息"
  17.  
    android:textColor="#000000"
  18.  
    android:textSize="35sp" />
  19.  
     
  20.  
    <TextView
  21.  
    android:id="@ id/allTime"
  22.  
    android:layout_width="match_parent"
  23.  
    android:layout_height="200dp"
  24.  
    android:layout_above="@ id/stop"
  25.  
    android:layout_alignLeft="@ id/stop"
  26.  
    android:layout_marginBottom="22dp"
  27.  
    android:text="歌词占个地方"
  28.  
    android:textColor="#000000"/>
  29.  
    <LinearLayout
  30.  
    android:layout_width="match_parent"
  31.  
    android:layout_height="76dp"
  32.  
    android:layout_marginBottom="100dp"
  33.  
    android:orientation="horizontal">
  34.  
     
  35.  
    <ImageButton
  36.  
    android:id="@ id/lastsong"
  37.  
    android:layout_width="wrap_content"
  38.  
    android:layout_height="wrap_content"
  39.  
    android:layout_weight="1"
  40.  
    android:scaleType="centerInside"
  41.  
    android:background="#FFFFFF"
  42.  
    android:src="@drawable/last" />
  43.  
     
  44.  
    <ImageButton
  45.  
    android:id="@ id/playsong"
  46.  
    android:layout_width="wrap_content"
  47.  
    android:layout_height="wrap_content"
  48.  
    android:layout_weight="1"
  49.  
    android:scaleType="centerInside"
  50.  
    android:background="#FFFFFF"
  51.  
    android:src="@drawable/play" />
  52.  
     
  53.  
    <ImageButton
  54.  
    android:id="@ id/pause"
  55.  
    android:layout_width="wrap_content"
  56.  
    android:layout_height="wrap_content"
  57.  
    android:layout_weight="1"
  58.  
    android:scaleType="centerInside"
  59.  
    android:background="#FFFFFF"
  60.  
    android:src="@drawable/pause" />
  61.  
     
  62.  
    <ImageButton
  63.  
    android:id="@ id/stop"
  64.  
    android:layout_width="wrap_content"
  65.  
    android:layout_height="wrap_content"
  66.  
    android:layout_weight="1"
  67.  
    android:scaleType="centerInside"
  68.  
    android:background="#FFFFFF"
  69.  
    android:src="@drawable/stop" />
  70.  
    <ImageButton
  71.  
    android:id="@ id/nextsong"
  72.  
    android:layout_width="wrap_content"
  73.  
    android:layout_height="wrap_content"
  74.  
    android:layout_weight="1"
  75.  
    android:scaleType="centerInside"
  76.  
    android:background="#FFFFFF"
  77.  
    android:src="@drawable/next" />
  78.  
     
  79.  
    </LinearLayout>
  80.  
    </LinearLayout>
学新通

 4.主活动

  1.  
    package com.example.musicplayer;
  2.  
     
  3.  
    import androidx.appcompat.app.AppCompatActivity;
  4.  
     
  5.  
    import android.media.MediaPlayer;
  6.  
    import android.os.Bundle;
  7.  
    import android.os.Environment;
  8.  
    import android.view.View;
  9.  
    import android.view.WindowManager;
  10.  
    import android.widget.ImageView;
  11.  
    import android.widget.TextView;
  12.  
    import android.widget.Toast;
  13.  
     
  14.  
    import java.io.File;
  15.  
    import java.util.ArrayList;
  16.  
    import java.util.List;
  17.  
     
  18.  
    public class MainActivity extends AppCompatActivity {
  19.  
    private MediaPlayer player;//播放器
  20.  
    private int stateCode = 2;//0.播放 1.暂停 2.停止
  21.  
    private File file; //文件对象用于音频加载
  22.  
    List<String> songList = new ArrayList<String>();
  23.  
    int index = 0;//播放歌曲序号-1
  24.  
    TextView showMessage;
  25.  
    ImageView btn_play,btn_stop,btn_pause,btn_next,btn_last;
  26.  
     
  27.  
    @Override
  28.  
    protected void onCreate(Bundle savedInstanceState) {
  29.  
    super.onCreate(savedInstanceState);
  30.  
    setContentView(R.layout.activity_main);
  31.  
     
  32.  
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
  33.  
     
  34.  
    btn_play = findViewById(R.id.playsong);
  35.  
    btn_stop = findViewById(R.id.stop);
  36.  
    btn_pause = findViewById(R.id.pause);
  37.  
    btn_next = findViewById(R.id.nextsong);
  38.  
    btn_last = findViewById(R.id.lastsong);
  39.  
    showMessage = findViewById(R.id.showMessage);
  40.  
     
  41.  
    if(!isFileExists())
  42.  
    btn_play.setEnabled(false);
  43.  
    //播放完成时间监听器
  44.  
    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
  45.  
    @Override
  46.  
    public void onCompletion(MediaPlayer mp) {
  47.  
    playSong(2,"下一首");
  48.  
    }
  49.  
    });
  50.  
    //播放按钮
  51.  
    btn_play.setOnClickListener(new View.OnClickListener() {
  52.  
    @Override
  53.  
    public void onClick(View arg0) {
  54.  
    play();
  55.  
    stateCode = 0;
  56.  
    setBtnAndMessage("play","正在播放",false,true,true);
  57.  
    }
  58.  
    });
  59.  
    //暂停
  60.  
    btn_pause.setOnClickListener(new View.OnClickListener() {
  61.  
    @Override
  62.  
    public void onClick(View v) {
  63.  
    player.pause();
  64.  
    stateCode = 1;
  65.  
    setBtnAndMessage("已暂停播放","暂停播放",true,false,true);
  66.  
    }
  67.  
    });
  68.  
    //停止
  69.  
    btn_stop.setOnClickListener(new View.OnClickListener() {
  70.  
    @Override
  71.  
    public void onClick(View arg0) {
  72.  
    player.stop();
  73.  
    stateCode = 2;
  74.  
    setBtnAndMessage("停止播放","将播放",true,false,false);
  75.  
    }
  76.  
    });
  77.  
    //下一曲
  78.  
    btn_next.setOnClickListener(new View.OnClickListener() {
  79.  
    @Override
  80.  
    public void onClick(View arg0) {
  81.  
    playSong(2,"下一首");
  82.  
    }
  83.  
    });
  84.  
    //上一曲
  85.  
    btn_last.setOnClickListener(new View.OnClickListener() {
  86.  
    @Override
  87.  
    public void onClick(View arg0) {
  88.  
    playSong(1,"上一首");
  89.  
    }
  90.  
    });
  91.  
    }
  92.  
     
  93.  
    //消息及提醒
  94.  
    public void setBtnAndMessage(String message,String text,boolean play,boolean pause,boolean stop){
  95.  
    setTextMessage(message, text);
  96.  
    btn_play.setEnabled(play);
  97.  
    btn_pause.setEnabled(pause);
  98.  
    btn_stop.setEnabled(stop);
  99.  
    }
  100.  
     
  101.  
    //消息及提示
  102.  
    public void setTextMessage(String text,String playState){
  103.  
    Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
  104.  
    showMessage.setText(playState "\n" songList.get(index));
  105.  
    }
  106.  
     
  107.  
    //flag 2:last 1:next
  108.  
    public void playSong(int flag,String text){
  109.  
    stateCode = 0;
  110.  
    if(flag == 1){//若为最后一首,播放下一首,索引置0
  111.  
    if( index == (songList.size()-1))
  112.  
    index = 0;
  113.  
    else
  114.  
    index ;
  115.  
    }else if(flag == 2){//若为第一首,播放上一首,索引置为(总长 -1
  116.  
    if(index == 0)
  117.  
    index = songList.size()-1;
  118.  
    else
  119.  
    index--;
  120.  
    }
  121.  
    loadMusic();
  122.  
    readyToPlay();
  123.  
    setBtnAndMessage(text,"正在播放",false,true,true);
  124.  
    }
  125.  
     
  126.  
    //app启动时加载音频
  127.  
    public boolean isFileExists(){
  128.  
    String state = Environment.getExternalStorageState();
  129.  
    if(state.equals(Environment.MEDIA_MOUNTED)){
  130.  
    songList = getFilesAllName();//加载sdcard目录下所有mp3音乐到播放列表
  131.  
    loadMusic();//加载当前index指向音乐
  132.  
    if(file.exists()){
  133.  
    player = new MediaPlayer();
  134.  
    try {
  135.  
    player.setDataSource(file.getAbsolutePath());//获取文件绝对路径加载音乐
  136.  
    player.prepare();
  137.  
    setBtnAndMessage("音乐已加载,请点击播放", "点击播放", true, false, false);
  138.  
    } catch (Exception e) {
  139.  
    e.printStackTrace();
  140.  
    }
  141.  
    return true;
  142.  
    }else
  143.  
    return false;
  144.  
    }else
  145.  
    Toast.makeText(MainActivity.this, "SD卡故障", Toast.LENGTH_SHORT).show();
  146.  
    return false;
  147.  
    }
  148.  
     
  149.  
    //播放
  150.  
    public void play(){///第一次歌曲预加载,只需播放
  151.  
    if(stateCode == 1){
  152.  
    player.start();
  153.  
    setTextMessage("开始播放", "正在播放");
  154.  
    }else if(stateCode == 2){
  155.  
    loadMusic();
  156.  
    readyToPlay();
  157.  
    setTextMessage("开始播放", "正在播放");
  158.  
    }
  159.  
    }
  160.  
     
  161.  
    //预备到播放
  162.  
    public void readyToPlay(){
  163.  
    try {
  164.  
    player.reset();// 从新设置要播放的音乐
  165.  
    player.setDataSource(file.getAbsolutePath());//加载音乐
  166.  
    player.prepare();
  167.  
    player.start();// 播放音乐
  168.  
    } catch (Exception e) {
  169.  
    e.printStackTrace();
  170.  
    }
  171.  
    }
  172.  
     
  173.  
    //清理资源
  174.  
    @Override
  175.  
    protected void onDestroy() {
  176.  
    if(player.isPlaying())
  177.  
    player.stop();
  178.  
    player.release();
  179.  
    super.onDestroy();
  180.  
    }
  181.  
    public void loadMusic(){
  182.  
    file = new File(Environment.getExternalStorageDirectory() File.separator songList.get(index));
  183.  
    }
  184.  
    //获取SD卡所有的.mp3资源
  185.  
    public List<String> getFilesAllName(){
  186.  
    File file = new File(Environment.getExternalStorageDirectory() File.separator);
  187.  
    File[] files = file.listFiles();
  188.  
    List<String> s = new ArrayList<String>();
  189.  
    for(int i =0;i<files.length;i ){
  190.  
    if(files[i].toString().endsWith(".mp3"))
  191.  
    s.add(files[i].getName());
  192.  
    }
  193.  
    return s;
  194.  
    }
  195.  
    }
学新通

 5.清单文件给权限

  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    package="com.example.musicplayer">
  4.  
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  5.  
     
  6.  
    <application
  7.  
    android:allowBackup="true"
  8.  
    android:icon="@mipmap/ic_launcher"
  9.  
    android:label="@string/app_name"
  10.  
    android:roundIcon="@mipmap/ic_launcher_round"
  11.  
    android:supportsRtl="true"
  12.  
    android:theme="@style/Theme.Map">
  13.  
    <activity
  14.  
    android:name=".MainActivity"
  15.  
    android:exported="true">
  16.  
    <intent-filter>
  17.  
    <action android:name="android.intent.action.MAIN" />
  18.  
     
  19.  
    <category android:name="android.intent.category.LAUNCHER" />
  20.  
    </intent-filter>
  21.  
    </activity>
  22.  
    </application>
  23.  
     
  24.  
    </manifest>
学新通

6.运行应用,在虚拟机设备的应用里面给应用权限

学新通

找到你写的应用

学新通

给权限

 学新通

 学新通

7.启动应用,播放吧

 学新通

环境:Android-studio

Android6 

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhibcebh
系列文章
更多 icon
同类精品
更多 icon
继续加载