I’m wondering how can I play an mp3 audio file from URL in Unity. So I tried to search around and I found the solution.
Watch this video below:
This is the script used in this project:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class testscript : MonoBehaviour
{
AudioSource audioSource;
AudioClip myClip;
void Start()
{
audioSource = GetComponent<AudioSource>();
StartCoroutine(GetAudioClip());
Debug.Log("Starting to download the audio...");
}
IEnumerator GetAudioClip()
{
using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("https://ciihuy.com/downloads/music.mp3", AudioType.MPEG))
{
yield return www.SendWebRequest();
if (www.isNetworkError)
{
Debug.Log(www.error);
}
else
{
myClip = DownloadHandlerAudioClip.GetContent(www);
audioSource.clip = myClip;
audioSource.Play();
Debug.Log("Audio is playing.");
}
}
}
public void pauseAudio(){
audioSource.Pause();
}
public void playAudio(){
audioSource.Play();
}
public void stopAudio(){
audioSource.Stop();
}
}
This Unity package can play an mp3 audio file stored on my web server. Download it here: https://drive.google.com/open?id=1u97pcEMQFPdqgVCx0nSm-J67367oOKHr
Please note that this thing won’t work on desktop platform, so please use it for Android or iOS platform, I’ve tested it.
loading...
Can we load mp3 file located in sd card and play that mp3 file ?
That one is bit more complicated, because unity will need device permission to access files on device storage.
audio clipnya di isi file gk om?
enggak