オーディオの接続

25479 ワード

既存のAMRの音声オーディオnセグメントは、HTML 5ブラウザ上で再生することが要求され、出力ストリーム時にパッチ化され、ブラウザでスクリプトでセグメント再生する必要がない.AMRファイル形式分の分析によれば、接合時に第2セグメントから始まるamrバイトストリームから上位6バイト「#!AMR」を削除する必要がある.次の2つの例は、Content-Longthを指定し、chunked方式で出力します.ここで、chunked方式はモバイルデバイスブラウザでは使用できません.
chunked出力方式:

  
    
public class AudioHandler : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
//
string voiceUrl = Path.GetFileNameWithoutExtension(context.Request.Url.LocalPath);
if ( string .IsNullOrEmpty(voiceUrl))
{
context.Response.StatusCode
= 404 ;
context.Response.End();
return ;
}

//
IAudioContract audio = AudioManager.Create();
GetAudioUrlsArgs args
= new GetAudioUrlsArgs(){ VoiceUrl = voiceUrl};
var result
= audio.GetAudioUrls(args);
if (result == null && result.Status != ExecuteStatus.OK && result.Data == null &&
result.Data.AddressList
== null && result.Data.AddressList.Count <= 0 )
{
context.Response.StatusCode
= 404 ;
context.Response.End();
return ;
}

//
context.Response.ContentType = " audio/amr " ;
context.Response.BufferOutput
= false ;

// , chunked
List < string > filePaths = result.Data.AddressList;
int count = filePaths.Count;
for ( int i = 0 ; i < count; i ++ )
{
OutputChunkedAudio(context, filePaths[i], (i
== 0 ) ? true : false );
}
context.Response.Flush();

}

public bool IsReusable
{
get { return false ; }
}

private void OutputChunkedAudio(HttpContext context, string filePath, bool first)
{
bool deleted = first ? true : false ;

Stream outputStream
= context.Response.OutputStream;
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
byte [] buffer = new byte [ 6 ];
while ( true )
{
int count = fileStream.Read(buffer, 0 , 6 );
if (count == 0 )
{
return ;
}
if ( ! deleted)
{
deleted
= true ;
continue ;
}

// context.Response.Write(Encoding.UTF8.GetString(buffer));
outputStream.Write(buffer, 0 , count);
}

}
}

}

Content-Longthの方法を指定します.

  
    
public class AudioHandler : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
//
string voiceUrl = Path.GetFileNameWithoutExtension(context.Request.Url.LocalPath);
if ( string .IsNullOrEmpty(voiceUrl))
{
context.Response.StatusCode
= 404 ;
context.Response.End();
return ;
}

//
IAudioContract audio = AudioManager.Create();
GetAudioUrlsArgs args
= new GetAudioUrlsArgs(){ VoiceUrl = voiceUrl};
var result
= audio.GetAudioUrls(args);
if (result == null && result.Status != ExecuteStatus.OK && result.Data == null &&
result.Data.AddressList
== null && result.Data.AddressList.Count <= 0 )
{
context.Response.StatusCode
= 404 ;
context.Response.End();
return ;
}

//
context.Response.ContentType = " audio/amr " ;
context.Response.BufferOutput
= false ;

//
List < byte > data = new List < byte > ();
List
< string > filePaths = result.Data.AddressList;
int count = filePaths.Count;
for ( int i = 0 ; i < count; i ++ )
{
LoadFullAudio(filePaths[i], (i
== 0 ) ? true : false , ref data);
}
context.Response.AppendHeader(
" Content-Length " , data.Count.ToString());
context.Response.OutputStream.Write(data.ToArray(),
0 , data.Count);
context.Response.Flush();

}

public bool IsReusable
{
get { return false ; }
}

private void LoadFullAudio( string filePath, bool first, ref List < byte > data)
{
bool deleted = first ? true : false ;

using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
byte [] buffer = new byte [ 6 ];
while ( true )
{
int count = fileStream.Read(buffer, 0 , 6 );
if (count == 0 )
{
return ;
}
if ( ! deleted)
{
deleted
= true ;
continue ;
}

for ( int i = 0 ; i < count; i ++ )
{
data.Add(buffer[i]);
}

}

}
}

}

f