I am developing an online listen music website, on that I want to play song based on the user selection from gridview.right now I am using flash object for playing mp3 and video file thi开发者_JAVA技巧s is running fine but its static path.how I can dynamically pass file URL of selected song on flash object.
waiting for your reply.
Using flashvars (requires reloading the page/SWF)
The easiest way would be to pass in the URL as a flashvar, e.g. via the querystring of the SWF file in your object/embed tag:
MyPlayer.swf?url=/path/to/song.mp3
The /path/to/song.mp3
can of course be printed by some server-side logic.
In Flash, you can then access the value of this variable using the LoaderInfo
instance of the root:
var url : String = root.loaderInfo.parameters['url'];
If you want to provide a default for when no flashvar is specified, which is great for dev purposes especially, you can do this by using the ||
operator.
var url : String = root.loaderInfo.parameters['url'] || 'default.mp3';
This will use the specified URL if such exists, or else fall back to use default.mp3.
Using ExternalInterface & Javascript
If you don't want to reload the page, set up a javascript interface to your Flash player using ExternalInterface, e.g. like so:
if (ExternalInterface.available) {
ExternalInterface.addCallback('playUrl', playUrl);
}
function playUrl(url : String) : void {
// TODO: Add playback code here, e.g. using new Sound(url);
}
Then, from Javascript, you can do this:
var swf = document.getElementById('idOfSwfEmbed');
swf.playUrl('http://example.com/path/to/song.mp3');
This will invoke the ActionScript method playUrl()
using the javascript API that was set up by ExternalInterface.addCallback()
.
I don't know .NET, so you'll need to figure out yourself how to invoke the playUrl()
javascript method when a song is selected in your GridView.
If you use the <object />
tag to add the *.swf to the page:
Add following to the tag:
<object ...>
<param name="flashvars" value="path=<%# YOUR_PATH; %>">
</object>
And then inside the SWF:
var path:String = root.loaderInfo.parameters.path;
Hello friends i have got the answer for dynamically play the song.here is my code for this.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Play Now"))
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gridCalls.Rows[index];
string songname = row.Cells[5].Text; // second column in the sql server database
StringBuilder str = new StringBuilder();
str.Append("<object width='300px' height='300px'>");
str.Append("<param name='autostart' value='true'>");
str.Append("<param name='src' value='songs/" + songname + "'>");
str.Append("<param name='autoplay' value='true'>");
str.Append("<param name='controller' value='true'>");
str.Append("<embed width='300px' height='300px' src='songs/" + songname + "' controller='true' autoplay='true' autostart='True' type='audio/wav' />");
str.Append("</object>");
LoadPlayer.Text = str.ToString();//here loadplayer is label control
}
}
精彩评论