返回顶部

收藏

使用 libZPlay 播放音乐文件

更多

libZPlay 集成了所有支持格式( mp3, mp2, mp1, ogg, flac, ac3, aac, oga, wav and pcm )的编码解码器。库本身是由WINAPI编写,你无须额外的库,也不需要MFC / .NET的支持,只能在Windows下运行。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using libZPlay;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Playing test.mp3. Press Q to quit.\\n");
            // create ZPlay class
            ZPlay player = new ZPlay();
            // open file
            if (player.OpenFile("test.mp3", TStreamFormat.sfAutodetect) == false)
            {
                Console.WriteLine(player.GetError());
                return;
            }

            // get song length
            TStreamInfo info = new TStreamInfo();
            player.GetStreamInfo(ref info);
            Console.WriteLine("Length: {0:G}:{1:G}:{2:G}:{3:G}", info.Length.hms.hour,
                info.Length.hms.minute,
                info.Length.hms.second,
                info.Length.hms.millisecond);

            // start playing
            player.StartPlayback();

            TStreamStatus status = new TStreamStatus();
            TStreamTime time = new TStreamTime();
            status.fPlay = true;
            ConsoleKeyInfo cki;

            while (status.fPlay)
            {
                player.GetPosition(ref time);
                Console.Write("Pos: {0:G}:{1:G}:{2:G}:{3:G}\\r", time.hms.hour,
                time.hms.minute,
                time.hms.second,
                time.hms.millisecond);
                player.GetStatus(ref status);
                System.Threading.Thread.Sleep(50);
                if (Console.KeyAvailable)
                {
                    cki = Console.ReadKey(true);
                    if (cki.Key == ConsoleKey.Q)
                        player.StopPlayback();
                }
            }
        }
    }
}
//该片段来自于http://outofmemory.cn

标签:c#,.net,基础

收藏

0人收藏

支持

0

反对

0

发表评论