Results 1 to 4 of 4

Thread: mp3 flash player issues

  1. #1

    mp3 flash player issues

    Hi everyone,

    I'm new to AS3 so please be as patient as you can.


    For the past 3 weeks or so, I've been trying to make a mp3 player that loads the songs via an XML file, based on the date of the playlist. I have few issues that are giving me some headaches.

    First, I get this error when I try to change the date of the playlist, but I have no clue why. I just can't see where the problem lies:
    Code:
    TypeError: Error #2007: Parameter text must be non-null.
    	at flash.text::TextField/set text()
    	at ddp_listen_fla::MainTimeline/add_btns()
    	at ddp_listen_fla::MainTimeline/processXML()
    	at flash.events::EventDispatcher/dispatchEventFunction()
    	at flash.events::EventDispatcher/dispatchEvent()
    	at flash.net::URLLoader/onComplete()
    second issue is that if I set a new position of the currently playing song, when the song finishes playing it does not play next song no matter what. But if I don't alter the sound progress it does play the next song...

    third issue, kind of related to the previous one, is that if I set a new position of the currently playing song and the song hasn't finished downloading the seek bar behaves strangely, moving backwards and forwards.

    this is the code that I'm using. hopefully will help you understand my issues:
    Code:
    stop();
    import flash.events.MouseEvent;
    import flash.media.Sound;
    import flash.events.Event;
    
    var currentDate = new Date();
    var playlistDate = new Date();
    var monthDigit:String = new String();
    var dayDigit:String = new String();
    var playlistLoader:URLLoader = new URLLoader();
    var file_title:Array = new Array();
    var file_url:Array = new Array();
    var file_name:Array = new Array();
    var nr_songs:Number = new Number(0);
    var sound:Sound = new Sound();
    var channel:SoundChannel = new SoundChannel();
    var song:Number = new Number();
    var songPos:Number = new Number();
    var duration:Number = new Number();
    var total:Number = new Number();
    var loaded:Number = new Number();
    var repeatState:Number = new Number(0);
    // 0 = repetarea este oprita
    // 1 = doar playlistul curent
    // 2 = redare continua a playlisturilor
    
    addEventListener(Event.ENTER_FRAME, calcProgress);
    retry_btn.addEventListener(MouseEvent.CLICK, reloadPlaylist);
    nextPlaylist_btn.addEventListener(MouseEvent.ROLL_OVER, btn_roll_over);
    nextPlaylist_btn.addEventListener(MouseEvent.ROLL_OUT, btn_roll_out);
    nextPlaylist_btn.addEventListener(MouseEvent.CLICK, nextPlaylist);
    prevPlaylist_btn.addEventListener(MouseEvent.ROLL_OVER, btn_roll_over);
    prevPlaylist_btn.addEventListener(MouseEvent.ROLL_OUT, btn_roll_out);
    prevPlaylist_btn.addEventListener(MouseEvent.CLICK, prevPlaylist);
    playPause_btn.addEventListener(MouseEvent.CLICK, playPause);
    stop_btn.addEventListener(MouseEvent.CLICK, stopPlay);
    progress.progressDownload.addEventListener(MouseEvent.CLICK, setNewProgress);
    
    retry_btn.buttonMode = true;
    progress.progressDownload.buttonMode = true;
    progress.progressDownload.scaleX = 0;
    progress.progressFill.scaleX = 0;
    
    currentDate.date = (currentDate.getDate() - currentDate.getDay() - 1);
    playlistDate.date = (playlistDate.getDate() - playlistDate.getDay() - 1);
    
    currentDate = new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate());
    playlistDate = new Date(playlistDate.getFullYear(),playlistDate.getMonth(),playlistDate.getDate());
    
    
    function main():void
    {
    	day.text = playlistDate.getDate();
    	month.text = playlistDate.getMonth() + 1;
    	year.text = playlistDate.getFullYear();
    	//generate playlist file name
    	//var url:String = new String("http://---web--site--adress---/playlist.php?date=");
    	twoDigit();
    	//var playlisturl:String = url.concat(playlistDate.getFullYear(),monthDigit,playlistDate.getMonth() + 1,dayDigit,playlistDate.getDate());
    	var playlisturl:String ="playlist.xml";
    	playlistLoader = new URLLoader(new URLRequest(playlisturl));
    	playlistLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
    	playlistLoader.addEventListener(Event.COMPLETE, processXML);
    	trace("playlist loaded");
    }
    main();
    
    //load rss;
    function processXML(event:Event):void
    {
    	var xml:XML = new XML(event.target.data);
    	var il:XMLList = xml.channel.item;
    	var mediaNS:Namespace = xml.namespace("media");
    	xml.addNamespace(mediaNS);
    	for (var i:uint = 0; i < xml.channel.item.length(); i++)
    	{
    		var item_url:XMLList = new XMLList(xml.channel.item[i].mediaNS::content.(@type=="audio/mpeg3")[0].@url);
    		var item_title:XMLList = new XMLList(xml.channel.item[i].description);
    		file_title[i] = String(item_title);
    		file_url[i] = String(item_url);
    		file_name[i] = file_url[i].slice(file_url[i].indexOf("["),-4);
    		nr_songs++;
    	}
    	playlistLoader.removeEventListener(Event.COMPLETE, processXML);
    	playlistLoader.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
    	playlistLoader = null;
    	trace("got song info from xml file");
    	add_btns();
    }
    
    //add btns
    function add_btns()
    {
    	for (var i:int = 0; i <nr_songs; i++)
    	{
    		var new_btn:song_btn = new song_btn();
    		new_btn.name = "song_btn_" + [i];
    		new_btn.x = 25 + i * 85;
    		new_btn.y = 65;
    		new_btn.song_title.text = file_title[i];
    		new_btn.addEventListener(MouseEvent.ROLL_OVER, btn_roll_over);
    		new_btn.addEventListener(MouseEvent.ROLL_OUT, btn_roll_out);
    		new_btn.addEventListener(MouseEvent.CLICK, getSong_nr);
    		//new_btn.buttonMode = true;
    		this.addChild(new_btn);
    	}
    	trace("buttons added");
    }
    
    function remove_btns()
    {
    	for (var i:int=0; i<nr_songs; i++)
    	{
    		if(getChildByName("song_btn_"+i)){
    		removeChild(getChildByName("song_btn_"+i));
    		}
    	}
    	trace("buttons removed");
    }
    
    function getSong_nr(event:MouseEvent):void
    {
    	song = Number(event.currentTarget.name.slice(-1));
    	playSong(song);
    }
    
    //play song
    function playSong(song:Number):void
    {
    	if (channel)
    	{
    		channel.stop();
    	}
    	sound = new Sound();
    	sound.load(new URLRequest (file_url[song]));
    	sound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
    	sound.addEventListener(ProgressEvent.PROGRESS, loadingSong);
    	channel = sound.play();
    	channel.addEventListener(Event.SOUND_COMPLETE, nextSong);
    	playPause_btn.gotoAndStop("pause");
    	currentSong.text = file_name[song];
    	trace("playing song: "+(song+1));
    
    }
    
    //stop;
    function stopPlay(event:Event):void
    {
    	playPause_btn.gotoAndStop("play");
    	channel.stop();
    	songPos = 0;
    	progress.progressDownload.scaleX = 0;
    	progress.progressFill.scaleX = 0;
    	currentSong.text == "";
    }
    
    function prevPlaylist(event:Event):void
    {
    	playlistDate = new Date(year.text,Number(month.text) - 1,day.text);
    	playlistDate.date -=  7;
    	trace("prev playlist called");
    	remove_btns();
    	main();
    }
    function nextPlaylist(event:Event):void
    {
    	playlistDate = new Date(year.text,Number(month.text) - 1,day.text);
    	if (playlistDate < currentDate)
    	{
    		playlistDate.date +=  7;
    		trace("next playlist called");
    		remove_btns();
    		main();
    	}
    }
    
    //next song;
    function nextSong(event:Event)
    {
    	song++;
    	if (song == nr_songs)
    	{
    		song = 0;
    	}
    	trace("new song called");
    	playSong(song);
    }
    
    function loadingSong(event:ProgressEvent):void
    {
    
    	total = sound.bytesTotal;
    	loaded = sound.bytesLoaded;
    	progress.progressDownload.width = loaded / total * 300;
    
    	if (total == loaded)
    	{
    		sound.removeEventListener(ProgressEvent.PROGRESS, loadingSong);
    	}
    }
    
    function setNewProgress(event:MouseEvent):void
    {
    	var soundDist:Number = progress.progressDownload.mouseX / progress.progressDownload.width;
    	if (soundDist < 0)
    	{
    		soundDist = 0;
    	}
    	if (soundDist > 1)
    	{
    		soundDist = 1;
    	}
    	channel.stop();
    	channel = sound.play(sound.length * soundDist);
    	progress.progressFill.scaleX = soundDist;
    
    	playPause_btn.gotoAndStop("pause");
    
    }
    
    function calcProgress(event:Event):void
    {
    	var percentLoaded:Number = sound.bytesLoaded / sound.bytesTotal*100;
    	var approxProgress:Number  = channel.position / sound.length*percentLoaded;
    	progress.progressFill.scaleX = approxProgress/100;
    }
    
    //play pause btn
    function playPause(event:Event):void
    {
    	if (channel)
    	{
    		if (playPause_btn.currentFrame == 2)
    		{
    			songPos = channel.position;
    			playPause_btn.gotoAndStop("play");
    			channel.stop();
    		}
    		else
    		{
    			channel = sound.play(songPos);
    			playPause_btn.gotoAndStop("pause");
    		}
    	}
    }
    
    // get month and date with 2 digit
    function twoDigit():void
    {
    	if (Number(month.text) < 10)
    	{
    		monthDigit = "0";
    	}
    	else
    	{
    		monthDigit = "";
    
    	}
    	if (Number(day.text) < 10)
    	{
    		dayDigit = "0";
    	}
    	else
    	{
    		dayDigit = "";
    	}
    }
    
    function reloadPlaylist(event:Event):void
    {
    	remove_btns();
    	main();
    }
    
    //button animations
    function btn_roll_over(event:MouseEvent):void
    {
    	event.currentTarget.roll_anim.play();
    }
    
    function btn_roll_out(event:MouseEvent):void
    {
    	event.currentTarget.roll_anim.play();
    }
    
    function ioErrorHandler(event:Event):void
    {
    }
    I'm attaching the fla file as well as the XML file I'm using - just in case. Please help me with this and point in the right direction.
    Thanks!
    Attached Files Attached Files

  2. #2
    Someone?

  3. #3

    >= nr_songs!

    nr_songs is the total number of songs but song is an index.
    Just do >= nr_songs - 1 & not == nr_songs.

    Code:
    function nextSong(event:Event) {  
        song++;     
        if (song >= nr_songs - 1)     { 
            song = 0;     
        } 
        trace("new song called");     
        playSong(song); 
    }

    -----------------------------------------------
    pirates game
    Last edited by motrack; June 25th, 2012 at 11:11 AM.

  4. #4
    Hi motrack,

    I appreciate your answer! I can't give it a run right now, but I can tell it will not work.
    I have sketched bellow the way that function "works" for both your way and mine

    my way your way
    song = 0;
    nr_songs = 4;

    1st call --------------------------->
    song ++; (song = 1);
    is song == nr_songs? (1==4?) NO
    playSong(1);

    2nd call--------------------------->
    song ++; (song = 2);
    is song == nr_songs? (2==4?) NO
    playSong(2);

    3rd call--------------------------->
    song ++; (song = 3);
    is song == nr_songs? (3==4?) NO
    playSong(3);

    4th call--------------------------->
    song ++; (song = 4);
    is song == nr_songs? (4==4?) YES
    song = 0
    playSong(0);
    song = 0;
    nr_songs = 4;

    1st call
    song ++; (song = 1);
    is song >= nr_songs-1? (1>=4-1?) NO
    playSong(1);

    2nd call
    song ++; (song = 2);
    is song >= nr_songs-1? (2>=4-1?) NO
    playSong(2);

    3rd call
    song ++; (song = 3);
    is song >= nr_songs-1? (3>=4-1?) YES
    song = 0
    playSong(0);





























    If the above is correct, then the last song will never get played.

    LE: Yep, just like I said, it does not play last song in the playlist.
    Last edited by taseandrei15; June 27th, 2012 at 09:22 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012