Actionscript 3 Flash动作脚本:如何在Flash swf中动态选择视频源?

Actionscript 3 Flash动作脚本:如何在Flash swf中动态选择视频源?,actionscript-3,flash,video,Actionscript 3,Flash,Video,我已经看过一些教程,您可以在其中更改flv视频显示的来源 e、 g 因此,如果单击该按钮,它将播放带有文件路径Testing/01.flv的视频 我想做的是用一个变量替换Testing/01.flv,该变量将包含文件路径数据。因此,理论上,用户可以从计算机中选择另一个视频文件来播放 我以前使用过下面的代码,允许用户从外部源选择文本文件。这能适应我的目的吗 import flash.filesystem.File; import flash.filesystem.FileMode; import

我已经看过一些教程,您可以在其中更改flv视频显示的来源

e、 g

因此,如果单击该按钮,它将播放带有文件路径Testing/01.flv的视频

我想做的是用一个变量替换Testing/01.flv,该变量将包含文件路径数据。因此,理论上,用户可以从计算机中选择另一个视频文件来播放

我以前使用过下面的代码,允许用户从外部源选择文本文件。这能适应我的目的吗

import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;

movieClip_1.addEventListener(MouseEvent.CLICK, fl_OpenFile);

var fl_OpenFileStream:FileStream = new FileStream();    // FileStream used to read from the file
var fl_OpenFileChooser:File = File.documentsDirectory;  // Default to the documents directory
fl_OpenFileChooser.addEventListener(Event.SELECT, fl_FileOpenSelected);

// Main function for opening a file
function fl_OpenFile(event:MouseEvent):void
{
    fl_OpenFileChooser.browseForOpen("Select a text file.");
}

// Opens a FileStream object to read the file
function fl_FileOpenSelected(event:Event):void
{
    fl_OpenFileChooser = event.target as File;
    fl_OpenFileStream = new FileStream();
    fl_OpenFileStream.addEventListener(Event.COMPLETE, fl_FileReadHandler);

    fl_OpenFileStream.openAsync(fl_OpenFileChooser, FileMode.READ);
}

// Write data from the file to the Output Panel
function fl_FileReadHandler(event:Event):void
{
    var fileData:String = fl_OpenFileStream.readMultiByte(fl_OpenFileStream.bytesAvailable, File.systemCharset);
    // The data loaded from the file can now be used from the variable fileData.
    // This example code displays data from the file in the Output panel.
    trace(fileData);
    fl_OpenFileStream.close();
}

多亏了乔治·普洛芬萨斯的评论,我才找到了解决问题的办法

有了这段代码,我现在有了一个可以工作的应用程序,用户可以在其中选择视频源

所以在第一帧我有两个按钮,一个打开文件选择窗口movieClip_1,另一个移动到下一帧movieClip_2

在第二帧中,我有以下代码:

vidPlayer.source = videopath ;
其中,vidPlayer是flvplayer组件的实例

最后,这里是变量videopath的actionscript类文件代码

将变量videopath设置为字符串非常重要,否则链接将无法工作


我还在学习,所以这可能不是最好的方法,也不是最好的编码实践,但它很有效,我找不到更好的方法,所以现在就开始吧。

看看你的示例代码,你想让它用于AIR,对吗?这对Flash player不起作用。是的,我正在使用AIR,因为它对我所做的工作更有效。我认为你不需要在AIR中加载和读取文件,你只需要找到文件的路径,所以在fl_FileOpenSelected中,只要你指定fl_OpenFileChooser,你就可以尝试vidPlayer.source=fl_OpenFileChooser.nativePath;。基本上,您看到了在fl_FileOpenSelected处理程序中选择的文件,只需要将文件传递给您的播放器。也来看看辉煌吧!谢谢乔治·普洛芬萨。我错过的是那条本土道路。正如您所说,我不需要使用全部代码,因为我不是在读取文件,而是在获取文件路径。所以我只使用了代码的第一部分。再次感谢
stop()

import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;

movieClip_1.addEventListener(MouseEvent.CLICK, fl_OpenFile);

var fl_OpenFileStream:FileStream = new FileStream();    // FileStream used to read from the file
var fl_OpenFileChooser:File = File.documentsDirectory;  // Default to the documents directory
fl_OpenFileChooser.addEventListener(Event.SELECT, fl_FileOpenSelected);

// Main function for opening a file
function fl_OpenFile(event:MouseEvent):void
{
    fl_OpenFileChooser.browseForOpen("Select a video file.");

}

// Opens a FileStream object to read the file
function fl_FileOpenSelected(event:Event):void
{
videopath = fl_OpenFileChooser.nativePath;
}


/* Click to Go to Next Frame and Stop
Clicking on the specified symbol instance moves the playhead to the next frame and stops the movie.
*/

movieClip_2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame);

function fl_ClickToGoToNextFrame(event:MouseEvent):void
{
    nextFrame();
}
vidPlayer.source = videopath ;
package  {

    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.utils.setInterval;
    import flash.utils.clearInterval;
    import flash.events.VideoEvent;
    import fl.video.*;

    public class videotest extends MovieClip {
        public var videopath:String; 


        public function videotest() {
            // constructor code
        }
    }

}