只需按一下按钮,如何在swift 2中播放m4a文件?

时间:2015-10-11 18:26:45

标签: ios swift audio swift2

当我触摸按钮时,如何播放音频文件,没有什么工作可以在网上找到,因为它全部都很快。

我想要original功能中的音频代码。

import UIKit
import AVFoundation

class ViewController: UIViewController {
    @IBAction func original(sender: AnyObject) {        
    }
}

3 个答案:

答案 0 :(得分:7)

以下是一些可以帮助你的代码:

Swift 3

import UIKit
import AVFoundation

class ViewController: UIViewController
{
    let player = AVQueuePlayer()

    @IBAction func original(sender: AnyObject) {
        if let url = Bundle.main.url(forResource: "sample_song", withExtension: "m4a") {
            player.removeAllItems()
            player.insert(AVPlayerItem(url: url), after: nil)
            player.play()
        }
    }
}

Swift 2

import UIKit
import AVFoundation

class ViewController: UIViewController
{
    let player = AVQueuePlayer()

    @IBAction func original(sender: AnyObject) {
        if let url = NSBundle.mainBundle().URLForResource("SomeAudioFile", withExtension: "m4a") {
            player.removeAllItems()
            player.insertItem(AVPlayerItem(URL: url), afterItem: nil)
            player.play()
        }
    }
}

你没有解释如果按钮被多次击中会发生什么,所以我认为你想要停止正在播放的当前项目并开始一个新项目。

答案 1 :(得分:0)

import os

os.chdir("C:/path_to_pdf_files")
for pdf in glob.glob("C:/path_to_pdf_files/*.pdf"):
    pdf_name = pdf.split("/")[-1]
    pdf_name = pdf_name.replace(" ","\ ")
    pdf_path_name = "C:/path_to_pdf_files/"+pdf_name
    os.system("pdf2htmlEX --zoom 1.3 %s"%pdf_path_name)```

答案 2 :(得分:0)

Swift 5更新:

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *


class Page(QWidget):
    def __init__(self, parent=None):
        super(Page, self).__init__(parent)

        my_label = QLabel("This is my label")
        layout = QVBoxLayout()

        layout.addWidget(my_label)

        mainLayout = QGridLayout()
        mainLayout.addLayout(layout, 0, 1)

        self.setLayout(mainLayout)
        self.setWindowTitle("My First Qt App")


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)

    window = Page()
    window.show()

    sys.exit(app.exec_())

相关问题