Jump to content

Shuffle() Issue


Fijiannn

Recommended Posts

Hi in my code below, echo $body; outputs the link to every song in my $SongsFolder. How can I make it echo only one link and that link is the song the $Shuffle chose? I know I have array but is there a way to get around it? Anything I should change up?

<?php
function fooFucker($url) {
$a = join('/', array_map('urlencode', explode('/', 
$url)));
return str_replace('+', '%20', $a);
}

$SongsFolder = "songs";

header("Content-Type: audio/x-mpegurl");

$body = '';

$Songs = glob($SongsFolder."/*.mp3");
shuffle($Songs);
$first = 0;

foreach($Songs as $Song)
{
$SongPath = pathinfo($Song);
if ($first != 0) {
$body .= "\n";
}
$first = 1;
$body .= 
"http://".$_SERVER['HTTP_HOST'].fooFucker(dirname($_SERVER['PHP_SELF']))."/".fooFucker($SongsFolder)."/".fooFucker($SongPath['basename']);
}
header('Content-Length: ' . strlen($body));
echo $body;
?>
Link to comment
Share on other sites

The shuffle function shuffles all files in the array. What did you mean by saying this:

When I echo $body it echo's the link to every mp3 song in my folder called "songs" ($SongsFolder) when all I want is for the shuffle() to just shuffle, choose a link to a song then output the link to the song it chose.

Link to comment
Share on other sites

 

Your first shuffled file into an array could be:

$Songs = glob($SongsFolder."/*.mp3");

shuffle($Songs);

echo $Songs[0];  // or am I wrong? 

Based on his previous thread, he believes that shuffle($Songs) in conjunction with $Songs[0] creates an array for each song, rather it the return value of Glob is an array which has all the songs in it

 

Someone gave this solution to him before, not sure why the second thread.

Edited by GetFreaky
Link to comment
Share on other sites

Based on his previous thread, he believes that shuffle($Songs) in conjunction with $Songs[0] creates an array for each song, rather it the return value of Glob is an array which has all the songs in it

 

Someone gave this solution to him before, not sure why the second thread.

So this code above will give me the link to the chosen song after shuffle? That's what I don't get.

Link to comment
Share on other sites

You're off the track.

Try this code.

<?php
$songsfolder = "songs";
header("Content-Type: audio/x-mpegurl");
$body = '';
$songs = glob($songsfolder."/*.mp3");
if (count($songs) == 0)
{
echo "Error - no songs found";
exit();
}
shuffle($songs);
$body .= "http://" . $_SERVER['HTTP_HOST'] . $songs[0];
header('Content-Length: ' . strlen($body));
echo $body;
?>

Not sure about the syntax of my $body content since it's been awhile since I used glob and pathinfo functions. You may have to add a slash or another folder reference, but this will do what you want.

Plus - not sure what at all you expect this to do since you are simply echoing a text line that has a url in it, but you have specified headers that imply something else. AND - your original post mentioned a "link", which is not in your code either.

And it does it without using offensive or childish function names.

Link to comment
Share on other sites

You're off the track.

Try this code.

<?php
$songsfolder = "songs";
header("Content-Type: audio/x-mpegurl");
$body = '';
$songs = glob($songsfolder."/*.mp3");
if (count($songs) == 0)
{
echo "Error - no songs found";
exit();
}
shuffle($songs);
$body .= "http://" . $_SERVER['HTTP_HOST'] . $songs[0];
header('Content-Length: ' . strlen($body));
echo $body;
?>

Not sure about the syntax of my $body content since it's been awhile since I used glob and pathinfo functions. You may have to add a slash or another folder reference, but this will do what you want.

Plus - not sure what at all you expect this to do since you are simply echoing a text line that has a url in it, but you have specified headers that imply something else. AND - your original post mentioned a "link", which is not in your code either.

And it does it without using offensive or childish function names.

I'll try it when I get home but I wanted to make the link that it outputted a variable so I can call that variable in my iPhone app for something in my code.

Link to comment
Share on other sites

So this code above will give me the link to the chosen song after shuffle? That's what I don't get.

 

An array has a set of indexes, much like memory address, its has an address (key) and a value.

 

-> SongArray 

 

   [0] => "Song 1"

   [1] => "Song 2"

   [2] => "Song 3"

 

- > Invoke Shuffle(SongArray)

 

-> SongArray

 

   [0] => " Song 2"

   [1] => "Song 1"

   [2] => "Song 3"

 

 

Index 0 of the array will most likely never contain the same value after using shuffle, so it only makes sense to grab the index 0.

Edited by GetFreaky
Link to comment
Share on other sites

An array has a set of indexes, much like memory address, its has an address (key) and a value.

 

-> SongArray

 

[0] => "Song 1"

[1] => "Song 2"

[2] => "Song 3"

 

- > Invoke Shuffle(SongArray)

 

-> SongArray

 

[0] => " Song 2"

[1] => "Song 1"

[2] => "Song 3"

 

 

Index 0 of the array will most likely never contain the same value after using shuffle, so it only makes sense to grab the index 0.

Very good explanation man! See I just needed it explained like this.

Link to comment
Share on other sites

No problem, everyone helped out equally as well,

 

Is your code working now?

 

Well I replaced my code with the code above. 

<?php

$songsfolder = "songs";

header("Content-Type: audio/x-mpegurl");

$body = '';

$songs = glob($songsfolder."/*.mp3");

if (count($songs) == 0)

{

echo "Error - no songs found";

exit();

}

shuffle($songs);

$body .= "http://" . $_SERVER['HTTP_HOST'] . $songs[0];

header('Content-Length: ' . strlen($body));

echo $body;

?>

It does give me a link "http://virsamusic.comsongs/21 Bharat Mata Ki Jai.mp3 " I messed up link, not linking to the actual song but it does get the name of the .mp3. Also, it's suppose to stream music and my music isn't playing. What could be wrong now?

Link to comment
Share on other sites

No problem, everyone helped out equally as well,

 

Is your code working now?

 

So with a little editing on the code below. I got it to output the correct link to a song but the thing is my music isn't playing so I can't tell if link the console outputted is correct with the song playing. What's wrong?

<?php
    function myFunction($url) {
        $a = join('/', array_map('urlencode', explode('/', $url)));
        return str_replace('+', '%20', $a);
    }
    
    $songsfolder = "songs";
    header("Content-Type: audio/x-mpegurl");
    $body = '';
    $songs = glob($songsfolder."/*.mp3");
    if (count($songs) == 0)
    {
        echo "Error - no songs found";
        exit();
    }
    shuffle($songs);
    $body .= "http://" . $_SERVER['HTTP_HOST'].myFunction(dirname($_SERVER['PHP_SELF']))."/".myFunction($SongsFolder).$songs[0];
    
    header('Content-Length: ' . strlen($body));
    echo $body;
?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.