Cannot set URL after the fact on Ti.Media.createSound() - titanium

I can successfully play a remote mp3 via the Titanium.Media.Sound object, but there is a problem. The 1st clip always plays fine. But each time I try to play a different clip, the first clip plays again. This is 100% repeatable.
So at the top of my js file I'm doing this:
var soundPlayer = Ti.Media.createSound({allowBackground: false});
Then inside the event listener for a button click, I'm setting the URL thusly:
soundPlayer.setUrl(mp3URL);
And I have even tried this approach:
soundPlayer.url = mp3URL;
But they both have the same flaw: the audio is always still the first clip, never any clip I set afterward. I have verified via console logging that in fact a new url was handed to the sound player, and in fact the new url points at a valid mp3. It seems that once I set a url for the sound player, it is set in stone and cannot be changed--just replayed.
So then I tried a completely different approach. Inside my button click listener, I re-create the Sound object each time and hand in the new URL:
soundPlayer = Ti.Media.createSound({allowBackground: false, url: mp3URL});
This actually DOES work and plays the new url each time, BUT it has a terrible side effect: any attached event listeners no longer fire. I need to track state changes in the player, for example when it completes, etc. But these event listeners no longer fire when I take this approach.
Strangely, the AudioPlayer object behaves correctly in all respects, allows me to set new URLs, etc. BUT the problem with that is that AudioPlayer absolutely refuses to play any mp3 file smaller than 40Kb. I have posted questions around that issue on this and other forums, and based on responses and experimentation, I have lots of evidence for the 40Kb limitation, and no evidence to the contrary.
So why can't I change the url after object creation on the Sound object? Thanks in advance for any ideas.

I'd put this in the "Known bug" category, see here: https://jira.appcelerator.org/browse/TIMOB-3348 marked as "won't fix".
Bug report:
var sound = Titanium.Media.createSound();
sound.url='../cricket.wav'; sound.play();
But if I try to change the url after that, sound plays still the cricket.wav
sound.
Response from Appcelerator(?):
"Duplicate of customer issue TIMOB-1488. This can be easily achieved
by creating a new sound object. Furthermore, this would create a
platform parity issue and would complicate any future API that, for
example, would support sound channels, etc. A sound object should be
considered an immutable object once it's constructed."
So create a new sound object and re-add the event listeners is the solution I think:
var sampleListener = function(){
alert('something happened');
}
var sound = Ti.Media.createSound({url:myURL});
sound.addEventListener('complete', sampleListener);
sound.play();
//later on, make a new sound object:
sound = Ti.Media.createSound({url:anotherURL});
sound.addEventListener('complete', sampleListener);
sound.addEventListener('someevent', anotherSampleListener);
sound.play();

Just so know, this is happening to the Ti.Media.AudioPlayer also. The proposed solution will work just fine in that case as well.

Related

SoundCloud Widget API setVolume Method broken?

I'm successfully using Soundcloud's widget API, except the setVolume() method is broken :(
First, I love the widget, but I'm blown away that after all that amazing work there is no volume control available! People will immediately "back button" out of my site when they hear 100% audio and I can't alter it using setVolume()... I have to get it working or pull it :(
Here is what is happening, I have an instance named "widget" (which loads and plays well on the page).
widget.bind(SC.Widget.Events.READY, function() {
$('#audioIndictments').removeClass('remove'); // This disables a CSS rule, making the soundCloud iframe visible
widget.setVolume(10); // This should set the volume to 10% but doesn't :(
widget.getVolume(function(vol){
console.log(vol); // This outputs 0.1 in the console :/
console.log(widget); // This outputs the object in the console and I don't see anything out of whack :|
});
widget.play(); // This successfully fires up the widget and the audio begins playing :)
widget.setVolume(10); // Just to see if it makes a difference, after the play() method, I ran setVolume again after the play() method and still no change :(
widget.getVolume(function(vol){
console.log(vol); // This still outputs 0.1 in the console :/
});
});
Strange results. I found another blogger who asked a similar question and got no satisfactory answer. What's the deal here? What am I not seeing?
Thank you for taking the time :)
Try using the a volume between 0 and 1. This fixed it for me.
0.25 = 25% volume
I too am facing the same issue and I discovered that the volume does not reset to full when setVolume method is called outside of the READY event. This is why the setVolume button on the SC api playground works since it's called externally. But there is another problem - when the next track in the playlist is loaded into the widget, it resets the volume back to full and as a result, deafens the user.
I've used a hacky workaround until this is fixed.
Setup a new PLAY_PROGRESS event and call the method inside there.
widget.bind(SC.Widget.Events.PLAY_PROGRESS, function() {
widget.setVolume(10);
});
The setVolume method will continuously be called when the track is played. Not ideal but it works.
If you have a slider in place for volume then you may use this instead:
widget.bind(SC.Widget.Events.PLAY_PROGRESS, function() {
var vol = jQuery('.slider').val();
widget.setVolume(vol);
});

Safari extension options pages with access to background page

I'm developing a cross-platform browser extension, and have based all my code on the Chrome-way of doing this. I have counted on that the background page will be accessible from the options page, which in Safari extensions turns out to be not possible (since there is no such thing as an options-page). You can only access safari.extension.globalPage.contentWindow from within the extension popup, and the background page itself.
Now, I have an options page, which is an html-page within the extension bundle, and so far I haven't found a way for Safari to give it extension "rights". The closest I have come is adding a content script that's only added on the options page. This seems a bit silly, since the html page itself is in the extension bundle?!
Others have suggested using asynchronous ping-pong style message event handlers, and even the canLoad-mechanism (which is "only" able to run in a beforeload-event). I have been able to hack the canLoad-mechanism for synchronous messaging by forging the BeforeLoadEvent:
// Content script (run from anywhere)
var result = safari.self.tab.canLoad(new BeforeLoadEvent, "data")
-> "return value"
// Background page
safari.application.addEventListener('message', function(e) {
if ( e.name === "canLoad" )
e.message = "return value";
}, true);
It's a hack, but it works. However, I am crippled by the message transport serialization, since I need to be able access methods and data on my objects from the background page. Is there anyway around this?
Possible ways that might work but I don't know if possible:
Access options-page window-object from backgrounds page. Is that possible?
Message passing, need to bypass message serialization
Any shared/global object that I can attach objects to and fetch from the options page?
Make Safari run the options.html page from outside the content-script sandbox? It works in Chrome since they are both within the extension-bundle. It's quite annoying that safari doesn't do this too.
Run the options-page from within the popup. This is promising, but it crashes safari (which is very promising!). However, from the looks of it it's just something to do with a CSS animation in my options.html page. The biggest issue is that it has to be able to open an OAuth2 popup, but thanks to being able to programmatically open the popover, it might be a non-issue. However, this option is the most realistic, but I would rather have it open in a new tab.
Any suggestions and hackish workarounds would really help.

Kaltura - Force player to stop with API only?

Is there any way to force a Kaltura videoplayer to stop ONLY using code and the Kaltura API?
Currently I have solved it by adding a Access Control Profile named "Free preview" under Settings > Access Control in KMC and then added this profile to the Entries I've choosen.
I then add the session to the players flashvars to restrict non-members to only watch the preview, not the whole clip.
But I would like to restrict ALL, or even better selected Categories of clips by using only code, so I don't need to involve KMC.
Is that possible?
Alt) Can you create a new player in KMC and restrict it to viewing only X seconds, no matter what length of Entry? Then I can do the check if user is valid or not and get the category via API and show it in the "preview-player" och the "default player".
If I use the mediaProxy.mediaPlayTo attribute the clip stops, but is easily started again by presing play.
Would greatly appreciate an answer
I got this answer from a guy named oferc in a different forum:
You can listen to the head move event and pause the clip it goes beyond a certain time (then if someone pressed play, you can stop it again)
function jsCallbackReady(player_id) {
my_kdp = $("#"+player_id).get(0); // document.getElementById(player_id) if you do not use jquery/ prefer pure js
my_kdp.addJsListener("kdpReady", "kdpReady"); // when you load the player with an entry (and the player is ready to begin playing it using doPlay for instance)
}
function kdpReady() {
my_kdp.addJsListener("playerUpdatePlayhead","headMove");
}
function headMove(position) {
if (position > "30") { // Your Time, example 30 seconds
my_kdp.sendNotification('doStop')
}
}
Works like a charm!
fredrik_w - neither of the ways you chosen here are a good option to restrict access.
in both cases, your videos are made public, and can be easily accessible by anyone.
The best way to limit access to a video is by defining an Access Control, and like everything in Kaltura, you can define an ACL using API as well.
Check this out as a reference sample-
http://blog.kaltura.org/turning-profit-online-video-made-easy-using-paypal-html5-digital-goods

Objective-c -> class method to main file query

Sorry to bug twice so quickly, but since people were so kind in their informative responces, I figured it couldnt hurt to ask another question.
The same program i tried to make it rather swanky and have a main screen which allows you to click on a button which leads to a limited options screen. This lets you switch the music on or off. Or at least it should do.
The music running code is in the main file (game.m), under the following:
//Music
[Settings setMusicEnabled:YES];
music = [SPSound soundWithContentsOfFile:#"music.caf"];
channel = [[music createChannel] retain];
channel.loop = YES;
channel.volume = 0.25;
if([Settings musicEnabled]){
[channel play];
}
I apologize for the strange format, but it is Sparrow framework. basically, the Settings file contains the class methods I am trying to use. If the methods cause YES, the music is on. If it is No, then the music is off.
settings.m
static BOOL isMusicEnabled;
#implementation Settings
+ (BOOL)musicEnabled
{
return isMusicEnabled;
}
+ (void)setMusicEnabled:(BOOL)value
{
isMusicEnabled = value;
NSLog(#"SME? %i", isMusicEnabled);
}
#end
Now, the options file is working and i tested that section. The program is reading that isMusicEnabled is getting a new value, thus musicEnabled is being altered as well, so there should be a change and the music should be switched off.
However, nothing happens. I have tried to use debugger, but I am not very good at it and I dont understand a lot of the information i am given. I do understand that the problem is sending the message from Settings file to the main/Game file.
I would appriciate anyone's help who could enlighten me as to how this could be solved.
I'm not familiar with Sparrow Framework, but let me make a guess anyway.
[channel play]; starts playing the music in background until the channel is asked to stop playing.
Changing the isMusicEnabled does not trigger any code to stop the currently playing music. When you change the value in Settings, you should inform the channel to stop (most probably by somehow accessing the channel and calling [channel stop].
There's another problem - isMusicEnabled is just a variable in memory, your program will not remember its state between restarts. And Settings are usually supposed to be remembered.
To summarize I see two problems: persisting settings between restarts first and informing about change of settings second. To remember settings I suggest you look into NSUserDefaults class. To inform the channel to stop playing you have couple of options - depending on you skills. Easiest is to simply access the channel variable from within the setMusicEnabled and call stop. Another option would be to use notifications, but for a beginner programmer that is more complicated (look for NSNotificationCenter if interested).

Prevent FLV Buffering (AS FLVPlayback component)

I have a client who is insisting on having a 65Mb FLV on her home page. I talked her down off of the auto-play ledge but I am concerned about wasting bandwidth on a movie that one in 30 visitors might play.
In looking at the docs for the FLVPlayback component here the only buffer related parameter I found was
bufferTime
Number of seconds to buffer before beginning playback. The default value is 0.
The only solution I have come up with is to load a jpeg still of the player and replace it onclick with the SWF.
Anyone have a cleaner solution to prevent wasted bandwidth?
Thanks.
I (literally) just sorted out the same issue (was looking for easy answers when I found your question)
My solution was to use a bit of Actionscript to not give the name of the flv file to the FlvPlayback until the first time the play button was clicked. Snippet of Actionscript 3 code below ( myVideo is the instance name I used for the FlvPlayback component and BigButton is the name of the Play button I display).
var firstplay=true;
var moviefile = "nameofmovie";
function playMovie(event:MouseEvent):void {
if (firstplay) {
myVideo.source = moviefile +".flv";
firstplay = false;
}
myVideo.play();
}
BigButton.addEventListener(MouseEvent.CLICK, playMovie);