unity 3D音乐播放器

using UnityEngine;

using System.Collections;


public class Player : MonoBehaviour {
    //中文字体
    public GUISkin ChineseSkin;
    //音频文件
    public AudioSource[] MyAudio;
    //歌曲名称
    public string[] SongNames;
    //当前歌曲编号
    public string[] SongerNames;
    //当前歌曲编号
    int SongIndex = 0;
    //设定是否循环
    bool LoopPlay = true;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}
    void OnGUI()
    {
        GUI.skin = ChineseSkin;
        if (GUI.Button(new Rect(10,10,50,30),"开始"))
        {
            MyAudio[SongIndex].Play();
        }
         if (GUI.Button(new Rect(70,10,50,30),"暂停"))
        {
            MyAudio[SongIndex].Pause();
        }
         if (GUI.Button(new Rect(130,10,50,30),"停止"))
        {
            MyAudio[SongIndex].Stop();
        }
         if (GUI.Button(new Rect(190,10,60,30),"上一首"))
        {
            ///方法一: 停止播放当前音乐
            //for (int i = 0; i < MyAudio.Length; i++)
            //{
            //    if (MyAudio[i].isPlaying)
            //    {
            //        MyAudio[i].Stop();
            //    }
            //}


            //方法二:停止当前播放音乐
            if (MyAudio[SongIndex].isPlaying)
            {
                MyAudio[SongIndex].Stop();
            }
             //上一曲
            if (SongIndex>0)
                SongIndex--;            
            else if(LoopPlay)
                SongIndex = MyAudio.Length - 1;
             //播放当前音乐
            MyAudio[SongIndex].Play();
        }
         if (GUI.Button(new Rect(260, 10, 60, 30), "下一首"))
         {
             //方法一: 停止播放当前音乐
             //for (int i = 0; i < MyAudio.Length; i++)
             //{
             //    if (MyAudio[i].isPlaying)
             //    {
             //        MyAudio[i].Stop();
             //    }
             //}


             //方法二:停止当前播放音乐
             if (MyAudio[SongIndex].isPlaying)
             {
                 MyAudio[SongIndex].Stop();
             }


             //下一曲
             if (SongIndex<MyAudio.Length-1)
                SongIndex++;
             else if (LoopPlay)
                SongIndex = 0;
             //播放当前音乐
             MyAudio[SongIndex].Play();
         }
        //选择是否进行循环
         GUI.contentColor = Color.red;
         LoopPlay = GUI.Toggle(new Rect(10, 45, 80, 30), LoopPlay, "循环播放");         


        //显示当前歌手和歌曲名称
         GUI.Label(new Rect(10, 80, Screen.width, 30), "当前歌曲名称:"+SongNames[SongIndex]);
         GUI.Label(new Rect(10, 115, Screen.width, 30), "歌手姓名"+SongerNames[SongIndex]);
         GUI.contentColor = Color.white;
    }

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值