Android studio 讀取sd卡mp3 播放音樂

第一步佈局文件
  <ListView
        android:id="@+id/lv"
        android:layout_width="409dp"
        android:layout_height="729dp"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="1dp" />

第二步權限
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
第三步 手機設置權限
第四步  類
package com.example.myapplication;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Environment;
import android.widget.Toast;


import java.io.IOException;

public class MP3Player {

    private MediaPlayer mp;
    private String path;
    private int length;



    public MP3Player(Context ctx) {
        mp = new MediaPlayer();
        // 獲取內部存儲器絕對路徑/sdcard/Download/ANewDay.mp3
        path = Environment.getExternalStorageDirectory().getAbsolutePath();
        path="/sdcard/Music/";
        Toast.makeText(ctx, path, Toast.LENGTH_LONG).show();
    }

    public int getPosition() {
        // 獲取當前位置
        return mp.getCurrentPosition();
    }

    public int getLength() {
        return length;
    }

    public void init(String fileName) {
        path = path + "/" + fileName;
        try {
            // 存儲在SD卡或其他文件路徑下的媒體文件
            mp.setDataSource(path);
            // 音樂文件準備
            mp.prepare();
            // 音樂文件長度
            length = mp.getDuration();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void play() {
        if (mp.isPlaying()) {
            mp.stop();
        }
        else {
            mp.start();
        }

    }

    public void pause() {
        mp.pause();
    }

    public void stop() {
        mp.stop();
    }

    public void destroy() {
        mp.release();
    }

}


第五步主代碼
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    private MP3Player mp3Player;

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

        System.out.println("11111111");
        System.out.println(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC));

        ///mnt/sdcard/Music
        File file=new File("/sdcard/Music");
        final File[] files=file.listFiles();
        if (files == null){
            Log.e("error","空目錄");
        }
        List<String> s = new ArrayList<>();
        for(int i =0;i<files.length;i++){
            //s.add(files[i].getAbsolutePath());
            s.add(files[i].getName());
            System.out.println(files[i].getAbsolutePath());
        }

        ArrayAdapter<String> arrayAdapter= new ArrayAdapter<String> (
                MainActivity.this, android.R.layout.simple_list_item_1,s);
        ListView listView = (ListView) findViewById(R.id.lv);
        listView.setAdapter(arrayAdapter);



        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                System.out.println(id);
                System.out.println(files[(int)id].getAbsolutePath());

                mp3Player = new MP3Player(MainActivity.this);
                mp3Player.init(files[(int)id].getName());
                mp3Player.play();
            }
        });


    }
}


在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章