Download an MP3 from server and play multiple times (loop). Multiple downloads? - android

I have a simple button that plays a small MP3 file, looping it 30 times. The MP3 is streamed from a server (urlMP3).
I can see on my Galaxy S2 that it accesses the server for each of the 30 loops. Is it downloading the MP3 each time it loops or downloading once and playing from the phone's memory?
//Button 'audioYes' to play audio loop x 30
var soundLoop:Sound = new Sound();
var soundChannel:SoundChannel;
var soundLoopUrl:URLRequest = new URLRequest(urlMP3);
audioYes.addEventListener(MouseEvent.CLICK, f2_MouseClickHandler);
function f2_MouseClickHandler(event:MouseEvent):void {
soundLoop.load(soundLoopUrl);
soundLoop.play(0, 30);
}
If it is downloading each time, what would be a good way to download it once and then play? Thanks for your help.
Edit: Sep 1 2012
I've created a simple flash file and added the following provided by #Rytis. I'm getting an error from the last line, this.mySound.play "Error #1009: Cannot access a property or method of a null object reference." What do I do with that?
import flash.media.Sound;
import flash.net.URLLoader;
import flash.display.Loader;
var mySound:Sound = new Sound();
var myurlLoader:URLLoader = new URLLoader();
myurlLoader.addEventListener(Event.COMPLETE, this.onSoundLoadComplete)
myurlLoader.load(new URLRequest("01.mp3"))
function onSoundLoadComplete(event:Event):void{
this.mySound = URLLoader(event.target).data as Sound;
this.mySound.play(0,30);
}

Short answer - download sound and store it to variable before playing it.
Example:
package {
import flash.events.Event;
import flash.media.Sound;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class SoundLoadTest {
protected var sound : Sound;
public function SoundLoadTest () {
var urlLoader : URLLoader = new URLLoader();
urlLoader.addEventListener( Event.COMPLETE, this.onSoundLoadComplete )
urlLoader.load( new URLRequest( "path/to/sound.file" ) )
}
protected function onSoundLoadComplete ( event : Event) : void {
// save loaded sound to a class field
this.sound = URLLoader( event.target ).data as Sound;
// start playing sound
this.sound.play( 0, 30 );
}
}
}

Related

How to playback an .mp3 file in a Flutter app?

I have written a Dart web app that retrieves .mp3 files from a server and plays them back; I am trying to write a mobile version using Flutter. I know dart:web_audio is the main option for a web app, but Flutter can't find it in my SDK. I know it's there because I can compile the following to Javascript:
import 'dart:html';
import 'dart:convert';
import 'dart:web_audio';
AudioContext audioContext;
main() async {
audioContext = new AudioContext();
var ul = (querySelector('#songs') as UListElement);
var signal = await HttpRequest.getString('http://10.0.0.6:8000/api/filelist');
// Map json = JSON.decode(signal);
// for (Map file in json['songs']) {
print("signal: $signal");
Map json = JSON.decode(signal);
for (Map file in json['songs']) {
var li = new LIElement()
..appendText(file['title']);
var button = new ButtonElement();
button.setAttribute("id", "#${file['file']}");
button.appendText("Play");
li.append(button);
new Song(button, file['file']);
ul.append(li);
}
}
class Song {
ButtonElement button;
bool _playing = false;
// AudioContext _audioContext;
AudioBufferSourceNode _source;
String title;
Song(this.button, this.title) {
button..onClick.listen((e) => _toggle());
}
_toggle() {
_playing = !_playing;
_playing ? _start() : _stop();
}
_start() {
return HttpRequest
.request("http://10.0.0.6:8000/music/$title", responseType: "arraybuffer")
.then((HttpRequest httpRequest) {
return audioContext
.decodeAudioData(httpRequest.response)
.then((AudioBuffer buffer) {
_source = audioContext.createBufferSource();
_source.buffer = buffer;
_source.connectNode(audioContext.destination);
_source.start(0);
button.text = "Stop";
_source.onEnded.listen((e){
_playing = false;
button.text = "Play";
});
});
});
}
_stop() {
_source.stop(0);
button.text = "Play";
}
}
How would I rewrite the dart:web_audio parts of my code for a Flutter app? Can Flutter access MediaPlayer? And if so, how would I refer to it in pubspec.yaml?
As raju-bitter noted above, Flutter used to provide some built-in audio wrappers in its core engine but those have since been removed: https://github.com/flutter/flutter/issues/1364.
Flutter-using Apps are just iOS or Android apps, and thus it is possible to do anything the underlying iOS/Android can do via Flutter using some Java or Obj-C code in the hello_services model (https://github.com/flutter/flutter/tree/master/examples/hello_services). This model is documented at https://flutter.io/platform-services. It's not nearly as easy as we'd like it to be yet. Many improvements to come soon.
I know its 4 years late, but i have found audioplayers package which can be used as the following
import 'package:audioplayers/audio_cache.dart';
import 'package:audioplayers/audioplayers.dart';
//Call this function from an event
void playRemoteFile() {
AudioPlayer player = new AudioPlayer();
player.play("https://luan.xyz/files/audio/ambient_c_motion.mp3");
}

Play a youtube video inside an AIR app (AS3)

I'd like to know if it's possible to play a youtube video inside an AIR app using AS3 code.
It seems that youtube API is deprecated...
If anybody knows a way or a good tutorial.
Thx
----------------EDIT
I've tried this code :
var wt:uint;
var ht:uint ;
var webview:StageWebView ;
var rect:Rectangle;
var url:String;
url = "https://www.youtube.com/watch?v=hFupHx5G44s";
trace(url);
wt = this.stage.stageWidth;
ht = this.stage.stageHeight;
webview = new StageWebView();
rect = new Rectangle(0,300,wt,ht/2);
webview.stage = this.stage;
webview.viewPort = rect;
webview.loadURL(url);
But it's loading the all page of youtube (youtube video + the recommended videos..) with a scroller on the right.
I'd like to load only the video (not all the youtube page).
Edit :
I'd like to load only the video (not all the youtube page).
Use this link format :
url = "https://www.youtube.com/v/hFupHx5G44s";
for example, choose a format like below :
https://www.youtube.com/v/hFupHx5G44s - use the /v/ to get a SWF version
https://www.youtube.com/embed/hFupHx5G44s - use the /embed/ to get a HTML5 version
It's for an ANDROID AIR app.
You should have mentioned that detail in the question. You could try adding
Security.allowDomain("www.youtube.com");
Security.loadPolicyFile("https://www.youtube.com/crossdomain.xml");
//////////# End of Edit
Why not use stageWebView to load the embed (HTML5) player as web page?
The YouTube AS3 API being deprecated likely means you cannot create your own user interface or use features like search. You can re-create the search function easily though (load "search results" source into String and extract links by parsing the source code).
Anyways, I just tried this code below and it worked for an AIR Desktop App.Maybe you can use it as a starting point...
package
{
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.events.*;
import flash.system.*;
public class Youtube_AIR_code extends MovieClip
{
public var YT_Loader : Loader;
public var my_VIDEO_ID : String = "hFupHx5G44s"; //# the YouTube ID
public function Youtube_AIR_code ()
{
Security.loadPolicyFile("http://www.youtube.com/crossdomain.xml");
YT_Loader = new Loader();
YT_Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, YT_Ready );
YT_Loader.load( new URLRequest("http://www.youtube.com/v/" + my_VIDEO_ID) );
}
function YT_Ready (e:Event) : void
{
trace("YouTube SWF :: [status] :: Loading Completed");
//addChild(YT_Loader);
//# Wait for Youtube Player to initialise
YT_Loader.content.addEventListener("onReady", on_YT_PlayerLoaded );
}
private function on_YT_PlayerLoaded (e:Event) : void
{
//# Check Original Width/Height - Scale it proportionally
trace( "YT_Loader content Width : " + YT_Loader.content.width );
trace( "YT_Loader content Height : " + YT_Loader.content.height );
//# Testing changed size and position
YT_Loader.width = 320; YT_Loader.height = 240;
YT_Loader.x = 30; YT_Loader.y = 100;
addChild(YT_Loader); //# can add to stage only
}
} //# End Public Class
} //# End Package
Have you seen the Video Player ANE from My Flash Labs?
http://www.myflashlabs.com/product/video-player-ane-adobe-air-native-extension/

Playing AAC streams with Adobe AIR in Android/iOS

Looks like it's a pretty common trouble but I still can't see any solution.
The goal is to play some AAC stream in Adobe AIR mobile app.
I've used a transcoder from https://code.google.com/p/project-thunder-snow/. The one adds FLV headers to AAC data so the stream can be played through a standard AS3 NetStream object (as I understand). It works fine for Win and Mac, but the same app launched on Android or iOS produces neither sound nor any error. I've figured out that transcoder works fine, the cause is in the different
characteristics of NetStreams.
So, is there any solution or, at least, any documentation describing the difference between the NetStreams on PC and mobile platform?
okay... I might have a clue for you. I tried running the NBAAC code link you posted on the other question from inside Flash CS5 and got this error:
onChannelReady : MetaData
Error: Error #2067: The ExternalInterface is not available in this container. ExternalInterface requires Internet Explorer ActiveX, Firefox, Mozilla 1.7.5 and greater, or other browsers that support NPRuntime.
So it seems the player is designed to be used only inside an html page & uses ExternalInterface (Javascript) for getting metadata from browser into SWF
I removed the ExternalInterface stuff and it played ok in Flash Player & Device Central without browser/HTML. Try this code in your AIR app (I don't have AIR installed anymore to confirm..)
package
{
import com.thebitstream.flv.CodecFactory;
import com.thebitstream.flv.Transcoder;
import com.thebitstream.flv.codec.*;
import com.thebitstream.ice.*;
import com.thebitstream.nsv.*;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
//import flash.external.externalInterface;
import flash.media.SoundTransform;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.NetStreamAppendBytesAction;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLStream;
import flash.utils.ByteArray;
import flash.utils.setTimeout;
[SWF (width="250",height="250")]
public dynamic class NBAAC_AIR_v2 extends Sprite
{
public var request:URLRequest;
public var transcoder :Transcoder
public var transport :NetConnection;
public var transportStream :NetStream;
public var serverConnection:URLStream;
//public var host:String=" "; //now not used in line... request=new URLRequest(resource);
public var resource:String="http://aacplus-ac-64.timlradio.co.uk/;"; //test station
public function NBAAC_AIR_v2 ()
{
super();
/*
if(loaderInfo.parameters.host)
host=loaderInfo.parameters.host;
if(loaderInfo.parameters.resource)
resource=loaderInfo.parameters.resource;
*/
//CodecFactory.ImportCodec(MetaData);
CodecFactory.ImportCodec(AAC);
CodecFactory.ImportCodec(AACP);
transcoder = new Transcoder();
transcoder.addEventListener(CodecEvent.STREAM_READY,onChannelReady);
transcoder.addEventListener(StreamDataEvent.DATA, onTag);
transcoder.initiate();
transcoder.loadCodec("AAC");
transport = new NetConnection();
transport.connect(null);
flash.utils.setTimeout(boot,500);
}
public function boot():void
{
//externalInterface.addCallback('nbaac.setVolume', onVolume);
//externalInterface.addCallback('nbaac.togglePause', onTogglePause);
//externalInterface.addCallback('nbaac.setBuffer', setBufferLength);
//externalInterface.addCallback('nbaac.getBuffer', getBufferLength);
//externalInterface.addCallback('nbaac.getTime', getTime);
/*
var meta:Object={};
meta.uri="";
meta.StreamTitle="";
*/
//externalInterface.call('logit','start up');
transportStream = new NetStream(transport);
transportStream.bufferTime=2;
transportStream.client = this;
transportStream.soundTransform=new SoundTransform(.5,0);
transportStream.play(null);
transportStream.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
var headerTag:ByteArray=transcoder.createHeader(false,true);
transportStream.appendBytes(headerTag);
headerTag.clear();
//transcoder.readMetaObject(meta,0);
serverConnection=new URLStream();
serverConnection.addEventListener(ProgressEvent.PROGRESS,loaded);
serverConnection.addEventListener(IOErrorEvent.IO_ERROR, onIo);
serverConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onNoPolicy);
serverConnection.addEventListener(Event.CLOSE, onClose);
request=new URLRequest(resource); //removed "host" from url
//request.requestHeaders=[new URLRequestHeader("GET",resource+" HTTP/1.0")];
//request.requestHeaders=[new URLRequestHeader("Icy-MetaData","1")];
request.method=URLRequestMethod.GET;
serverConnection.load(request);
}
private function getTime() :Number
{ return transportStream.time; }
private function getBufferLength() :Number
{ return transportStream.bufferLength; }
private function setBufferLength (val:Number) :void
{ transportStream.bufferTime = val; }
private function onTogglePause():void
{ transportStream.togglePause(); }
private function onVolume (val:Number) :void
{ transportStream.soundTransform = new SoundTransform(val, 0); }
private function onIo(pe:IOErrorEvent) :void
{ /* externalInterface.call('logit','IOErrorEvent') */ }
private function onTag(sde:StreamDataEvent) :void
{
sde.tag.position=0;
transportStream.appendBytes(sde.tag);
}
private function onChannelReady(ce:CodecEvent) :void
{ trace('onChannelReady :',ce.codec.type); }
private function onClose(e:Event):void
{ /* externalInterface.call('logit','onClose') */ }
public function onMetaData(e:Object):void
{ /* externalInterface.call('logit','onMetaData') */ }
private function loaded(e:ProgressEvent):void
{
var chunk:ByteArray=new ByteArray();
while(serverConnection.bytesAvailable)
{ chunk.writeByte( serverConnection.readByte() ); }
chunk.position=0;
transcoder.addRawData( chunk, 0, "AAC" );
}
private function onNoPolicy(se:SecurityErrorEvent):void
{ /* externalInterface.call('logit','SecurityErrorEvent'+host+resource+'<br />'+se.text); */ }
}
}

Simple flash video loop play (played on android) always crash after a few hours

Why is my flash-based video player on android always crash after a few hours of play?
I'm writing a flash-based Android App. The only thing that native android part do using webview to load a flash swf. The swf acted as the container for all module (which all written in flash as3). One of the module is a simple video module which loop play a set of video playlist forever.
I've considered memory leak, but after printing memory usage (using flash's System.totalMemory), the result is always around 12MB to 14MB (which seems normal for two videos). I've test the flash using both webview and other third party swf player for android (such as "Swf Player" and "Smart SWF Player"), all results in crash after a few hours.
The as3 code is simple and I can't see any possible cause for this. Here is my main class:
import flash.display.MovieClip;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.NetStatusEvent;
public class simpleVid extends MovieClip {
private var video:Video;
private var nc:NetConnection;
private var ns:NetStream;
private var uri:Array = new Array("vid1.flv", "vid2.flv");
private var counter:int = 0;
public function simpleVid() {
// constructor code
nc = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
video = new Video();
video.attachNetStream(ns);
ns.client = {onMetaData:videoReady, NetStatusEvent:onStatusEvent};
ns.addEventListener(NetStatusEvent.NET_STATUS, onStatusEvent);
ns.play(uri[counter]);
stage.addChild(video);
counter++;
counter = counter % 2;
}
public function videoReady(item:Object){
video.width = 1280;
video.height = 720;
}
public function onStatusEvent(event:NetStatusEvent):void{
if (event.info.code == "NetStream.Play.Stop") {
ns.play(uri[counter]);
counter++;
counter = counter % 2;
}
}
}
Is there is anything I missed or I did wrong in this code?
Thanks in advance.
The problem "mysteriously" disappeared after I switch to AIR instead of flash.
No code is changed, I only changed the release setting from Flash player to air for android.
Now it can run continuously for several days without problem.

AS3 Android Air, audio stream error

I have a Android Actionscript project that I am trying to incorporate sound in, but having a problem that I cannot figure out. I have the sound file in the same directory as my class file and my .xfl file. I am trying to start the music file as soon as the application starts via class reference, but am getting the same error everytime:
Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error.
at playAudio()[C:\pathtoerror\playAudio.as:9]
at networkScores/frame1()[networkScores::frame1:7]
Here is my class file:
package
{
import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundChannel;
public class playAudio extends Sound
{
public var music:Sound = new Sound(new URLRequest("mathHomeSong.mp3"));
public var sc:SoundChannel;
public var num:int;
public function playAudio(controlNum:int)
{
num = controlNum;
if(num == 1)
{
sc = music.play();
}
else if(num == 2)
{
}
}
}
}
Here is my call from the timeline:
import playAudio;
var playMusic:playAudio = new playAudio(1);
Just figured this out, I had to put a link to the mp3 file in my server, instead of putting it in my directory.
Sorry about the confusion
I posted too soon. I just figured out my question, figures. Ok, so what I did not do is post this file on my server and with the URLRequest(""); I had to put a link to my file on my server, not linki
The answer to this question is that I forgot to add this music file to a server and then link it via hyper link. For example http://mysite.com/audio.file
Sorry about this
thanks

Categories

Resources