Audio FLVPlayback not working - actionscript-3

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);
}

Related

How to display mp4 then pause and replay in Flash CC

I’m resurrecting a simple banner where we used an FLV movie and the following code to have the movie pause using setInterval and then play again. It’s a simple butterfly movie that flaps it’s wings and then goes back to a static state.
Looks like FLVs are now discouraged in the more recent Flash versions so I would like to know how to make a movie play, then pause using setInterval, then replay.
Here is the working AS3 when using an FLV on the Timeline in a frame based animation but does not work with an imported .mp4:
stop();
function timesUp()
{
gotoAndPlay(1,"Scene 1");
clearInterval(itv);
}
var itv=setInterval(timesUp, 15000);
Do the new suggested methods use a single frame movie where you load and play the mp4, then use setInterval as a timer to replay?
I can’t find any tutorials that have this method as an example.
I did read a tutorial on the Adobe site but the following did not load and play the movie (morpho.mp4)
var nc:NetConnection = new NetConnection();
nc.connect(null);
var vid:Video = new Video();
addChild(vid);
var ns:NetStream = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
function netStatusHandler(event:NetStatusEvent):void
{
// handle netStatus events, described later
}
function asyncErrorHandler(event:AsyncErrorEvent):void
{
// ignore error
}
vid.attachNetStream(ns);
ns.play("morpho.mp4");
Try this :
import flash.utils.Timer
import flash.events.TimerEvent
var video_playing:Boolean = false,
timer:Timer,
nc:NetConnection,
ns:NetStream,
video:Video,
video_name:String = 'video.mp4'
nc = new NetConnection()
nc.connect(null)
ns = new NetStream(nc)
ns.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
function netStatusHandler(event:NetStatusEvent):void {
switch (event.info.code){
case 'NetStream.Play.Start' :
if(timer.currentCount == 0){
timer.start()
}
break
}
}
function asyncErrorHandler(event:AsyncErrorEvent):void {
}
video = new Video(135, 135) // set the size of video to 135x135px
video.x = 165 // set the left of video to 300 - 135 = 165px
video.y = 0 // set the top of video to 0
video.attachNetStream(ns)
addChild(video)
timer = new Timer(1000, 3) // 3 seconds, just for example
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timer_on_Complete)
function timer_on_Complete(e:TimerEvent){
if(video_playing){
video_playing = false
ns.close()
} else {
start_video()
}
timer.reset()
timer.start()
}
start_video()
function start_video(){
ns.play(video_name)
video_playing = true
}

video stream: "onBWDone: undefined" and video doesn't play

I'm trying to stream a video from Amazon's CloudFront/S3 service. Despite my filename being correct, and a "NetConnection.Connect.Success" status, my NetConnection onBWDone callback is giving me an "undefined" error and the video doesn't play or show up anywhere on the stage. The connection reports that it's successful so I don't know where the problem lies. Here is my code:
var amazonCloudFrontDomain:String = "myCloudFrontDistributionID.cloudfront.net";
var amazonCloudFrontStreamURL:String = "rtmp://" + amazonCloudFrontDomain + "/cfx/st/";
var videoFileName:String = "myVideo.flv";
Security.allowDomain(amazonCloudFrontDomain);
Security.allowDomain("rtmp://" + amazonCloudFrontDomain);
Security.allowDomain(amazonCloudFrontStreamURL);
var client:Object = new Object();
client.onBWDone = function(e){trace("onBWDone: " + e);}
client.onMetaData = function(e){trace("onMetaData: " + e);}
client.onCuePoint = function(e){trace("onCuePoint: " + e);}
var video:Video = new Video(300, 400); // create a new Video item and set its width and height
video.x = 0; // position the video's x position
video.y = 0; // position the video's y position
var duration:Number; // use this later to get the duration of the video being played
this.addChild(video);
var nc:NetConnection = new NetConnection(); // variable for a new NetConnection
nc.client = client;
nc.addEventListener(NetStatusEvent.NET_STATUS,netConnectionStatusHandler,false,0,true);
nc.connect(amazonCloudFrontStreamURL); // set the nc variable to null
var ns:NetStream;
function netConnectionStatusHandler(e):void
{
switch(e.info.code)
{
case "NetConnection.Connect.Success":
trace("S3 Connected");
ns = new NetStream(nc); // create a variable for a new NetStream connection & connect it to the nc variable
ns.addEventListener(NetStatusEvent.NET_STATUS, netStreamStatusHandler); // add a listener to the NetStream to listen for any changes that happen with the NetStream
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, netStreamAsyncErrorHandler); // add a listener to the NetStream for any errors that may happen
ns.client = client;
video.attachNetStream(ns); // attach the NetStream variable to the video object
video.smoothing = true;
video.deblocking = 1;
ns.bufferTime = 5; // set the buffer time to 5 seconds
ns.play(videoFileName); // tell the netstream what video to play and play it
break;
}
trace(e.info.code);
}
function netStreamAsyncErrorHandler(Event:AsyncErrorEvent):void
{
// trace(event.text); // this will handle any errors with video playback
}
function netStreamStatusHandler(event:NetStatusEvent):void
{
trace(event.info.code); // this will handle any events that are fired when the video is playing back
switch(event.info.code) // switch statement to handle the various events with the NetConnection
{
case "NetStream.Buffer.Full": // when the buffer is full fire the code below
ns.bufferTime = 10; // set buffer time to 10 seconds break;
case "NetStream.Buffer.Empty": // when the buffer is empty fire, code below
ns.bufferTime = 10; // set buffer time to 10 seconds
break;
case "NetStream.Play.Start": // when the video starts playing, fire the code below
ns.bufferTime = 10; // set the buffer time to 10 seconds
break;
case "NetStream.Seek.Notify": // when you seek with the scrubber it sends a notify signal of the time
ns.bufferTime = 10; // set the buffer time to 10 seconds
break;
case "NetStream.Seek.InvalidTime": // when you release the scrubber ahead of the video that has been loaded, you get this error. it will jump you back to the last frame that has been loaded
ns.bufferTime = 10; // set the buffer time to 10 seconds
break;
case "NetStream.Play.Stop": // when you reach the end of the video
ns.pause(); // pause the video
ns.seek(1); // seek the video to the first frame
break;
}
}
This is the console output:
S3 Connected
netConnectionStatusHandler: NetConnection.Connect.Success
onBWDone: undefined
NetStream.Play.Reset
NetStream.Play.Start
...and nothing happens. No video, no audio, nothing. Can anyone see any problems with my code?
I believe that not all streaming applications have to use the onBWDone function, so if they don't use it, it probably passes null or undefined to the function instead of the kbps.
So you're problem is likely with the e in your trace statement below:
client.onBWDone = function(e){trace("onBWDone: " + e);}
Also, the function does not recieve an event object, but rather a ... rest array that usually only has one item in it. try this instead:
client.onBWDone = function(... rest){ //
if(rest && rest.length > 0){ //since the function might not get any data, you need to check before trying to trace it out
trace("onBWDone: " + rest[0]); //this is the kbps of the bandwidth available
}
};
You can learn more at the Adobe docs:
http://help.adobe.com/en_US/FlashMediaServer/3.5_Deving/WS5b3ccc516d4fbf351e63e3d11a0773d56e-7ffa.html
EDIT
In regards to your video path:
Flash Media Server applications are accessed in the following format over rtmp: "server:port/application/instance". For VOD, instance is the name of your file and for FLV's it doesn't require the extension.
http://help.adobe.com/en_US/FlashMediaServer/3.5_Deving/WS5b3ccc516d4fbf351e63e3d11a0773cfae-7ff3.html
I changed
var videoFileName:String = "myVideo.flv";
to
var videoFileName:String = "myVideo";
and now it works. For some reason, excluding the file extension makes it work for me. I don't know why...strange.

Preload a bit of FLV or F4V with the main project loader

My project has a main loader that load all the assets for the project. I need to load a bit of my video with it.
The video has 16mb, i want to load 3mb to use after the main loader is completed.
I've tried to open a new connection using Netconnection/Netstream to load 3mb and close the connection, but when the project starts and the video is played, a new connection is opened loading it from beginning.
I'm trying to find a way that i can use those 3mb already loaded. Doing this way, the user don't need to wait a main loader and a secondary loader (buffertime).
That's my code, sorry guys.
var loader:Loader = new Loader();
var nc:NetConnection = new NetConnection();
var ns:NetStream = new NetStream(nc);
var client:Object = new Object();
var swfRatio:Number;
var videoRatio:Number;
function init():void
{
nc.connect(null);
client.onCuePoint = cuePointHandler;
client.onMetaData = metaDataHandler;
ns.client = client;
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressLoader);
addEventListener(Event.ENTER_FRAME, progressTotal);
loader.load(new URLRequest("swf/main.swf"));
ns.play("f4v/main_movie.f4v");
ns.pause();
}
function progressLoader(event:ProgressEvent):void
{
swfRatio = (event.bytesLoaded / event.bytesTotal);
}
function progressTotal():void
{
//Here i get the amount that i want to preload from my video, in this case i want 3mb or 3072000 bytes
videoRatio = (ns.bytesLoaded / 3072000);
//This is a variable that i use to fill my loader asset and verify if my content its totaly loaded.
var frameValue:int = ((videoRatio + swfRatio) / 2) * 100;
if (frameValue >= 100)
{
removeEventListener(Event.ENTER_FRAME, progressTotal);
// Here i close my connection, i suppose that i need to use the same connection in my player.
ns.close();
ns = null;
nc.close();
nc = null;
loaderComplete();
}
}
function loaderComplete():void
{
removeChild(assetLoader);
//Here i add my player to the stage, i want to use the preloaded video with him.
addChild(loader.content);
}
function cuePointHandler(infoObject:Object):void {
trace(infoObject.name);
}
function metaDataHandler(infoObject:Object):void {
trace("metaData");
}
Then in my player that i've just loaded and added to the stage i'm using OSMF to help me with controls.
To test the "preloaded video" i'm doing this:
private var mediaPlayer:MediaPlayerSprite;
private function _init(e:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, _init);
mediaPlayer = new MediaPlayerSprite();
addChild(mediaPlayer);
//And here OSMF start to load the entire main_movie.f4v again.
mediaPlayer.resource = new URLResource("f4v/main_movie.f4v");
}
It looks like your strategy would work if you let the video download completely. I'm assuming if the video downloaded completely, the browser may cache it and make it available to the next NetStream that comes along.
Your strategy looks OK otherwise. What you are doing (playing, then pausing the video immediately) is the way to start buffering the video. But since there are two NetStream's being used (one by the main loader, the other by the OSMF player) this won't work.
Perhaps you can devise a scheme where you pass the NetStream from the main loader into the loaded SWF (main.swf). So that it can use the data it's already downloaded. Just a thought, I've never tried this.
Another idea would be to get the OSMF player in your main.swf to do the buffering. That would mean the buffering would start happening only after main.swf is loaded (which may be too late).

Recording sound in FMS

hello
I want to save a loaded sound in FMS .
public function Record()
{
nc.connect("rtmp://192.168.1.2:1935/videoRecorder");
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
play_btn.visible = stop_btn.visible = start_btn.visible = false;
}
function netStatusHandler(event:NetStatusEvent):void
{
t1_txt.text = event.info.code;
trace(event.info.code);
switch (event.info.code)
{
case "NetConnection.Connect.Success" :
connectStream();
break;
case "NetStream.Play.StreamNotFound" :
//trace("Stream not found: " + videoURL);
break;
}
}
function connectStream()
{
ns = new NetStream(nc);
var mic:Microphone = Microphone.getMicrophone();
var cam:Camera = Camera.getCamera();
if (cam)
{
cam.setMode(400,300,15,false);
cam.setQuality(0,100);
ns.attachAudio(mic);
ns.attachCamera(cam);
video.attachCamera(cam);
video.height = 300;
video.width = 400;
addChild(video);
start_btn.visible = true;
start_btn.addEventListener(MouseEvent.MOUSE_UP,startRecord);
}
else
{
t1_txt.text = "No camera attached";
}
}
This is my code. But i need to save my loaded sound insted of mic . Is it possible?
There is no way to attach a Sound to a NetStream.
You can solve your issue in the following way:
remove the attachAudio part from your code
when the video recording is completed, use a web or FTP server to upload the sound file to FMS (there are no other ways to upload files to FMS).
play back the audio and the video at the same time (or you can mix them with FFMPEG)
Cheers
Tamas Gronas

NetStream.publish Webcam to FMS working in standalone player, but not in browser

I am trying to publish the video of a webcam to a Flash Media Server 2.
My code is working in the Flash standalone player (tested with 10.0 and 10.2), but not in the browser plugin (tested with 10.2, both in IE and Opera.
The connection to my FMS is working successfully, but after the publish, nothing happens, I never get the NetStream.Publish.Start Event. On the server I can see the connection in the management console, even the stream in the streams tab. But I cannot connect to that strea.
Anybody an idea what could be going wrong?
NetConnection.defaultObjectEncoding = ObjectEncoding.AMF0; // needed to get correct connection to FMS2
private function netStatusHandler(event:NetStatusEvent):void {
output.appendText("\n" + event.info.code);
switch (event.info.code) {
case "NetConnection.Connect.Success":
output.text = ("Connection successful, streaming camera...");
connectCamera();
break;
case "NetConnection.Connect.Failed":
break;
case "NetStream.Play.StreamNotFound":
break;
case "NetStream.Publish.Start":
output.appendText("\nPublishing video!");
break;
}
}
private function connectCamera(ev:Event = null):void {
var stream:NetStream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
stream.attachCamera(camera);
videoURL = createGUID();
stream.publish(videoURL, "live");
output.text = "publish stream...";
}
Ok, I found what my problem is here:
I declare the reference to the stream variable inside the connectCamera function:
private function connectCamera(ev:Event = null):void {
var stream:NetStream = new NetStream(connection);
}
So stream is only declared inside the scope of that function.
This doesn't seem to be a problem in the standalone player, but it is in the browser plugin. The browser plugin seems to do a much more thourough job on the garbage collector, garbage collecting my stream after the function has executed.
So what I have to do is declare the stream variable outside of the function scope, inside the class scope.
var stream:NetStream;
private function connectCamera(ev:Event = null):void {
stream = new NetStream(connection);
}
You should always declare stuff that you need later as a field on your class, and not inside a function. You just never know when GC might clean up that stuff.