当前访客身份:游客 [ 登录 | 加入开源中国 ]

代码分享

当前位置:
代码分享 » Scala  » 编程基础
崔钢

scala mp3 播放器

崔钢 发布于 2010年12月30日 17时, 2评/1423阅
分享到: 
收藏 +0
1
使用javazoom类库。程序打开一个swing窗口。实现了播放和暂停。主要展示了如何使用actor消息实现暂停控制。
标签: MP3 Scala

代码片段(1) [全屏查看所有代码]

1. [代码][Scala]代码     跳至 [1] [全屏预览]

import javazoom.jl.player.FactoryRegistry
import actors.Actor
import scala.actors.Actor._
import swing.SimpleSwingApplication
import swing._
import java.io.{FileInputStream, InputStream}
import swing.event._
import javazoom.jl.decoder.{SampleBuffer, Decoder, Bitstream}
import java.util.Arrays


/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2010-12-26
 * Time: 10:17:35
 * 我们自己的播放器 ;这个例子需要使用javazoom类库。实现的是Actor的通信,进行暂停控制。
 */

class Player(var stream: InputStream) extends Actor {
    val bitstream = new Bitstream(stream)
    val decoder = new Decoder
    val audio = FactoryRegistry.systemRegistry.createAudioDevice

    var closed = false
    var complete = false
    var lastPosition = 0
    var paused = false
    var frames = 0

    audio.open(decoder)

    println(audio.isOpen)

    def act() {
        play
    }

    def close: Unit = {
        audio.close
        lastPosition = audio.getPosition
        bitstream.close
    }

    def play {
        var ret = true
        while (ret) {
            if (paused) {
                println("paused")
                Thread.sleep(100)
            } else {
                ret = decodeFrame
                frames =  + 1
                //println(frames)
            }
        }
        if (!ret) {
            audio.flush
            close
        }
    }

    def decodeFrame: Boolean = {
        try {
            val h = bitstream.readFrame
            val output = decoder.decodeFrame(h, bitstream).asInstanceOf[SampleBuffer]
            //println("output:" + Arrays.toString(output.getBuffer))
            audio.write(output.getBuffer, 0, output.getBufferLength)
            //println("decoding...")
            bitstream.closeFrame
            true
        } catch {
            case e: Exception =>
                e.printStackTrace
                return false
        }
    }
}

object Player extends SimpleSwingApplication {
    val test = new FileInputStream("d:/hero.mp3")

    val myplay = new Player(test)

    val controler = actor {
        loop {
            react {
                case "play" =>
                    if (myplay.paused) {
                        myplay.paused = false
                    }
                    else {
                        myplay.start
                    }
                case "pause" => myplay.paused = true
            }
        }
    }

    controler.start

    val play = new Button {
        text = "play"
    }

    val pause = new Button {
        text = "pause"
    }

    val mainpanel = new FlowPanel {
        contents ++ Array(play, pause)
    }

    def top = new MainFrame {
        title = "my player"
        contents = mainpanel
    }

    listenTo(play, pause)
    reactions += {
        case ButtonClicked(b) => b.text match {
            case "play" => controler ! "play"
            case "pause" => controler ! "pause"
        }
    }

}


开源中国-程序员在线工具:Git代码托管 API文档大全(120+) JS在线编辑演示 二维码 更多»

发表评论 回到顶部 网友评论(2)

  • 1楼:mj4738 发表于 2012-08-26 14:57 回复此评论
    javazoom 是啥、

  • 2楼:寂寞的地瓜 发表于 2013-06-14 19:44 回复此评论

    第111行 contents ++ Array(play, pause)

    改为 contents ++= Array(play, pause)就可以啦

开源从代码分享开始 分享代码
崔钢的其它代码 全部(8)...