Recording sound in FMS - actionscript-3

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

Related

play a external flv video

Hello please someone can help me with this ...
I want to play a external flv video ("../sync/video/video.flv"), but in case the video is missing or when there is a (StreamNotFound) error
I want to play automatically another flv video.
case "NetStream.Play.StreamNotFound":
ns.play("../sync/filler/video2.flv");
but it doesn't work ....
here is the full code :
var vid:Video;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
var customClient:Object = new Object();
customClient.onMetaData = metaDataHandler;
ns.client = customClient;
ns.play("../sync/video/video.flv");
vid = new Video();
vid.attachNetStream(ns);
addChild(vid);
function netStatusF(e:NetStatusEvent):void
{
switch (e.info.code)
{
case "NetStream.Play.StreamNotFound" :
ns.play("../sync/filler/video2.flv");
break;
}
}
function metaDataHandler(infoObject:Object):void
{
vid.width = infoObject.width;
vid.height = infoObject.height;
}
You have just to add a NetStatusEvent.NET_STATUS event listener to your NetStream object :
ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusF);
Then you have to assure that your second video file exist, otherwise you'll have a looping problem.
Hope that can help.

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
}

mute unmute button in actionscript 3

i need some help with my actionscript 3 project. I have a button with a sound in it. I have some code (see below) that when i press the button it plays the sound and if i press the button again it stops the sound (like a mute/unmute button). The problem is that when i press the button to play the sound for the second time it plays two sounds (the same sound twice) and if i press the button to play the sound more times the same sound plays many times. Can you please help me solve the problem? Thank you.
function setMute1(vol){
sTransform1.volume = vol;
SoundMixer.soundTransform = sTransform1;
}
var sTransform1:SoundTransform = new SoundTransform(1,0);
var Mute1:Boolean = true;
sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
function toggleMuteBtn1(event:Event) {
if(Mute1 === false) {
Mute1 = true;
setMute1(0);
} else {
Mute1 = false;
setMute1(1);
}
}
From what I understand you start the sound by assigning it to the buttons hit frame? You need to have the sound started by the code in order to control the sound in good way.
Here is an working example based on your code, that loads an external mp3 file. The sound is played and stopped via the same button.
// load the sound
var mySound:Sound = new Sound();
mySound.load(new URLRequest("loop.mp3"));
var myChannel:SoundChannel = new SoundChannel();
// tool you need for manipulating the volume;
var sTransform1:SoundTransform = new SoundTransform(1,0);
// The sound starts not muted
var Mute1:Boolean = true;
var vol:Number = 0;
sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
// Set the sound volume;
function setMute1(vol)
{
sTransform1.volume = vol;
SoundMixer.soundTransform = sTransform1;
// Check if sound is muted
if (vol<=0)
{
Mute1 = true;
}
else
{
Mute1 = false;
}
}
// Start/stop sound
function startOrStop()
{
if (Mute1 === false)
{
myChannel.stop();
setMute1(0);
}
else
{
setMute1(1);
myChannel = mySound.play();
}
}
// This happens when you click the buttom
function toggleMuteBtn1(event:Event)
{
startOrStop()
}
In actionscrip 2 there was a function that would stop all sounds, in actionscript 3 you can't do that anymore, but you can still assign sounds to frames.
This example mutes and unmutes the sound. The sound is't stopped, just muted.
Also here the sound must be assigned in the code, and not to the frame.
// load the sound
var mySound:Sound = new Sound();
mySound.load(new URLRequest("loop.mp3"));
var myChannel:SoundChannel = new SoundChannel();
// tool you need for manipulating the volume;
var sTransform1:SoundTransform = new SoundTransform(1,0);
// The sound starts not muted
var Mute1:Boolean = true;
var vol:Number = 0;
sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
// Set the sound volume;
function setMute1(vol)
{
sTransform1.volume = vol;
SoundMixer.soundTransform = sTransform1;
// Check if sound is muted
if (vol<=0)
{
Mute1 = true;
}
else
{
Mute1 = false;
}
}
// Toggle mute on/off
function toggleMute()
{
if (Mute1 === false)
{
setMute1(0);
}
else
{
setMute1(1);
}
}
// This happens when you click the buttom
function toggleMuteBtn1(event:Event)
{
// if not playing, the sound
if (myChannel.position != 0) {
} else {
myChannel = mySound.play();
}
toggleMute();
}

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

Problem with displaying video when streaming

I'm having a problem when streaming video. Randomly the video isnt shown, the video is playing though as the playhead moves and audio sounds.
It's strange though because if I press pause then play the video appears and also if I make it fullscreen it appears.
private var videoURL:String = "filename.f4v";
private function setupConnection():void
{
connection = new NetConnection();
connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onErrorConnect);
connection.connect("rtmp://url to my streaming server");
}
private function netStatusHandler(event:NetStatusEvent):void
{
trace("event.info.code "+event.info.code);
switch (event.info.code) {
case "NetConnection.Connect.Success":
connectStream();
break;
case "NetStream.Play.Start":
onPlayVideoHandler();
break;
case "NetStream.Play.StreamNotFound":
trace("Stream not found: " + videoURL);
break;
default :
}
}
private function onErrorConnect(event:AsyncErrorEvent):void
{
trace("onErrorConnect: " + event);
}
private function securityErrorHandler(event:SecurityErrorEvent):void
{
trace("securityErrorHandler: " + event);
}
private function connectStream():void
{
stream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
stream.bufferTime = 10;
var client:Object = new Object();
client.onMetaData = onMetaData;
stream.client = client;
video = new Video(200, 200);
video.name = "video";
video.addEventListener(Event.ADDED_TO_STAGE, videoAddedToStage)
video.attachNetStream(stream);
video.smoothing = true;
video.x = 0;
video.y = 0;
mainHolder.addChild(video);
stream.play(videoURL, 0, 100, true);
stream.seek(0);
}
private function onPlayVideoHandler():void
{
// add Controls
}
OK Ive found out the reason it doesnt show is because the video sometimes has a width and height of 0 pixels. Anybody know why it would return these values? Is it something todo with the nature of rtmp streaming videos?
I had to listen for the width and height to be greater than zero before proceeding. I never found out why but this is how to fix it.