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!

oliversen
oliversenAuthor
Inspiring
April 29, 2018

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


Thanks alot JoãoCésar!

I have worked a little with your code. It seems like I get an error if I change the document type to Air for Android.

Scene 1, Layer 'Layer_1', Frame 1, Line 41, Column 13 1046: Type was not found or was not a compile-time constant: SongButton.

Scene 1, Layer 'Layer_1', Frame 1, Line 46, Column 16 1180: Call to a possibly undefined method SongButton.

I guess It has something to do with SongButton not being part of the AIR. But I cannot find anything about it on the web.

Thank you

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