如何在安卓设备上加载位于SD卡上的mp3文件?



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

public string path = C:UsersPCDesktopmyMusic.mp3
IEnumerator Start()
{
using (WWW www = new WWW(path))
{
yield return www;
source.clip = www.GetAudioClip();
source.Play();
}
}

但是,它不适用于Android。mp3文件位于我的SD卡的MP3文件夹中。我尝试了这些路径: "/storage/emulated/MP3/myMusic.mp3" ;"/storage/sdcard/MP3/myMusic.mp3" ;"/storage/emulated/sdcard/MP3/myMusic.mp3",但它不起作用。

所以,我不知道我是否没有使用正确的路径或WWW是否。GetAudioClip(( 方法在 Android 上不起作用。

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

问题是路径。必须使用 C#FileInfoDirectoryInfoAPI 返回相应的路径,然后将该路径传递给WWWAPI。将/mnt/sdcard传递给DirectoryInfoAPI,它将为您提供正确的使用路径。用于访问 SD 卡上数据的路径与WWWAPI 一起使用的路径是"file:///" + FileInfo.FullName.

打击就是一个例子。它假设音乐.mp3文件放置在SD卡上名为"music">的文件夹中。如果它位于名为"MP3">的文件夹中,请将"/mnt/sdcard/music"更改为"/mnt/sdcard/MP3"确保转到Android的构建设置,将写入权限内部更改为外部(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();
}
}

它是一个协程函数,因此您可以将其称为StartCoroutine(LoadAndPlaySound());

最新更新