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 /
avatar image
0
Question by aer12994 · Nov 03, 2016 at 07:59 AM · unity 5audio

Play music in Unity without repeating a song

Hello all, i was wondering how i can play music from a playlisrt without replaying something that has already been played. Here is my script for playing music randomly from a playlist.

 using UnityEngine;
 using System.Collections;
 
 public class musicPlayer : MonoBehaviour {
 
     public AudioClip[] clips;
     private AudioSource audioSource;
 
     // Use this for initialization
     void Start () {
         audioSource = FindObjectOfType<AudioSource>();
         audioSource.loop = false;
     
     }
 
     private AudioClip GetRandomClip()
     {
         
         return clips[Random.Range(0, clips.Length)];
     }
     
     // Update is called once per frame
     void Update () {
     if(!audioSource.isPlaying)
         {
             audioSource.clip = GetRandomClip();
             audioSource.Play();
         }
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by guneyozsan · Nov 04, 2016 at 11:35 PM

  • Create an integer array of length clips.Length

  • Assign shuffled numbers between 0 and clips.Length to this integer array. Search for a sorting algorithm or use the just-made-up-untested-sorting-algorithm presented below.

  • Play the next song in normal order getting the index from your shuffled array.

Rough untested code to be used as guideline:

 int[] songList[clips.Length];
 int songIndex = 0;
 
 for (i=0; i=clips.Length; i++) {
     songList[i] = i;
 }
 for (int i = 0; i <= clips.Length; i++) {
     int tempIndex = Random.Range(i, clips.Length);
     int tempSongNumber = songList[tempIndex];
     for (int k = tempIndex; k >= (i+1); k--) {
         songList[k] = songList[k - 1];
     }
     songList[i] = tempSongNumber;
 }
 audioSource.clip = clips(songList(songIndex));
 audioSource.Play();
 songIndex++;
Comment
Add comment · Show 1 · 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 Bunny83 · Nov 05, 2016 at 02:55 AM 1
Share

What's the point of the inner for-loop? It's enough to simply swap the element at "i" with the element at tempIndex. Also this "shuffelling" could be done without the seperate "index array".

Anyways your solution should work as well, so +1

ps.: k >= (i+1) would be the same as k > i

avatar image
0

Answer by aditya · Nov 03, 2016 at 08:05 AM

I think you are searching for something like AudioSource.time

Accept if it helped

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
avatar image
0

Answer by RangerDog · Nov 03, 2016 at 09:33 AM

Put randomclip in a variable, delete the randomclip?

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

AudioSource IsPlaying confusion 0 Answers

How to change audio output device to bottom speaker on android? 0 Answers

How do I get to the AudioMixerSnapshots of a cloned AudioMixer? 0 Answers

Audio source play stop in Unity5 3 Answers

GVR Audio Renderer not found iOS 0 Answers


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