日期:2014-05-18  浏览次数:20824 次

再次求问,如何监听http请求的mp3的在线播放?
我有一些mp3歌曲存在 d:\audio 下
没有安装IIS/TOMCAT等,想通过C# 编程开放一个端口,比如9999. 我的IP(192.168.0.1)
能让网上的用户 通过media play的URL输入 http://192.168.0.1:9999/1.mp3 访问我,
我接收到后,去d:\audio\1.mp3 提取并在线播放给他.

希望还能有朋友帮忙/

------解决方案--------------------

------解决方案--------------------
我好像以前写过一个类似,但不是很成熟,一会去找一下,抛砖引玉.呵呵.

另外,2楼的排名数很棒啊.
 lifetimeus 
高手过招(爽) 
等级: 
可用分等级:长工 
总技术分:77 
总技术分排名:281888
------解决方案--------------------
一直监听指定端口,用Socket什么的,如果有请求就应答请求,否则就一直监听。如果人数多的话,还可以考虑线程池。
------解决方案--------------------
顶顶
我也看看自己的排名,呵呵
------解决方案--------------------
比你差多了,带着4
------解决方案--------------------
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.IO;

namespace FilePlay
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            thListen = new Thread(new ThreadStart(listenConn));   //开始监听网络命令
            thListen.Start();
        }
        private static HttpListener listener;
        private static Thread thListen;
        private static Thread thUpAudio=null;
        HttpListenerResponse response = null;
        HttpListenerRequest request = null;

        private void listenConn()
        {
            CheckForIllegalCrossThreadCalls = false;
            listener = new HttpListener();
            listener.Prefixes.Add("http://*:9999/");
            try
            {
                listener.Start();
                while (true)
                {
                    try
                    {
                        HttpListenerContext context = listener.GetContext();
                        request = context.Request;
                        response = context.Response;
                        try
                        {
                            if (thUpAudio != null)
                            {
                                thUpAudio.Abort();
                            }
                        }
                        catch { }
                        thUpAudio = new Thread(new ThreadStart(DoUpAudio));  //有新的播放请求就关掉旧的,建一个新的播放线程
                            thUpAudio.IsBackground = true;
                        thUpAudio.Start();
                    }
                    catch { }
                }
            }
            catch    {}
            finally
            {
                listener.Close();
            }
        }

        private void DoUpAudio()
        {
            try
            {
                string FILE_NAME = request.RawUrl;
                FILE_NAME = @"D:\Audio\" + FILE_NAME;  //这你要自己调试一下文件名对不对
                if (!File.Exists(FILE_NAME)) { return; }

                FileInfo fileinfo = new FileInfo(FILE_NAME);
                int FILE_SIZE = (int)fileinfo.Length;
                byte[] streambytes = new byte[FILE_SIZE];

                FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read, FileShare.Read);
                BinaryReader br = new BinaryReader(fs);

                br.Read(streambytes, 0, FILE_SIZE);

                response.ContentLength64 = streambytes.Length;
                System.IO.Stream ws = response.OutputStream;
                ws.Write(streambytes, 0, streambytes.Length);

                br.Close();
                fs.Close();

                if (response != null)
                    response.Close();
            }
            catch { }
        }
    }
}