Flex playing mp3 stream doesn't work - actionscript-3

I've copied the sample code from Adobe for playing an mp3 stream in Flex mobile but it doesn't seem to work.
The stream i used as an example works perfectly fine in Winamp.
This is my code:
import flash.net.*;
import flash.media.*;
private var req:URLRequest;
private var context:SoundLoaderContext = new SoundLoaderContext(8000, true);
private var s:Sound;
private var channel:SoundChannel = new SoundChannel();
private function AudioOn():void
{
req = new URLRequest("http://stream2.srr.ro:8000");
s = new Sound(req,context);
channel=s.play();
}
private function onInit() : void {
AudionOn();
}
Using the debbuger the s (sound) object has the following state:
s.isBuffering is true;
s.isURLInaccesible is false;
s.bytesLoaded = 0
s.bytesTotal = 0;
This seems to be an easy task but why doensn't this example work ?
Thanks a lot!
Dan

Have you ever tried to modified your sound file by
Remove all metadata in the sound file.
Export it to a new MP3 file.
Is it too large to load? Try to add error event to listen.

Related

ActionScript 3.0 sound not working

So having trouble making sound on keyboard press
I have the imports:
import flash.net.URLRequest;
import flash.media.Sound;
I have the variables
private var soundDownRequest:URLRequest = new URLRequest ("SoundDown.mp3");
private var downSound:Sound = new Sound (soundDownRequest);
and the event listener
private function keyDownHandler(evt:KeyboardEvent):void
{
if (evt.keyCode == 40)//ascii for down arrow
{
downSound.play();
}
}
The sound folder is in the same folder as the .as, its also in the library of the fla, yet it still doesn't work. Any idea why?
Thank you.
Update:
I got the sound to work but not using the external method I was trying to do above.
Had to do it internally.
so you need:
import flash.media.SoundChannel;
-Then you need to make sure your sound file is in your fla library.
once its in the library
-Right click > properties
-Select the Action Script Tab
-Check "export for action script"
-Give the class a name in accordance to the sound
-press ok
add this variable (your will be different):
private var downSound:TheDownSound = new TheDownSound();
downsound is the selected name of the variable, and TheDownSound is the name of the class (the one made earlier for the sound file)
then add this to where you want the sound to play:
var myDownSound:SoundChannel = downSound.play();
Do this if you cant get it working externally like me.
for a better explanation watch this guys youtube video:
https://www.youtube.com/watch?v=SZpwppe7yGs
Your code is working perfectly ok if you put your .mp3 file in the same folder as the output .swf, not near the class .as source file (because its the swf file loading the sound, so the path must be relative to it)
public class ASEntryPoint extends Sprite {
private var soundDownRequest:URLRequest = new URLRequest ("click.mp3");
private var downSound:Sound = new Sound (soundDownRequest);
public function ASEntryPoint() {
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
}
private function keyDownHandler(evt:KeyboardEvent):void{
if (evt.keyCode == 40) {
downSound.play();
}
}
}
You need to load the external file, which is asynchronous operation. Then you track the loading event and if it all goes normally you can play your loaded sound.
import flash.events.SecurityErrorEvent;
import flash.events.IOErrorEvent;
import flash.events.Event;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundChannel;
// Keep the sound loader from being garbage collected.
var soundLoader:Sound;
function loadSound(url:String):void
{
var aRequest:URLRequest = new URLRequest(url);
soundLoader = new Sound();
// Handle the normal loading.
soundLoader.addEventListener(Event.COMPLETE, onLoaded);
// Handle the error cases.
soundLoader.addEventListener(IOErrorEvent.IO_ERROR, onError, false, 0, true);
soundLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError, false, 0, true);
soundLoader.load(aRequest);
}
var audioChannel:SoundChannel;
function onLoaded(e:Event):void
{
// Sound is available here for playback.
audioChannel = soundLoader.play();
}
function onError(e:Event):void
{
trace(e);
}
You can also handle your sound as a streaming audio, but I worked with that years ago in AS2 so I cannot help here. Still, internet suggests a link: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d22.html

It's possible to write a sharedobject when allowscriptaccess is = "never"?

I will try to write a sharedObject item using my swf embeded into an html page.
"allowscriptaccess" is setted to "never". I can't write sharedObject!
However if I set allowscriptaccess to "always", write action work well...
If I can't use sharedObject with allowscriptaccess setted to never, exist alternative for saving data?
I write a little and stupid example:
public class Main extends Sprite {
private var SHARED_NAME:String = "__SO__";
private var so:SharedObject;
Security.allowDomain("*");
Security.allowInsecureDomain("*");
public function Main() {
this.so = SharedObject.getLocal(this.SHARED_NAME, "/");
this.setSharedObj("YEAHHHHHHHH");
this.getSharedObj();
}
public function getSharedObj(clientId:String = null):Object {
var url:String = "http://localhost:8080?so=" + this.so.data.test;
var request:URLRequest = new URLRequest(url);
var loader:URLLoader = new URLLoader();
loader.load(request);
return this.so.data;
}
public function setSharedObj(setValue:String):void {
this.so.data.test = setValue;
this.so.flush();
}
}
When embed the compiled swf using AllowScriptAccess: "never", get Request never have setted the queryparam so.
If set AllowScriptAccess to "always" queryparam will be correctly setted
#akmozo is right, the problem was not related by AllowScriptAccess, but from my way of testing.
The problem is:
Using firefox in anonymous mode, every time you reload the page, shared object are deleted.
I thought for anonymous session are saved, it is not so!

Recording Flash Webcam FMS 4.5 to Mp4 results in terrible quality

I have successfully setup recording webcam to FLV using FMS 4.5 developer edition, so I wanted to attempt recording to an Mp4 next. I am doing a silent save of the video file because the goal here is to be able to have these videos playable outside of Flash/FMS. I set the program up to save the Mp4 file generated by FMS, but the quality is terrible. I am seeing green distortion when movement is captured, and heavy pixelation. Here is my test application code that saves the video file after 5 seconds of recording. Can anyone please point out where I am going wrong? Any help is greatly appreciated.
package com
{
import flash.display.*;
import flash.net.*;
import flash.utils.Timer;
import flash.events.*;
import flash.filesystem.File;
import flash.media.*;
public class Main extends MovieClip
{
private var nc:NetConnection;
private var ns:NetStream;
private var nsPlayer:NetStream;
private var vid:Video;
private var vidPlayer:Video;
private var cam:Camera;
private var mic:Microphone;
private static const LOCAL_VIDEO:String = "myCamera";
private static const VIDEO_FPS:uint = 30;
private static const SAVE_FOLDER_NAME:String = "Saved_Videos";
private static const PATH_TO_FMS:String = "C:/Program Files/Adobe/Flash Media Server 4.5";
private var timer:Timer = new Timer(5000, 1);
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(evt:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
nc.connect("rtmp://localhost/PublishLive/myCamera");
}
function onNetStatus(evt:NetStatusEvent):void
{
if(evt.info.code == "NetConnection.Connect.Success")
{
publishCamera();
displayPublishingVideo();
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleted);
timer.start();
}
}
private function timerCompleted(evt:TimerEvent)
{
trace("timer completed");
timer.stop();
ns.close();
ns = null;
var saveFile:File = new File(PATH_TO_FMS + "/applications/PublishLive/streams/" + LOCAL_VIDEO + "/" + LOCAL_VIDEO + ".mp4");
var fileName:String = "Video" + ".mp4";
var dir:File = File.documentsDirectory.resolvePath(SAVE_FOLDER_NAME);
dir.createDirectory();
var fileToSave = dir.resolvePath(fileName);
if(fileToSave.exists)
{
fileToSave.deleteFile();
}
saveFile.copyTo(fileToSave, true);
}
private function publishCamera()
{
var h264Settings:H264VideoStreamSettings = new H264VideoStreamSettings();
h264Settings.setProfileLevel(H264Profile.BASELINE, H264Level.LEVEL_3_1);
cam = Camera.getCamera();
cam.setMode(stage.stageWidth, stage.stageHeight, VIDEO_FPS, true);
cam.setQuality(0, 90);
cam.setKeyFrameInterval(15);
cam.setMotionLevel(100);
mic = Microphone.getMicrophone();
mic.setSilenceLevel(0);
mic.rate = 11;
ns = new NetStream(nc);
ns.videoStreamSettings = h264Settings;
ns.attachCamera(cam);
ns.attachAudio(mic);
ns.publish("mp4:myCamera.mp4", "record");
}
private function displayPublishingVideo():void
{
vid = new Video();
vid.width = stage.stageWidth;
vid.height = stage.stageHeight;
vid.attachCamera(cam);
addChild(vid);
}
}
}
Ok, so I am answering my own question. I found the answer here along with the link to the tool to process: adobe help site
You must convert the files after recording them using a post-processing tool so they can be viewed in other video players.
Edit: VLC can actually play the unprocessed file, so I thought it was a quality issue at first!
You could try to change the quality of the video stream VideoStreamSettings.setQuality(bandwidth:int, quality:int) (link).
You only set one of the bandwidth or quality values and leave the other to 0. I would try to set bandwidth (measured in bytes per second) to 750000 (6 Mbps), which should be plenty for anything less than full HD.
So, in your case, you could try:
h264Settings.setQuality(750000, 0);

RTMP streaming with OSMF - AS3

New to OSMF and trying to play a streaming mp4 on our limelight server. According to this tutorial http://www.adobe.com/devnet/flash/articles/video_osmf_streaming.html, you simply pass the RTMP link to the URLResource. I've tried that and it isn't working. It plays fine if I pass a local URL. I am using OSMF 1.5 SWC and my code is
package
{
import flash.display.*;
import flash.events.*;
import org.osmf.media.*;
public class Main extends Sprite
{
private var mps:MediaPlayerSprite;
public function Main()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
mps = new MediaPlayerSprite();
mps.width = 640;
mps.height = 360;
mps.resource = new URLResource("rtmp://my.limelight.host.net/mp4:dyk_seatbelts_high.mp4");
addChild(mps);
}
}
}
I dont get any errors just a blank canvas. Any ideas?
You should add streamer and video url for RTMP streaming. For example:
var resource:DynamicStreamingResource = new DynamicStreamingResource(videoStreamer);
resource.urlIncludesFMSApplicationInstance = true;
var vector:Vector.<DynamicStreamingItem> = new Vector.<DynamicStreamingItem>(1);
vector[0] = new DynamicStreamingItem(videoUrl, 1200);
resource.streamItems = vector;
element = new VideoElement(resource);
player.media = element;
You can add few dynamic streaming items. Video files with different bitrate.
Example for videoStreamer: rtmp://streamer_url
Example for videoUrl: mp4:path_to_video.mp4
This is just an update. The DynamicStreamingItem is not available anymore. You can simply add your rtmp stream url to a StreamingURLResource. Plays like a charm. (Correct me if i'm wrong....i'm new to OSMF)
var videoElement:VideoElement = new VideoElement();
videoElement.resource = new StreamingURLResource("rtmp://cp140972.XXXXX",StreamType.LIVE,NaN,NaN,null,false);
player.media = videoElement;

Flixel - How to load and play an embedded swf file

I have been searching the web and all of the codes I have found are to play external swf files that have a timeline. The file that I am trying to load does not have a timeline. I am using the Flixel framework for this project and the file that I want to play is also made in Flixel(don't have the source file just the swf file).
Most of the code I have is from a cutscene template that I found on the Flixel forum. Here is what I have so far:
package
{
import org.flixel.FlxState;
import org.flixel.FlxG;
import flash.display.MovieClip;
import flash.media.SoundMixer;
import flash.events.Event;
public class SponsorsState extends FlxState
{
//Embed the cutscene swf relative to the root of the Flixel project here
[Embed(source='assets/DirtPileLogo.swf', mimeType='application/octet-stream')] private var SwfClass:Class;
//This is the MovieClip container for your cutscene
private var movie:MovieClip;
//This is the length of the cutscene in frames
private var length:Number;
override public function create():void
{
movie = new SwfClass();
//Set your zoom factor of the FlxGame here (default is 2)
var zoomFactor:int = 2;
movie.scaleX = 1.0/zoomFactor;
movie.scaleY = 1.0 / zoomFactor;
//Add the MovieClip container to the FlxState
addChildAt(movie, 0);
//Set the length of the cutscene here (frames)
length = 100;
//Adds a listener to the cutscene to call next() after each frame.
movie.addEventListener(Event.EXIT_FRAME, next);
}
private function next(e:Event):void
{
//After each frame, length decreases by one
length--;
//Length is 0 at the end of the movie
if (length <= 0)
{
//Removes the listener
movie.removeEventListener(Event.EXIT_FRAME, next);
//Stops all overlaying sounds before state switch
SoundMixer.stopAll();
//Enter the next FlxState to switch to
FlxG.state = new PlayState();
}
}
}
}
When I run this I get this error: Type Coercion failed: cannot convert SponsorsState_SwfClass#fb5161 to flash.display.MovieClip., all I want to do is play the swf file for a set frame count then move onto the next state.
Any ideas on how to do this?
Try to replace the following:
[Embed(source='assets/DirtPileLogo.swf', mimeType='application/octet-stream')]
private var SwfClass:Class;
//This is the MovieClip container for your cutscene
private var movie:MovieClip;
into
//Mark your symbol for export and name it => MyExportedSymbol
[Embed(source='assets/DirtPileLogo.swf', symbol = "MyExportedSymbol")]
private var SwfSymbol:Class;
//Make sure that MyExportedSymbol base class is MovieClip
private var movie:MovieClip = new SwfSymbol;
Basically, you mark your symbol for export, give it a name and use that in the embed code. You will embed that symbol only.
You are incorrectly setting a mimeType on your embed. Remove the mimeType and it should work properly. See the docs on embedding assets for more information.
I believe the solution you are looking for is to use the Loader class.
[Embed (source = "assets/DirtPileLogo.swf", mimeType = "application/octet-stream")]
private var content:Class;
private var loader:Loader;
public function Main():void
{
var data:ByteArray = new content();
loader = new Loader();
addChild( loader );
loader.loadBytes( data, new LoaderContext(false, new ApplicationDomain() ) );
// ... add listener to loader if necessary, etc...
}