Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Dec 11, 2015 at 07:21 PM by KnightRiderGuy for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by KnightRiderGuy · Dec 10, 2015 at 05:14 PM · c#audiomusicaudioplayaudio.play

Why Music Player Pause and Stop Buttons Do Not Work As They Should

I have this music player script that when it plays the next song from the music folder it makes my Pause and stop buttons not work quite right. wondering if anyone can take a look and see why?

UPDATE: I updated the script The main problem is solved now. Now the Stop and Pause buttons are functioning the way they should. What a pain in the you know what, all day to do something that should have taken 5 minutes. Gee thankx :/

using UnityEngine; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEngine.UI;

public class AltMusicPlayer : MonoBehaviour {

 public Text MusicListText;// Reference to my UI Display Text for Songs
 private string display = "";//NEW Delete if does not work!!
 public enum SeekDirection { Forward, Backward }
 
 public AudioSource source;

 bool playing = true;

 public List<AudioClip> clips = new List<AudioClip>();
 
 [SerializeField] [HideInInspector] private int currentIndex = 0;
 
 private FileInfo[] soundFiles;
 private List<string> validExtensions = new List<string> { ".ogg", ".wav" }; // Don't forget the "." i.e. "ogg" won't work - cause Path.GetExtension(filePath) will return .ext, not just ext.
 private string absolutePath = "./Audio/Music"; // relative path to where the app is running - change this to "./music" in your case

 void Start()
 {
     //being able to test in unity
     if (Application.isEditor)
         absolutePath = "Assets/Audio/Music";
     
     if (source == null)
         source = gameObject.AddComponent<AudioSource> ();
     ReloadSounds ();
 }
 

 void Update(){
     if(!source.isPlaying && playing)
     {
         Seek(SeekDirection.Forward);
         PlayCurrent();
     }
 }
 
 
 
 //My New UI Buttons Added
 public void PlayPrevious (){
     Seek(SeekDirection.Backward);
     PlayCurrent();
 }

 public void PlayCurrentClip () {
     PlayCurrent();
     playing = true;
 }

 public void PlayNext () {
     Seek(SeekDirection.Forward);
     PlayCurrent();
 }

 public void ReloadAudio () {
     ReloadSounds();
 }

 public void StopAudio () {
         source.Stop ();
         playing = false;
     
 }

 public void PauseAudio () {
     source.Pause ();
     playing = false;

 }

 void Shuffle()
 {
     //Shuffle or Randomize Songs in Array List
     //ShuffleMusic();
 }


 //End My New UI Buttons Added
 
 void Seek(SeekDirection d)
 {
     if (d == SeekDirection.Forward)
         currentIndex = (currentIndex + 1) % clips.Count;
     else {
         currentIndex--;
         if (currentIndex < 0) currentIndex = clips.Count - 1;
     }
 }
 
 void PlayCurrent()
 {
     source.clip = clips[currentIndex];
     source.Play();
     Debug.Log (source.clip);
     MusicListText.text = clips[currentIndex].name;


 }
 

 void ReloadSounds()
 {
     clips.Clear();
     
     // get all valid files
     var info = new DirectoryInfo(absolutePath);
     soundFiles = info.GetFiles()
         .Where(f => IsValidFileType(f.Name))
             .ToArray();
     
     // and load them
     foreach (var s in soundFiles)
         StartCoroutine(LoadFile(s.FullName));
 }
 
 bool IsValidFileType(string fileName)
 {
     return validExtensions.Contains(Path.GetExtension(fileName));
     // Alternatively, you could go fileName.SubString(fileName.LastIndexOf('.') + 1); that way you don't need the '.' when you add your extensions
 }
 
 IEnumerator LoadFile(string path)
 {
     WWW www = new WWW("file://" + path);
     print("loading " + path);
     
     AudioClip clip = www.GetAudioClip(false);
     while(!clip.isReadyToPlay)
         yield return www;
     
     print("done loading");
     clip.name = Path.GetFileName(path);
     clips.Add(clip);


     //Count Array List Length
     //source.clip = clips[currentIndex];
     Debug.Log (clip);
     //int List = clips.Count;
     //Debug.Log (List);
 }

 //NEW
 void AddText()
 {
     display = currentIndex + " : " + clips[currentIndex].name;
     MusicListText.text = display;
 }
 //END NEW

 //Quit Button
 public void  GoQuit(){
     StartCoroutine(LoadQuit("MainScreen"));
 }
 
 IEnumerator LoadQuit(string levelMain){
     yield return new WaitForSeconds(0.5f); // wait time
     Application.LoadLevel(levelMain);
     
 }


}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by callen · Dec 10, 2015 at 07:21 PM

I can't tell where these functions are all being called (I'm guessing a UI front-end hooks into it), but you should be able to have the player continue to the next song by adding this method to the script:

 void Update(){
    if(!source.isPlaying)
    {
        Seek(SeekDirection.Forward);
        PlayCurrent();
    }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KnightRiderGuy · Dec 10, 2015 at 07:44 PM 0
Share

@callen, Oh for crying out loud, lol that was all it needed, jeepers I had posted this blasted script a number of times and no one fielded this one... you got it, it works.

Only wird thing with it now is that the stop button does not completely stop the music? just kind of stops it momentarily and then it starts again?

avatar image callen KnightRiderGuy · Dec 10, 2015 at 08:27 PM 0
Share

Oh yeah, I didn't consider that! I dont actually see the code in here that is stopping the music, so I'm guessing that could have been done with a UI event directly calling the source.Stop() method.

There are a few ways to fix it, one would be tracking an internal 'playing' state yourself. To do this you'd need a bool playing variable, and to add your own void Stop$$anonymous$$usic() method (which you have to hook up to the stop button in ui!). In the Stop$$anonymous$$usic method, set playing=false and call source.Stop(); in the PlayCurrent method, set playing=true; finally in the Update() loop change the condition to "if(!source.isPlaying && playing)"

Another (possibly easier) way to fix it, without writing any more code, is to make these changes: Stop button - ins$$anonymous$$d of calling AudioSource.Stop(), call AudioSource.enabled=false Play button - keep calling your PlayCurrent method, but ALSO add a callback to set AudioSource.enabled=true

Hope that makes sense!

avatar image KnightRiderGuy callen · Dec 10, 2015 at 08:55 PM 0
Share

@callen, $$anonymous$$akes a little sense. I tried using the UI buttons to call Audio stop but I think with the update function it just makes it interrupt momentarily. I also tried adding the bool for source = false; but that just gave me weird results?

Show more comments
avatar image
0

Answer by KnightRiderGuy · Dec 10, 2015 at 09:29 PM

Just for the heck of it I'll add the slightly modified script in @callen so you can see the changes. It's working better but still a little goofy. Oh just in case I didn't mention it the button are on a UI canvas. ;)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using UnityEngine.UI;
 
 public class AltMusicPlayer : MonoBehaviour
 {
 
     public Text MusicListText;// Reference to my UI Display Text for Songs
     private string display = "";//NEW Delete if does not work!!
     public enum SeekDirection { Forward, Backward }
     
     public AudioSource source;
 
     bool playing = true;
 
     public List<AudioClip> clips = new List<AudioClip>();
     
     [SerializeField] [HideInInspector] private int currentIndex = 0;
     
     private FileInfo[] soundFiles;
     private List<string> validExtensions = new List<string> { ".ogg", ".wav" }; // Don't forget the "." i.e. "ogg" won't work - cause Path.GetExtension(filePath) will return .ext, not just ext.
     private string absolutePath = "./Audio/Music"; // relative path to where the app is running - change this to "./music" in your case
 
     void Start()
     {
         //being able to test in unity
         if (Application.isEditor)
             absolutePath = "Assets/Audio/Music";
         
         if (source == null)
             source = gameObject.AddComponent<AudioSource> ();
         ReloadSounds ();
     }
     
 
     void Update(){
         if(!source.isPlaying && playing)
         {
             Seek(SeekDirection.Forward);
             PlayCurrent();
         }
     }
     
     
     
     //My New UI Buttons Added
     public void PlayPrevious (){
         Seek(SeekDirection.Backward);
         PlayCurrent();
     }
 
     public void PlayCurrentClip () {
         PlayCurrent();
         playing = true;
     }
 
     public void PlayNext () {
         Seek(SeekDirection.Forward);
         PlayCurrent();
     }
 
     public void ReloadAudio () {
         ReloadSounds();
     }
 
     public void StopAudio () {
             
             playing = false;
         
     }
 
     void Shuffle()
     {
         //Shuffle or Randomize Songs in Array List
         //ShuffleMusic();
     }
 
 
     //End My New UI Buttons Added
     
     void Seek(SeekDirection d)
     {
         if (d == SeekDirection.Forward)
             currentIndex = (currentIndex + 1) % clips.Count;
         else {
             currentIndex--;
             if (currentIndex < 0) currentIndex = clips.Count - 1;
         }
     }
     
     void PlayCurrent()
     {
         source.clip = clips[currentIndex];
         source.Play();
         Debug.Log (source.clip);
         MusicListText.text = clips[currentIndex].name;
 
 
     }
     
 
     void ReloadSounds()
     {
         clips.Clear();
         
         // get all valid files
         var info = new DirectoryInfo(absolutePath);
         soundFiles = info.GetFiles()
             .Where(f => IsValidFileType(f.Name))
                 .ToArray();
         
         // and load them
         foreach (var s in soundFiles)
             StartCoroutine(LoadFile(s.FullName));
     }
     
     bool IsValidFileType(string fileName)
     {
         return validExtensions.Contains(Path.GetExtension(fileName));
         // Alternatively, you could go fileName.SubString(fileName.LastIndexOf('.') + 1); that way you don't need the '.' when you add your extensions
     }
     
     IEnumerator LoadFile(string path)
     {
         WWW www = new WWW("file://" + path);
         print("loading " + path);
         
         AudioClip clip = www.GetAudioClip(false);
         while(!clip.isReadyToPlay)
             yield return www;
         
         print("done loading");
         clip.name = Path.GetFileName(path);
         clips.Add(clip);
 
 
         //Count Array List Length
         //source.clip = clips[currentIndex];
         Debug.Log (clip);
         //int List = clips.Count;
         //Debug.Log (List);
     }
 
     //NEW
     void AddText()
     {
         display = currentIndex + " : " + clips[currentIndex].name;
         MusicListText.text = display;
     }
     //END NEW
 
     //Quit Button
     public void  GoQuit(){
         StartCoroutine(LoadQuit("MainScreen"));
     }
     
     IEnumerator LoadQuit(string levelMain){
         yield return new WaitForSeconds(0.5f); // wait time
         Application.LoadLevel(levelMain);
         
     }
 
 
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I play audio clips depending on the players movement 0 Answers

Trigger audio loop on beat with PlayScheduled 1 Answer

Can not play a disabled audio source 2 Answers

How to play music/audio 2 Answers

How To Turn Off Sound For Certain Objects? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges