a
Giờ chúng ta sẽ tiến hành làm bài tập về gọi 1 file âm thanh từ bên ngoài vào flash và điều khiển nó nhé.
Nguyên vật liệu cho bài tập này là 1 file nhạc mp3, mình chọn bài happy new year còn các bạn có thể chọn 1 bài tùy ý cho bài tập này.
Bước 1:
Tạo 1 file flash mới Ctrl + N chọn loại là Action Script AS 3.0, nhấn Ctrl + J để tùy chỉnh kích thước cho phù hợp.
Bước 2:
Công việc của chúng ta là tạo 2 button Playpause và Stop cho phù hợp với ý định của bạn.
Đặt tên cho button Play và pause là: xplay và button stop là: xstop
Tạo 1 layey mới, click vào frame đầu tiên nhấn F9 để mở bảng ActionScript và chèn vào đoạn code sau:
//number that is redefined when the pause button is hitvar pausePoint:Number = 0.00;//a true or false value that is used to check whether the sound is currently playingvar isPlaying:Boolean;//think of the soundchannel as a speaker system and the sound as an mp3 playervar soundChannel:SoundChannel = new SoundChannel();var sound:Sound = new Sound(new URLRequest("http://aloflash.com/images/flash/sound.mp3"));//you should set the xstop and xplay values to match the instance names of your stop button and play/pause buttonsxstop.addEventListener(MouseEvent.CLICK, clickStop);xplay.addEventListener(MouseEvent.CLICK, clickPlayPause);soundChannel = sound.play();isPlaying = true;function clickPlayPause(evt:MouseEvent) {if (isPlaying) {pausePoint = soundChannel.position;soundChannel.stop();isPlaying = false;} else {soundChannel = sound.play(pausePoint);isPlaying = true;}}function clickStop(evt:MouseEvent) {if (isPlaying) {soundChannel.stop();isPlaying = false;}pausePoint = 0.00;}
Trong đó: http://aloflash.com/images/flash/sound.mp3 là đường dẫn đến file nhạc của bạn
Như vậy là ta đã có thể điều khiển được file nhạc mà chúng ta đã gọi vào flash. Bạn hãy nhấn Ctrl + Enter để nghe thử.
Chúc các bạn thành công
aloflash.com