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
January 17, 2018

Sure!

In the changeSound function, remove the playSound from if (playing), like this:

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);

}

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