Search Unity

Playing mp3 file on Android

Discussion in 'Audio & Video' started by CrystalMD, Mar 6, 2017.

  1. CrystalMD

    CrystalMD

    Joined:
    Apr 24, 2016
    Posts:
    4
    Hi, i need your help so much, so i want to play a mp3 file into my android app, ok, i wrote the code using NAudio, and there's the problem, it works only on windows, on android i hear nothing, i mean the list of all music files i get but to play them i can't, pleaseee help mee. Oh, forgot to say that first it converts mp3 to wav(on line: audio.clip = NAudioPlayer.FromMp3Data (www.bytes);), i just tried someone's code, but it seems non-workable(work just for PC) Full code is:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.IO;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. public class MusicSelect : MonoBehaviour {
    6.      private string _curPath;
    7.      public List<string> _curMusicNames, _curMusicPaths;
    8.      public void GetCurFiles()
    9.      {
    10.          _curMusicNames.Clear ();
    11.          _curMusicPaths.Clear ();
    12.          _curPath = Directory.GetCurrentDirectory ();
    13.          Debug.Log ("Path:"+_curPath);
    14.          if (Application.platform != RuntimePlatform.Android)
    15.              return;
    16.          #if UNITY_ANDROID
    17.          using (AndroidJavaClass jcEnvironment = new AndroidJavaClass ("android.os.Environment"))
    18.          using (AndroidJavaObject joExDir = jcEnvironment.CallStatic<AndroidJavaObject> ("getExternalStorageDirectory")) {
    19.              _curPath = joExDir.Call<string> ("toString") + "/Music";
    20.          }
    21.          #endif
    22.          DirectoryInfo dir = new DirectoryInfo(_curPath);
    23.          FileInfo[] info = dir.GetFiles("*.mp3");
    24.          foreach (FileInfo f in info)
    25.          {
    26.              _curMusicNames.Add( f.Name );
    27.              _curMusicPaths.Add( f.FullName );
    28.          }
    29.      }
    30.      void OnGUI() {
    31.          if(_curMusicPaths.Count>0)
    32.          {
    33.              for(int i=0; i<_curMusicPaths.Count; i++)
    34.              {
    35.                  if (GUI.Button(new Rect(0,i*170, Screen.width, 150),_curMusicPaths[i])){
    36.                      Debug.Log ("Playing "+_curMusicNames[i]);
    37.                      StartCoroutine (PlayThis("file://"+_curMusicPaths[i]));
    38.                  }
    39.              }
    40.          }
    41.      }
    42.      IEnumerator PlayThis(string url)
    43.      {
    44.          WWW www = new WWW(url);
    45.          yield return www;
    46.          AudioSource audio = GetComponent<AudioSource>();
    47.          audio.clip = NAudioPlayer.FromMp3Data (www.bytes);
    48.          audio.Play();
    49.      }
    50. }
     
    AbgaryanFX likes this.
  2. unity_0vE5rwzuwauwrQ

    unity_0vE5rwzuwauwrQ

    Joined:
    Sep 23, 2018
    Posts:
    1
    you can change the extension of the file's name from .Mp3 to .Wav.