Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Serious Help! Handling sound position and speed.

Community Beginner ,
Oct 29, 2014 Oct 29, 2014

Take a look at this: https://sites.google.com/site/pardeepgames/Untitled-1.swf. It may take some time to load, sound is 42 seconds, see the second text field for time buffering.

I recommend reading the Source Code below first.

Now as you can see the pitch and speed functions are working perfectly, but the problem is that when you move the slider, the songTime text field dosen't work correctly. When you raise the pitch the songTime should just speed up the counting not change it's position, same when you lower the pitch, it should just slow down the counting not change it's position. Well that isn't working for me. The Source Code is there for you below and all the information you need is in the comments in the code. Please help me out. Oh and I recommend focusing on the songTime text field and then moving the slider.

Source Code:

/*

Name of the slider: "slider".

Name of the first text field: "songTime".

Name of the second text field: "songTotalTime".

*/

import flash.events.Event;

import flash.events.SampleDataEvent;

import flash.media.Sound;

import flash.net.URLRequest;

import flash.utils.ByteArray;

import fl.events.SliderEvent;

import flash.media.SoundChannel;

var _playbackSpeed:Number = 2;

var sound:Sound;

var sound2:Sound; // Copy of the original sound, to get the right information about the sound because the pitch shifting for the original sound variable ruins it up.

var myChannel:SoundChannel = new SoundChannel();

var _loadedMP3Samples:ByteArray;

var _phase:Number;

var _numSamples:int;

var request:URLRequest = new URLRequest("https://sites.google.com/site/pardeepgames/Not%20Afraid%20Instrumental.mp3");

var songTimeTime:Number = 1000; // The variable that allows the songTime's speed to change accoriding to the value of the slider. See in line 32.

loadAndplay10(request);

addEventListener(Event.ENTER_FRAME, enterFrame);

function enterFrame(event:Event):void

{

  progressBar.scaleX = myChannel.position / sound2.length;

  _playbackSpeed = slider.value;

  songTimeTime = 1000 / slider.value;

  songTime.text = convertTime(myChannel.position);

  songTotalTime.text = convertTimeLength(sound2.length);

}

function loadAndplay10(request:URLRequest):void

{

  sound = new Sound();

  sound2 = new Sound();

  sound.addEventListener(Event.COMPLETE, mp3Complete);

  sound.load(request);

  sound2.load(request);

}

function playLoadedSound(s:Sound):void

{

  var bytes:ByteArray = new ByteArray();

  s.extract(bytes, int(s.length * 44.1));

  play10(bytes);

}

function mp3Complete(event:Event):void

{

  playLoadedSound(sound);

}

function play10(bytes:ByteArray):void

{

  stop10();

  sound = new Sound();

  sound.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);

  _loadedMP3Samples = bytes;

  _numSamples = bytes.length / 8;

  _phase = 0;

  myChannel = sound.play();

}

function onSampleData( event:SampleDataEvent ):void

{

  var l:Number;

  var r:Number;

  var outputLength:int = 0;

  while (outputLength < 2048)

  {

  // until we have filled up enough output buffer

  // move to the correct location in our loaded samples ByteArray

  _loadedMP3Samples.position = int(_phase) * 8;// 4 bytes per float and two channels so the actual position in the ByteArray is a factor of 8 bigger than the phase

  // read out the left and right channels at this position

  l = _loadedMP3Samples.readFloat();

  r = _loadedMP3Samples.readFloat();

  // write the samples to our output buffer

  event.data.writeFloat(l);

  event.data.writeFloat(r);

  outputLength++;

  // advance the phase by the speed...

  _phase +=  _playbackSpeed;

  // and deal with looping (including looping back past the beginning when playing in reverse)

  if (_phase < 0)

  {

  _phase +=  _numSamples;

  }

  else if (_phase >= _numSamples)

  {

  _phase -=  _numSamples;

  }

  }

}

function convertTime(milliSeconds:Number):String

{

  var Minutes:Number = (milliSeconds % (songTimeTime * 60 * 60)) / (songTimeTime * 60);

  var Seconds:Number = (milliSeconds % (songTimeTime * 60 * 60)) % (songTimeTime * 60) / songTimeTime;

  if (Minutes < 10)

  {

  var displayMinutes:String = "0" + Math.floor(Minutes);

  }

  else

  {

  displayMinutes = Math.floor(Minutes).toString();

  }

  if (Seconds < 10)

  {

  var displaySeconds:String = "0" + Math.floor(Seconds);

  }

  else

  {

  displaySeconds = Math.floor(Seconds).toString();

  }

  return displayMinutes + ":" + displaySeconds;

}

function convertTimeLength(milliSeconds:Number):String

{

  var Minutes2:Number = (milliSeconds % (1000 * 60 * 60)) / (1000 * 60);

  var Seconds2:Number = (milliSeconds % (1000 * 60 * 60)) % (1000 * 60) / 1000;

  if (Minutes2 < 10)

  {

  var displayMinutes2:String = "0" + Math.floor(Minutes2);

  }

  else

  {

  displayMinutes2 = Math.floor(Minutes2).toString();

  }

  if (Seconds2 < 10)

  {

  var displaySeconds2:String = "0" + Math.floor(Seconds2);

  }

  else

  {

  displaySeconds2 = Math.floor(Seconds2).toString();

  }

  return displayMinutes2 + ":" + displaySeconds2;

}

TOPICS
ActionScript
373
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 29, 2014 Oct 29, 2014

No body, this really essential for my MP3 Player. Please help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 30, 2014 Oct 30, 2014

I'm sorry I can't help you, your script gives me the following error:

Scene 1, Layer 'Layer 1', Frame 1, Line 69 1180: Call to undefined method stop10.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 30, 2014 Oct 30, 2014

Oh, I'm sorry, I'll update that right away. Please do check back try to help me. And thank you for the reply.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 30, 2014 Oct 30, 2014

function stop10():void

{

  if (sound)

  {

  sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);

  myChannel.stop();

  }

}

Here is the "stop10()" function.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 30, 2014 Oct 30, 2014
LATEST

No one?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines