使用swift 2中的滑块更改音量

时间:2016-04-21 02:42:39

标签: ios swift avaudioplayer uislider

我在我的应用中添加了ExceptionsManager.js:61 Cannot read property 'animationInterpolators' of undefined handleException @ ExceptionsManager.js:61 handleError @ InitializeJavaScriptAppEngine.js:89 ErrorUtils.reportFatalError @ error-guard.js:27 guard @ MessageQueue.js:44 callFunctionReturnFlushedQueue @ MessageQueue.js:87 onmessage @ debuggerWorker.js:39 ,我正在尝试弄清楚如何更改音乐播放的音量。使用UISlider

从iTunes资料库中播放音乐

这是viewDidLoad函数,用于显示我如何从iTunes Music库中获取音乐。

MPMusicPlayerController

我将滑块连接到IBAction和IBOutlet,并具有以下功能来尝试调整音量:

var musicPlayer = MPMusicPlayerController()

override func viewDidLoad() {
        super.viewDidLoad()

        musicPlayer = MPMusicPlayerController.systemMusicPlayer()


        //Changing the Play/Pause Button
        if musicPlayer.playbackState == MPMusicPlaybackState.Playing {

            playPauseButton.setImage(UIImage(named: "Pause"), forState: UIControlState.Normal)

            //Artwork
            let currentItem: MPMediaItem = musicPlayer.nowPlayingItem!

            let artwork: MPMediaItemArtwork = currentItem.valueForProperty(MPMediaItemPropertyArtwork) as! MPMediaItemArtwork
            let artworkImage = artwork.imageWithSize(CGSize(width: self.view.frame.width, height: 372.0))
            musicPlayerArtwork.image = artworkImage
            musicBottomBackground.image = artworkImage

            //Titel
            let titleString: String = currentItem.valueForProperty(MPMediaItemPropertyTitle) as! String
            songTitle.text = titleString as String

            //Artist & Album
            let artistString: String = currentItem.valueForProperty(MPMediaItemPropertyArtist) as! String
            let albumString: String = currentItem.valueForProperty(MPMediaItemPropertyAlbumTitle) as! String
            albumAndArtistTitle.text = "\(artistString) - \(albumString)"


        } else {

            playPauseButton.setImage(UIImage(named:  "Play"), forState: UIControlState.Normal)

        }        


    }

// I have IBAction functions linked to controlling the playbackk and pause functions that look like this one to play the music 
func startPlayMusic() {

        if self.musicPlayer.playbackState == MPMusicPlaybackState.Paused {

            self.musicPlayer.play()

        }

    }

每次我尝试运行它并移动滑块时,我都会收到以下错误

enter image description here

任何想法我可能做错了什么?

1 个答案:

答案 0 :(得分:1)

您的audioPlayer未正确启动。你不应该致电AVAudioPlayer()。有关您应该使用的初始值设定项列表,请参阅documentation。这是一个例子:

let url = NSBundle.mainBundle().URLForResource("mySong", withExtension: "mp3")!
self.audioPlayer = try! AVAudioPlayer(contentsOfURL: url)