how to use method changeToMediaAtIndex tvmlkit js - tvos

how to use the method changeToMediaAtIndex in the tvmlkit js to change video in the playlist?
When i use this method with the player class (player.changeToMediaAtIndex(2)) nothing happened...

I was having issues with this too. My code looked like this.
var player = new Player();
player.playlist = new Playlist();
this.currentMediaItemIndex = 0;
for (var i = 0; i < videoItemsList.length; ++i) {
var video = new MediaItem('video', videoItemsList[i].videoUrl.valueOf());
video.artworkImageURL = videoItemsList[i].artworkUrl;
video.title = videoItemsList[i].title;
video.resumeTime = videoItemsList[i].watchedSeconds;
if (videoItemList[i].videoId == this.startingVideoId) {
this.currentMediaItemIndex = i;
}
player.playlist.push(video);
}
player.play();
player.changeToMediaAtIndex(this.currentMediaItemIndex);
I would call play and immediately call to change the media item. I tried different ordering (calling play after changing the item) and I noticed that when I stepped through the code, it would work as intended. Note that this.startingVideoId is set on the controller by an action before calling the play function that contains the above code. videoItemsList is also defined above and has Object's with the video information.
I decided to add an event listener and wait until I received an event from the player before trying to change the currentItem. I removed player.changeToMediaAtIndex(this.currentMediaItemIndex); from the play function and moved it into the event listener.
I added the event listener into the play function when the player is created like so.
player.addEventListener("stateDidChange", this.stateDidChange);
Then I implemented the event listener to check the playlist and see if it is playing the right video when it goes into a playing state.
private stateDidChange = (event) => {
if (event.state == "playing") {
var player = <Player>event.target;
for (var i = 0; i < player.playlist.length; ++i) {
if (player.playlist.item(i) == player.currentMediaItem) {
if (i != this.currentMediaItemIndex) {
player.changeToMediaAtIndex(this.currentMediaItemIndex);
}
break; // break out of the loop since we don't care about the rest.
}
}
}
};
This works great now and always starts from the right location. You will need additional state tracking with event listeners, like changing the currentMediaItemIndex when the track changes, but this should get you on the right track I hope.
There still isn't a lot of good tvos answers on here yet. Hopefully this will help.
Happy programming.

Related

Flash auto play movieclip

I have an instance of a movieclip Video_Flow called flow. I'm trying to make it to play only when you press a button, but for some reason the audio starts playing every time I run the program. This is my code:
var flow:Video_Flow = new Video_Flow();
PlayButton.addEventListener(MouseEvent.CLICK, PlayVideo);
function PlayVideo(event:MouseEvent)
{
addChild(flow);
flow.x = 0;
flow.y = 50;
}
Because in AS3 objects can exist and operate even if they are not added to display list. As soon as you instantiate your Video_Flow it starts playing video. Adding it to display list only makes you able to see it.
var flow:Video_Flow;
PlayButton.addEventListener(MouseEvent.CLICK, playVideo);
function playVideo(e:MouseEvent):void
{
flow = new Video_Flow;
flow.x = 0;
flow.y = 50;
addChild(flow);
}

Creating a vector array of movie clips AS3

I have several movie clips on the stage of my main .fla named btn1-btn7 which will act as buttons. I have a class file named Functions.as where an event listener is created when a button is clicked. onButtonClicked is just going to a frame on the timeline.
obj.addEventListener(MouseEvent.CLICK, onButtonClicked);
I would like the ability to set the buttonMode, visibility, etc. of all of the buttons simultaneously. I have been looking into this for a few hours and am not able to find any solutions. I am now looking into adding them to a vector (which is a new concept for me), but I am not sure how to go about executing this properly. This is what I have so far.
public var buttons:Vector.<MovieClip > = new Vector.<MovieClip > ();
function addButtons()
{
buttons.push(btn1,btn2,btn3,btn4,btn5,btn6,btn7);
for (var i:int; i<buttons.length; i++)
{
trace(buttons[i].name);
}
}
How would I go about, for example, adding the event listener to all of the objects? I will also be setting the buttonMode to true, and making them all invisible simultaneously. I don't even know if it's possible to accomplish this. Thank you in advance for any suggestions.
I'm going to asume that you use timeline code, and have instances of the buttons already placed on the stage. So, first, create the vector:
var _btns:Vector.<MovieClip> = new Vector.<MovieClip>;
_btns.push(btn1,btn2,btn43....) //add all the buttons
Than, you can init the properties of all the buttons:
var _mc:MovieClip;//helper var
for(var i:int=0,i<_btns.length;i++)
{
_mc = _btns[i];
_mc.visible = false;
_mc.buttonMode = true;
_mc.addEventListener(MouseEvent.CLICK, onClick);
}
Then, the event handler:
function onClick(e:MouseEvent):void
{
for(var i:int=0,i<_btns.length;i++)//reset all the buttons
{
_btns[i].visible = false;
}
_mc = MovieClip(e.eventTarget);
_mc.visible = true; //make visible the clicked one
}
You just need to do what you are doing with the .name property in your example code. You need to loop thru every single button in your array (or vector, if you prefer). Here is an example how to set the property of buttonMode:
function setButtonMode(b:Boolean):void {
for(var i:int=0; i<buttons.length; i++) {
var btn:MovieClip = buttons[i]; //store the current reference in a var for faster access
btn.buttonMode = b;
btn.mouseChildren = !b;
}
}

How do I loop and stop a sound using the same button in AS3?

I have 4 buttons on stage and I need a code that enables me to click on one of them, loop a specific sound "infinitely", and when it is clicked again, the sound stops. The sound is not initially playing. Also, while one of the sounds is looping, if I press another button, I'd like the previous sound to stop and the new one to play.
To help visualize this more, I will explain my project. I have to create an 'app' that is like an online guitar tuner. This feature is sort of what I would like to recreate:
http://www.gieson.com/Library/projects/utilities/tuner/
I don't even know where to begin with the coding... any help is greatly appreciated. Thank you!
The actual code is quite dependent on how you are loading in the sound, so I will write the "skeleton" for the code.
var currentSound:Sound = null;
var currentSoundChannel:SoundChannel;
var sound1:Sound = /* Load me */
var sound2:Sound = /* Load me */
button1.addEventListener(MouseEvent.CLICK, playSound1);
function playSound1(event:MouseEvent)
{
playSound(sound1);
}
button2.addEventListener(MouseEvent.CLICK, playSound2);
function playSound2(event:MouseEvent)
{
playSound(sound2);
}
function playSound(sound:Sound):void
{
if (currentSound != null)
{
// Stop the current music
currentSoundChannel.stop();
}
if (currentSound == sound)
{
// Stop playing ANY sound
currentSound = null;
currentSoundChannel = null;
}
else
{
// Play a different sound
currentSound = sound;
currentSoundChannel = sound.play();
}
}

action script to add a pause button on a few audio buttons

I am currently using the following code to play audio recordings from the project library.
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.MouseEvent;
var sound1:Sound = new audio1();
var sound2:Sound = new audio2();
var mySoundChannel:SoundChannel = new SoundChannel();
function stopSound():void
{
//This stops all sound in the sound channel.
//If there is nothing playing, nothing happens.
mySoundChannel.stop();
}
//In this function, we create an argument that allows us to tell the function
//what sound to we want it to play.
function playSound(soundname:String):void
{
try
{
mySoundChannel = this[soundname].play(0, 0);
}
catch(error:ReferenceError)
{
trace("playSound: That sound name doesn't exist.");
return;
}
}
//Hook up buttons-->
button1.buttonMode = true;
button1.useHandCursor = true;
button2.buttonMode = true;
button2.useHandCursor = true;
button1.addEventListener(MouseEvent.CLICK, button1click);
button2.addEventListener(MouseEvent.CLICK, button2click);
function button1click(evt:Event):void
{
stopSound();
playSound("sound1");
}
function button2click(evt:Event):void
{
stopSound();
playSound("sound2");
}
I need to pause the currently playing audio when a button is clicked. How do I do this?
You will need to do five things to your current code in order to pause and resume the currently playing sound:
Create a variable that stores the name of the currently playing
sound, and a variable to store the pause position of the audio.
Create a pause function.
Create a resume function.
Expand the current playSound and stopSound functions to work with the new variables.
Hook up the button event listener.
Step 1:
var currentSound:String = "";
var pausePosition:Number = 0;
Step 2: We're going to save the current position of the audio in that second variable we just created. We can get the current play position of the audio using the mySoundChannel.position property, which returns a Number value (matching the Number type we gave the pausePosition variable).
function pauseSound():void
{
//If there's a song to pause...
if(currentSound != "")
{
//Get pause position.
pausePosition = mySoundChannel.position;
//Stop the sound directly.
mySoundChannel.stop();
}
}
Note we didn't call stopSound(). There's a good reason for that. We're going to put an extra line of code in that function shortly that we don't want to use in pauseSound().
Step 3: Now we create the function to resume audio. Note this is NOT the same as playSound(). We're telling it to start playing from pausePosition, not from 0 (the beginning of the sound clip).
function resumeSound():void
{
//If there's a song to resume...
if(currentSound != "")
{
//Start playing the current audio from the position we left off at.
mySoundChannel = this[currentSound].play(pausePosition);
}
}
Step 4: Since we're now working with those variables we declared in step 1, we need to adjust how playSound() and stopSound() work.
In playSound(), instead of just passing soundname to the sound channel, we're going to save the soundname to currentSound.
function playSound(soundname:String):void
{
try
{
currentSound = soundname
mySoundChannel = this[currentSound].play(0, 0);
}
catch(error:ReferenceError)
{
trace("playSound: That sound name doesn't exist.");
return;
}
}
In stopSound(), we need to actually clear the currentSound and pausePosition variables when we stop, to ensure that resumeSound doesn't start audio after we've totally stopped it.
function stopSound():void
{
//This stops all sound in the sound channel.
//If there is nothing playing, nothing happens.
mySoundChannel.stop();
//Clear our variables.
currentSound = "";
pausePosition = 0;
}
GOTCHA WARNING: Ordinarily, you can loop audio by passing an integer other than 0 in the second argument (where I have the 5) in the following code:
mySoundChannel = this[currentSound].play(0, 5);
In the above code, the sound would play from the beginning, and repeat five times.
However, if you are starting the audio from any position other than 0, what will actually happen is that the audio will loop at the position you start at, not at the beginning.
That is to say, if you use this code:
mySoundChannel = this[currentSound].play(1000, 5);
The sound will loop five times, but every time the sound starts over in the loop, it will start playing from position 1000, and NOT the beginning of the sound (0).
I hope that answers your question!

AS3 play sounds in an array in a sequence

This is being programmed in Flash CS5.5:
I want to push a button, and play through the entire array one sound at a time. When the first sound stops, the second begins, etc. all the way until the last sound plays. When the last sound finishes, all sound should stop, and if you push the play button again, it should start over at the beginning, and play through all sounds again.
Currently, to advance to the next sound, you have to push the button again. I'm thinking the SOUND_COMPLETE needs to be used... I'm just not sure how, hence the empty function. I only want to have to push play one time to hear the entire array in a sequence. Any ideas?
var count;
var songList:Array = new Array("test1.mp3","test2.mp3","test3.mp3");
count = songList.length;
myTI.text = count;
var currentSongId:Number = 0;
playBtn.addEventListener(MouseEvent.CLICK, playSound);
function playSound(e:MouseEvent):void{
if(currentSongId < songList.length)
{
var mySoundURL:URLRequest = new URLRequest(songList[currentSongId]);
var mySound:Sound = new Sound();
mySound.load(mySoundURL);
var mySoundChannel:SoundChannel = new SoundChannel();
mySoundChannel = mySound.play();
currentSongId++;
mySoundChannel.addEventListener(Event.SOUND_COMPLETE,handleSoundComplete)
}
if(currentSongId == songList.length)
{
currentSongId = 0;
}
}
function handleSoundComplete(event:Event){
}
You should use functions to modulate what you do, this will make your code more readable.
private Array songList = new Array("test1.mp3", "test2.mp3");
public function onPlayBtnPressed(){
currentSongIndex = 0;
PlaySongFromIndex(currentSongIndex);
}
public function PlaySongFromIndex(songIndex:int){
//do what ever here to simply play a song.
var song:Sound = new Sound(songList[songIndex]).Play()
//Addevent listener so you know when the song is complete
song.addEventListener(Event.Complete, songFinished);
currentSongIndex++;
}
public function songFinished(e:Event){
//check if all the songs where played, if so resets the song index back to the start.
if(currentSongIndex < listSong.Length){
PlaySongFromIndex(currentSongIndex);
} else {
currentSongIndex=0;
}
}
This wont compile its just to show an exemple, hope this helps.