使用此脚本,我可以从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文件夹中。我尝试了以下路径: “ /storage/emulated/MP3/myMusic.mp3”; “ /storage/sdcard/MP3/myMusic.mp3”; “ /storage/emulated/sdcard/MP3/myMusic.mp3”,但没有用。
因此,我不知道我是否使用了不正确的路径,或者WWW.GetAudioClip()方法在Android上不起作用。
对不起,我的英语不好,希望您能理解。我真的需要您的帮助。
答案 0 :(得分:0)
问题在于路径。您必须使用C#FileInfo
和DirectoryInfo
API返回相应的路径,然后将该路径传递到WWW
API。将/mnt/sdcard
传递到DirectoryInfo
API,它将为您提供正确的使用路径。与WWW
API配合使用以访问SD卡上的数据的路径为"file:///" + FileInfo.FullName.
。
打击是一个例子。假定将音乐 .mp3 文件放在SD卡上名为“音乐” 的文件夹中。如果它位于名为“ MP3” 的文件夹中,则将"/mnt/sdcard/music"
更改为"/mnt/sdcard/MP3"
,确保进入Android的 Build Settings ,更改内部到外部(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());