AS3 RTMP broadcasting feed doesn't include audio - actionscript-3

I'm developing a little ActionScript3 app that broadcast the camera and microphone of the user to a RTMP feed.
The video is working great but the microphone doesn't work.
Playing the RTMP feed with VLC indicate that there is not audio feed at all, only the video feed.
The server (Wowza) is currently used with others streams so the problem is not coming from here. There was also a flash broadcast app used on the server before that was working fine.
Here's the code:
// Connection code
// Code run after connected event on connection
var h264:H264VideoStreamSettings = new H264VideoStreamSettings();
h264.setProfileLevel(H264Profile.BASELINE, H264Level.LEVEL_3_1);
outStream = new NetStream(connection);
outStream.videoStreamSettings = h264;
// Camera code
microphone = Microphone.getEnhancedMicrophone(0);
if( microphone )
{
var options:MicrophoneEnhancedOptions = new MicrophoneEnhancedOptions();
options.mode = MicrophoneEnhancedMode.FULL_DUPLEX;
microphone.enhancedOptions = options;
microphone.setLoopBack(false);
microphone.setUseEchoSuppression(true);
microphone.setSilenceLevel(0);
microphone.codec = SoundCodec.SPEEX;
microphone.encodeQuality = 10;
microphone.rate = 16;
microphone.gain = 75;
outStream.attachAudio(microphone);
}
outStream.publish( streamKey );

Related

How to load Subtitle text for Flash Video?

A Video player is created using "Video" in flash action script 3.0. And played video using net stream. The sample code is:
connection = new NetConnection();
connection.connect(null);
on connection success stream and video crested and played.
stream = new NetStream(connection);
video = new Video();
video.width = stage.stageWidth;
video.height = stage.stageHeight;
video.attachNetStream(stream);
stream.play(videoURL);
Video is playing correctly. I want to display subtitle for the video. I have .srt formeted file for the video, any solution in as3 to load the SRT for the Video on flash.
Writing a .srt parser isn't that difficult. Use the CuePoint API provided by AS3 to add cuepoints to your Video instance at runtime. Then listen for the onCuePoint event and display the relevant text in a text field.
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
var client = {};
client.onCuePoint = function(info:Object):void
{
var key:String;
for (key in info)
{
trace(key + ": " + info[key]);
}
};
ns.client = client;
var vid:Video = new Video();
vid.attachNetStream(ns);
addChild(vid);
ns.play("video.flv");
Instead of tracing the output, you can display text in an on-screen text field.

Got my external NetStream working - but how to load an .flv from my library?

How do I load an .flv (lets call it "library.flv") sitting in my (internal) Flash library into my Netstream?
I can easily load external .flv's with the below, but I need to load the .flv from my library
//Creating the video object
var ADFvideo = new Video(110,180);
videoContainer.addChild(ADFvideo);
ADFvideo.x = 0;
ADFvideo.y = 0;
//Loading the flv into the video object
var ADFnc:NetConnection = new NetConnection();
ADFnc.connect(null);
var ADFns:NetStream = new NetStream(ADFnc);
ADFvideo.attachNetStream(ADFns);
//Handling metaData (it arrives as the video starts playing, so it is the pefect time to fire the first event);
var ADFcustomClient:Object = new Object();
ADFcustomClient.onMetaData = ADFmetaDataHandler;
ADFns.client = ADFcustomClient;
var ADFinfoObj:Object;
function ADFmetaDataHandler(ADFinfo:Object):void
{
//meta stuff
}
ADFns.play("files/external.flv"); /* current way of loading the external .flv */
ADFns.addEventListener(NetStatusEvent.NET_STATUS, statusChanged);
function statusChanged(stats:NetStatusEvent)
{
//event changes
}
I don't think you can use NetStream with an embedded flv video. When you import the video to your library, you will have the option to convert it to a movieclip or to keep it as a simple video that will sit on the timeline. For the later option, you can't create an AS Linkage. It is not recommended for longer clip as you will experience syncronisation issue, playback problem and pre-loading issues.
You could try this adjustment of your posted code as a starting point.. Tested using 3-second video (H.263 / Sorenson) of 1280 width by 720 height with 44100hz 128 MP3 sound and there was no "..experiencing sluggish behaviour" on my side with a crappy testing machine.
When the video ends you need to use your function statusChanged(stats:NetStatusEvent) to handle what happens next (I've edited the code to do constant replays as example)
Anyway this code means the final compiled SWF and FLV exist as one package. So a 200kb FLV will add +200kb to the output SWF size. Just bear in mind especially with large / multiple FLV embeds.
//// Specify embed item and create Class to store such item
[ Embed(source = "yourFileName.flv", mimeType = "application/octet-stream") ]
var bytes_FLV : Class;
//// End embedded item setup
//create bytes for NStream from embedded item
var file_BA:ByteArray = new bytes_FLV();
//Creating the video object
var ADFvideo = new Video(110,180);
videoContainer.addChild(ADFvideo);
ADFvideo.x = 0;
ADFvideo.y = 0;
//Loading the flv into the video object
var ADFnc:NetConnection = new NetConnection();
ADFnc.connect(null);
var ADFns:NetStream = new NetStream(ADFnc);
ADFvideo.attachNetStream(ADFns);
//Handling metaData (it arrives as the video starts playing, so it is the pefect time to fire the first event);
var ADFcustomClient:Object = new Object();
ADFcustomClient.onMetaData = ADFmetaDataHandler;
ADFns.client = ADFcustomClient;
var ADFinfoObj:Object;
function ADFmetaDataHandler(ADFinfo:Object):void
{
//meta stuff
}
///ADFns.play("files/external.flv"); /* current way of loading the external .flv */
//Send file_BA to NStream for playback..
ADFns.play(null);
ADFns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
ADFns.appendBytes(file_BA);
ADFns.addEventListener(NetStatusEvent.NET_STATUS, statusChanged);
function statusChanged(stats:NetStatusEvent)
{
//event changes
//trace("NetStream Status : " + stats.info.code);
if (stats.info.code == "NetStream.Buffer.Empty")
{
//Buffer.Empty = video has ended..
ADFns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
ADFns.appendBytes(file_BA);
}
}

How to play a video while it is loading in Flash AS3?

I have a problem in Flash with AS3 to playing a video mp4, this is a H.264 video with AAC, I try to play video while it is loading as any web site, but my code doesn't work, I have two codes:
Using FLVPlayback:
var par:Object = LoaderInfo(this.root.loaderInfo).parameters;
//vidRepFla is my FLVPlayback
vidRepFla.addEventListener(VideoEvent.READY,vid_ready);
vidRepFla.addEventListener(VideoProgressEvent.PROGRESS,vid_progress);
vidRepFla.isLive = true;
vidRepFla.bufferTime = 1;
vidRepFla.play(par.source);
function vid_ready(e:VideoEvent):void{
trace('Playing!');
vidRepFla.play();
}
function vid_progress(e:VideoProgressEvent){
trace(e.bytesLoaded);
}
Using NetStream:
var par:Object = LoaderInfo(this.root.loaderInfo).parameters;
var video:Video;
var connect_nc:NetConnection = new NetConnection();
connect_nc.connect(null);
var stream_ns:NetStream = new NetStream(connect_nc);
stream_ns.client = this;
video = new Video();
addChild(video);
stream_ns.bufferTime = 1;
video.attachNetStream(stream_ns);
stream_ns.play(par.source);
Do you know if I need something more or if this type video don't work the bufferTime?
Thanks!
I work with Netstream and use a different method. I did notice something on the Adobe documentation for this. I read that the bufferTime setting Only works on flv files, this could be the problem since you are using mp4.
More information here:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/NetStream.html

Play MP3 in AIR app for iOS and Android

I was wondering if someone could shed some light on the best method to embed, select and play audio files in an AIR app built for iOS and Android?
I have an app where a user can select a file to play from a list of 10 audio files. These are selected when a slider moves left or right.
I currently would create 10 individual embed snippets and classes
[Embed(source="assets/audio/file1.mp3)]
public static const file1:Class;
[Embed(source="assets/audio/file2.mp3)]
public static const file2:Class;
...
And then in the app initialise each class so I can reference them. Then simply call
file1.play();
Issue is this only plays the sound once where I would like the sound to look until the user selects another sound.
I guess a couple of questions:
1. Is having 10 embed/classes the best way to handle 10 different MP3 files?
2. How would I loop the MP3 seamlessly
Thanks
You stored Array or Vector mp3 files URL which user selected. and playing mp3 filed ended. load next URL from Array, Vector.
var sound:Sound = new Sound();
var soundChannel:SoundChannel;
var currentIndex:int = 0;
var mp3List:Vector.<String> = new Vector.<String>();
//only stored user selected mp3 url
sound.addEventListener(Event.COMPLETE, onMp3LoadComplete);
sound.load(mp3List[currentIndex]);
function onMp3LoadComplete(e:Event):void
{
sound.removeEventListener(Event.COMPLETE, onMp3LoadComplete);
soundChannel = sound.play();
soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
}
function onSoundChannelSoundComplete(e:Event):void
{
e.currentTarget.removeEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
currentIndex++;
if(currentIndex==mp3List.length) currentIndex = 0;
sound.load(mp3List[currentIndex]);
soundChannel = sound.play();
sound.addEventListener(Event.COMPLETE, onMp3LoadComplete);
}

Audio FLVPlayback not working

I currently have an SWF application that records audio and saves it to FMS. Then inside Flash Professional I'm trying to use a FLVPlayback Component to play the recording, but it never plays.
The recording is saved here: RootInstall/applications/myapp/streams/folder/audioFile.flv
Then I have my FLVPlayback component source looking at rtmp://server-ip/myapp/streams/folder/audioFile but I can't get it to play.
I've also copied the recored audio file and stuck it in the vod application but couldn't get it to play. When I tried the sample.flv videos, they worked.
Here's how I'm recording the audio in AS3:
nc.connect("rtmp://server-ip/myapp/folder");
...
ns = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
mic = Microphone.getMicrophone();
ns.attachAudio(mic);
// start publishing
ns.publish("audioFile", "record");
Everything on the recording side was correct, it was the way I was trying to read the FLV was incorrect.
Based on the code above here's the player side I coded.
...
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
nc.connect("rtmp://server-ip/myapp/folder");
private function netStatusHandler(event:NetStatusEvent):void
{
trace(event.info.code);
switch (event.info.code)
{
case "NetConnection.Connect.Success" :
//Connected Successfully
playLiveStream();
break;
case "NetConnection.Connect.Rejected" :
// Connection Rejected
break;
case "NetConnection.Connect.Failed" :
// The server may be down or unreachable
break;
case "NetConnection.Connect.AppShutDown" :
// The application is shutting down
nc.close();
break;
case "NetConnection.Connect.Closed" :
//Connection Closed
break;
}
}
private function playLiveStream():void {
nsPlay = new NetStream(nc);
nsPlay.client = this;
nsPlay.bufferTime = 0.1;
nsPlay.play("audioFile", -1);
}