【AS3代码】MP3音乐的播放/暂停/设定音量大小
2012-04-13 11:13
447 查看
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.net.URLRequest;
public class Main extends Sprite
{
private var _sound:Sound;
private var _channel:SoundChannel;
private var _playing:Boolean = false;
private var _playPauseButton:Sprite;
private var _position:int;
private var _trans:SoundTransform
public function Main():void
{
_sound = new Sound(new URLRequest("music.mp3"));
_channel = _sound.play();
_trans = new SoundTransform();
_trans.volume = 0.3; //音量大小设置(0关闭,1最大)
_channel.soundTransform = _trans; //绑定音量设置
//正在播放状态
_playing = true;
//生成一个播放(暂停)按钮
_playPauseButton = new Sprite();
addChild(_playPauseButton);
_playPauseButton.graphics.beginFill(0xff0000);
_playPauseButton.graphics.drawRect(50,50,60,40);
_playPauseButton.graphics.endFill()
_playPauseButton.addEventListener(MouseEvent.MOUSE_UP, onPlayPause);
}
private function onPlayPause(evt:MouseEvent):void
{
//如果正在播放,此次单击则为暂停
if(_playing)
{
_position = _channel.position; //将暂停的当前处保存起来
_channel.stop(); //暂停
}
else
{
_channel = _sound.play(_position); //从暂停处继续播放
}
trace("当前播放长度:" + Math.round(_channel.position) + " / 总长:" + Math.round(_sound.length));
_playing = !_playing; //两种状态转换(播放中/暂停中)
}
}
}
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.net.URLRequest;
public class Main extends Sprite
{
private var _sound:Sound;
private var _channel:SoundChannel;
private var _playing:Boolean = false;
private var _playPauseButton:Sprite;
private var _position:int;
private var _trans:SoundTransform
public function Main():void
{
_sound = new Sound(new URLRequest("music.mp3"));
_channel = _sound.play();
_trans = new SoundTransform();
_trans.volume = 0.3; //音量大小设置(0关闭,1最大)
_channel.soundTransform = _trans; //绑定音量设置
//正在播放状态
_playing = true;
//生成一个播放(暂停)按钮
_playPauseButton = new Sprite();
addChild(_playPauseButton);
_playPauseButton.graphics.beginFill(0xff0000);
_playPauseButton.graphics.drawRect(50,50,60,40);
_playPauseButton.graphics.endFill()
_playPauseButton.addEventListener(MouseEvent.MOUSE_UP, onPlayPause);
}
private function onPlayPause(evt:MouseEvent):void
{
//如果正在播放,此次单击则为暂停
if(_playing)
{
_position = _channel.position; //将暂停的当前处保存起来
_channel.stop(); //暂停
}
else
{
_channel = _sound.play(_position); //从暂停处继续播放
}
trace("当前播放长度:" + Math.round(_channel.position) + " / 总长:" + Math.round(_sound.length));
_playing = !_playing; //两种状态转换(播放中/暂停中)
}
}
}
相关文章推荐
- Android 音视频深入 十四 FFmpeg与OpenSL ES 播放mp3音乐,能暂停(附源码下载)
- win mobile 5播放mp3音乐的方法(4)--5mbox封装类的代码
- 简单的Android音乐播放,解决incall类型不能通过音量按键控制声音大小的问题
- Android开发音乐播放和调节大小声音代码
- AS3.0实现音乐的播放,停止,暂停,和音量的控制
- Android 音视频深入 十四 FFmpeg与OpenSL ES 播放mp3音乐,能暂停(附源码
- win mobile 5播放mp3音乐的方法(4)--5mbox封装类的代码
- win mobile 5播放mp3音乐的方法(4)--5mbox封装类的代码
- win mobile 5播放mp3音乐的方法(4)--5mbox封装类的代码
- 实现点击通知栏发送广播控制音乐的播放、暂停、上一曲、下一曲
- 元旦快乐,阖家团圆,幸福安康.简单地播放MP3和暂停
- 使用VideoView播放视频,锁屏暂停播放之后,过一会自动播放,而且可以通过滑动屏幕调节音量。。。
- duilib 增加gif控件(基于gdi+,可控制播放暂停,自动设置大小)
- win mobile 5播放mp3音乐的方法(3)--pcm码流播放篇
- 使用service的startservice 和bindservice方法实现音乐的播放、暂停、继续
- 免费提供人人日志和博客 qq空间背景音乐、是志播放 在网上不容易找到链接的音乐、视频、mp3、flash等服务
- android播放音乐文件代码
- Linux Ubuntu 使用play命令播放音乐 mp3 wav 等文件
- Ubuntu终端命令行播放音乐(mp3)
- 用DELPHI写的播放器核心代码,播放MP3无压力