C# 如何在android设备上加载SD卡上的mp3文件?

C# 如何在android设备上加载SD卡上的mp3文件?,c#,unity3d,path,mp3,android-sdcard,C#,Unity3d,Path,Mp3,Android Sdcard,使用此脚本,我可以从PC加载mp3文件: public string path = C:\Users\PC\Desktop\myMusic.mp3 IEnumerator Start() { using (WWW www = new WWW(path)) { yield return www; source.clip = www.GetAudioClip(); source.Play(); } } 然而,它在Android上不起作用。mp3文件位于我的sd卡的mp3文件夹中。我尝试了以下

使用此脚本,我可以从PC加载mp3文件:

public string path = C:\Users\PC\Desktop\myMusic.mp3

IEnumerator Start()
{

using (WWW www = new WWW(path))
{
yield return www;

source.clip = www.GetAudioClip();
source.Play();
}
}
然而,它在Android上不起作用。mp3文件位于我的sd卡的mp3文件夹中。我尝试了以下路径: /存储/模拟/MP3/myMusic.MP3/存储/SD卡/MP3/myMusic.MP3/存储/模拟/SD卡/MP3/myMusic.MP3,但它不起作用

因此,我不知道我是否使用了正确的路径,或者WWW.GetAudioClip方法是否在Android上不起作用


对不起,我的英语不好,希望你能理解。我真的需要你的帮助。

问题在于路径。您必须使用C FileInfo和DirectoryInfo API返回适当的路径,然后将该路径传递给WWW API。将/mnt/sdcard传递给DirectoryInfo API,它将为您提供正确的使用路径。WWW API用于访问SD卡上的数据的路径是file:///+FileInfo.FullName

Blow就是一个例子。它假定music.mp3文件放在SD卡上名为music的文件夹中。如果它位于名为MP3的文件夹中,则将/mnt/sdcard/music更改为/mnt/sdcard/MP3确保转到Android的构建设置,将写入权限从内部SD卡更改为外部SD卡


这是一个协同程序功能,因此您可以将其称为StartRoutinLoadandPlaySound

非常感谢您快速而详细的回答!但是,它仍然不起作用。。我放置了这个路径/mnt/sdcard/MP3,但我有一个错误:DirectoryNotFoundException:找不到目录'C:\mnt\sdcard\MP3'。Unity在路径的边缘添加一个C:是正常的吗?你应该在Android上运行,而不是在Windows的编辑器中运行。这就是为什么你会看到我尝试过的错误,你是对的。在我的Android设备上,路径是正确的,但什么都没有发生。是mnt/sdcard/。。。唯一的路径还是根据手机型号存在不同的路径?
public AudioSource aSource;

public string path = @"/mnt/sdcard/music";
private FileInfo[] info;
private DirectoryInfo dir;

IEnumerator LoadAndPlaySound()
{
    //Get the proper path with DirectoryInfo
    dir = new DirectoryInfo(path);
    //Get all .mp3 files in the folder
    info = dir.GetFiles("*.mp3");

    //Use the first audio index found in the directory
    string audioPath = "file:///" + info[0].FullName;

    using (WWW www = new WWW(audioPath))
    {
        yield return www;

        //Set the AudioClip to the loaded one
        aSource.clip = www.GetAudioClip(false, false);
        //Play Audio
        aSource.Play();
    }
}