Skip to main content
oliversen
Inspiring
January 16, 2018
Answered

1061: Call to a possibly undefined method start through a reference with static type flash.media:SoundChannel

  • January 16, 2018
  • 2 replies
  • 5623 views

I have a problem with function start sound(startstart). I cant compile the program before getting an error.

var channel:SoundChannel = new SoundChannel();

I have 3 similar functions as NRKP1

mcButikker.btnNRKP1.addEventListener(MouseEvent.CLICK, NRKP1); 

function NRKP1(e:MouseEvent){

    
var SoundNRKP1:Sound = new Sound();
     channel
.stop();
     var req:URLRequest = new URLRequest("http://lyd.nrk.no/nrk_radio_p1pluss_mp3_m");
     SoundNRKP1.load(req);
     channel = SoundNRKP1.play();
}

When I added start/stop buttons for starting and stopping I got the error. But when I dont have them, and choose one of the 3 functions, it will stop the sound, and play another sound. "channel.start(); is the issue here. I have tried several ways around this but nothing seems to work.

btnStart.addEventListener(MouseEvent.CLICK, startstart);  

function
startstart(e:MouseEvent){
     channel
.start();
}   

btnStopp
.addEventListener(MouseEvent.CLICK, stopstop);

function stopstop(e:MouseEvent){
     channel
.stop(); 
}
This topic has been closed for replies.
Correct answer JoãoCésar17023019

Here is the complete code for reference:

import flash.events.MouseEvent;

import flash.media.Sound;

import flash.media.SoundChannel;

import flash.media.SoundTransform;

import flash.net.URLRequest;

import flash.display.SimpleButton;

import flash.events.Event;

var bgmList:Vector.<String> = new <String>

[

    "https://website.com/Beat_Your_Competition.mp3",

    "bgm/Bounce_House.mp3",

    "bgm/Chess_Pieces.mp3"

];

var sound:Sound;

var sChannel:SoundChannel;

var sTransform:SoundTransform;

var channelPosition:Number = 0;

var playing:Boolean = false;

var paused:Boolean = false;

var currentSound:int = -1;

function onMouse(e:MouseEvent):void

{

    if (e.type == MouseEvent.CLICK)

    {

        if (e.currentTarget.parent == soundButtons)

            changeSound(e.currentTarget as SimpleButton);

        else if (e.currentTarget.parent == controlButtons)

        {

            if (e.currentTarget == controlButtons.pauseButton)

                pauseSound();          

            else if (e.currentTarget == controlButtons.playButton)

                playSound(channelPosition);

            else if (e.currentTarget == controlButtons.stopButton)

                stopSound();

        }

      

        controlStates();

    }

}

function changeSound(button:SimpleButton):void

{

    currentSound = int(button.name.slice(5, button.name.length));

    channelPosition = 0;

  

    nowPlaying.text = "Current song = " + bgmList[currentSound].split("/").pop();

  

    if (playing)

    {

        sChannel.stop();

        playSound(channelPosition);      

    }          

}

function pauseSound():void

{

    if (playing)

    {

        channelPosition = sChannel.position;

        sChannel.stop();

        playing = false;

        paused = true;

    }  

}

function playSound(start:Number = 0, volume:Number = 1, loops:int = 0):void

{  

    if (currentSound == -1)

        return;

  

    if (sChannel)

        sChannel.stop();

  

    sound = new Sound();  

    sTransform = new SoundTransform();  

    sound.load(new URLRequest(bgmList[currentSound]));

    sChannel = sound.play(start, loops);  

    sTransform.volume = volume;

    sChannel.soundTransform = sTransform;

    playing = true;

    paused = false;

}

function stopSound():void

{

    if (sChannel)

    {

        sChannel.stop();

        channelPosition = 0;

        playing = false;

        paused = false;

    }

}

function controlStates():void

{

    if (playing)

    {

        progress.visible = true;

        bar.visible = true;

        controlButtons.pauseButton.alpha = 1;

        controlButtons.stopButton.alpha = 1;

    }

    else

    {

        controlButtons.pauseButton.alpha = 0.35;

      

        if (!paused)

        {

            progress.visible = false;

            bar.visible = false;

            controlButtons.stopButton.alpha = 0.35;

        }

    }

}

function enterFrameHandler(e:Event):void

{

    if (playing)

    {

        if (sound.length > 0)

            progress.scaleX = sChannel.position / sound.length;

    }      

}

function start():void

{

    stop();

  

    for (var i:int = 0, total:int = soundButtons.numChildren; i < total; i++)

        soundButtons.getChildAt(i).addEventListener(MouseEvent.CLICK, onMouse);

  

    for (i = 0, total = controlButtons.numChildren; i < total; i++)

        controlButtons.getChildAt(i).addEventListener(MouseEvent.CLICK, onMouse);

  

    stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

  

    nowPlaying.text = "Current song = none."

  

    controlStates();

}

start();

2 replies

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
January 17, 2018

Here is the complete code for reference:

import flash.events.MouseEvent;

import flash.media.Sound;

import flash.media.SoundChannel;

import flash.media.SoundTransform;

import flash.net.URLRequest;

import flash.display.SimpleButton;

import flash.events.Event;

var bgmList:Vector.<String> = new <String>

[

    "https://website.com/Beat_Your_Competition.mp3",

    "bgm/Bounce_House.mp3",

    "bgm/Chess_Pieces.mp3"

];

var sound:Sound;

var sChannel:SoundChannel;

var sTransform:SoundTransform;

var channelPosition:Number = 0;

var playing:Boolean = false;

var paused:Boolean = false;

var currentSound:int = -1;

function onMouse(e:MouseEvent):void

{

    if (e.type == MouseEvent.CLICK)

    {

        if (e.currentTarget.parent == soundButtons)

            changeSound(e.currentTarget as SimpleButton);

        else if (e.currentTarget.parent == controlButtons)

        {

            if (e.currentTarget == controlButtons.pauseButton)

                pauseSound();          

            else if (e.currentTarget == controlButtons.playButton)

                playSound(channelPosition);

            else if (e.currentTarget == controlButtons.stopButton)

                stopSound();

        }

      

        controlStates();

    }

}

function changeSound(button:SimpleButton):void

{

    currentSound = int(button.name.slice(5, button.name.length));

    channelPosition = 0;

  

    nowPlaying.text = "Current song = " + bgmList[currentSound].split("/").pop();

  

    if (playing)

    {

        sChannel.stop();

        playSound(channelPosition);      

    }          

}

function pauseSound():void

{

    if (playing)

    {

        channelPosition = sChannel.position;

        sChannel.stop();

        playing = false;

        paused = true;

    }  

}

function playSound(start:Number = 0, volume:Number = 1, loops:int = 0):void

{  

    if (currentSound == -1)

        return;

  

    if (sChannel)

        sChannel.stop();

  

    sound = new Sound();  

    sTransform = new SoundTransform();  

    sound.load(new URLRequest(bgmList[currentSound]));

    sChannel = sound.play(start, loops);  

    sTransform.volume = volume;

    sChannel.soundTransform = sTransform;

    playing = true;

    paused = false;

}

function stopSound():void

{

    if (sChannel)

    {

        sChannel.stop();

        channelPosition = 0;

        playing = false;

        paused = false;

    }

}

function controlStates():void

{

    if (playing)

    {

        progress.visible = true;

        bar.visible = true;

        controlButtons.pauseButton.alpha = 1;

        controlButtons.stopButton.alpha = 1;

    }

    else

    {

        controlButtons.pauseButton.alpha = 0.35;

      

        if (!paused)

        {

            progress.visible = false;

            bar.visible = false;

            controlButtons.stopButton.alpha = 0.35;

        }

    }

}

function enterFrameHandler(e:Event):void

{

    if (playing)

    {

        if (sound.length > 0)

            progress.scaleX = sChannel.position / sound.length;

    }      

}

function start():void

{

    stop();

  

    for (var i:int = 0, total:int = soundButtons.numChildren; i < total; i++)

        soundButtons.getChildAt(i).addEventListener(MouseEvent.CLICK, onMouse);

  

    for (i = 0, total = controlButtons.numChildren; i < total; i++)

        controlButtons.getChildAt(i).addEventListener(MouseEvent.CLICK, onMouse);

  

    stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

  

    nowPlaying.text = "Current song = none."

  

    controlStates();

}

start();

oliversen
oliversenAuthor
Inspiring
January 17, 2018

This is just wonderful!

Thank you so much!

I have now two more questions. Iwould like sound to be played if I choose a Soundbutton, instead of choosing the channel and then pushing play. Would that be possible?

Again, thank you so much!

JoãoCésar17023019
Community Expert
Community Expert
April 15, 2018

Thank you for helping me out, I am now wondering. If I want to get the bgmList from XML or something similar. And instead of creating all the sound buttons, they are made based on the XML file. If the XML file has 15 songs, I want 15 buttons generated. or a list of 15 elements. I am able to get data using JSON parsing and creating an bgmList out of that. But I am unsure how to instead of manually creating each button, either create them by their own, or use a list. I would have to change some code to make it to a list. Ive been trying a few things. But it doesnt seem to work.

var json:URLLoader = new URLLoader();

var parsedJSONData:Object;

   json = new URLLoader();

   json.addEventListener(Event.COMPLETE, parseJSON);

   json.load(new URLRequest("http://blogglista.no/TESTESTDBDBDBDBD/check_media.php"));

   trace("Loading JSON file...");

  

   var bgmList=new Vector.<String>();

function parseJSON(evt:Event):void {

trace("JSON file loaded successfully!");

trace("Parsing JSON...");

trace("RESULTS:");

parsedJSONData = JSON.parse(json.data)

//trace(parsedJSONData.streams[0])

trace(parsedJSONData);

trace(parsedJSONData.streams.length);

var i=0;

trace(bgmList)

for (i=0;i<parsedJSONData.streams.length;i++){

//for each (var data:Object in parsedJSONData.streams){//(i=0;i<parsedJSONData.streams.length;i++){

//list.addItem({label:parsedJSONData.kanalnavn});

bgmList.push(parsedJSONData.streams);

//bgmList.push(data);

}

}


Hi again!

Here is a very simple example for you to adapt to your app. Don't forget to change the songs names.

AS3 code:

import flash.events.Event;

import flash.events.IOErrorEvent;

import flash.net.URLLoader;

import flash.net.URLRequest;

import flash.text.TextFieldAutoSize;

var jsonPath:String = "playlist.json";

var json:Object;

var bgmList:Vector.<String> = new Vector.<String>();

function start():void

{

    var loader:URLLoader = new URLLoader();

    var request:URLRequest = new URLRequest();

    request.url = jsonPath;

    loader.addEventListener(Event.COMPLETE, completeHandler);

    loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);

    loader.load(request);

}

function completeHandler(e:Event):void

{

    var loader:URLLoader = URLLoader(e.target);

    json = JSON.parse(loader.data);

  

    createButtons(json);

}

function errorHandler(e:Event):void

{

    trace(e);

}

function createButtons(obj:Object):void

{

    var button:SongButton;

  

    for (var i:int = 0, total:int = obj['songs'].length; i < total; i++)

    {

        bgmList = obj['songs'];

        button = new SongButton();

        button.x = stage.stageWidth * 0.5;

        button.y = button.height + (button.height + 10) * i;

        button.txt.autoSize = TextFieldAutoSize.LEFT;

        button.txt.text = bgmList.split(".")[0];      

        addChild(button);

    }  

}

start();

JSON code:

{

    "songs":

    [

        "Acoustic_Circles.mp3",

        "City_Plaza.mp3",

        "Entire.mp3"

    ]

}

FLA download:

animate_cc_as3_playlist_from_json.zip - Google Drive

I hope it helps.

Regards,

JC

Robert Mc Dowell
Legend
January 16, 2018

where do you declare var channel:SoundChannel = new SoundChannel(); ?

oliversen
oliversenAuthor
Inspiring
January 16, 2018

Outside everything. It is the first line of code after import.

JoãoCésar17023019
Community Expert
Community Expert
January 16, 2018

The SoundChannel class doesn't have a start() function.

SoundChannel - Adobe ActionScript® 3 (AS3 ) API Reference