Help us understand the problem. What is going on with this article?

MediaPickerから取得した音楽のpanを調節する

More than 3 years have passed since last update.
ViewController.swift
//
//  ViewController.swift
//  AudioSample
//
//  Created by nagata on 8/22/16.
//  Copyright © 2016 nagata. All rights reserved.
//

import UIKit
import AVFoundation
import MediaPlayer

class ViewController: UIViewController, MPMediaPickerControllerDelegate {
    var audioPlayer: AVAudioPlayer!
    var musicPlayer = MPMusicPlayerController()
    @IBOutlet var slider: UISlider!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        musicPlayer = MPMusicPlayerController.applicationMusicPlayer()
    }

    @IBAction func setVolume() {
        audioPlayer.pan = slider.value
    }
    @IBAction func pick() {
        // MPMediaPickerControllerのインスタンスを作成
        let picker = MPMediaPickerController()
        // ピッカーのデリゲートを設定
        picker.delegate = self
        // 複数選択を不可にする。(trueにすると、複数選択できる)
        picker.allowsPickingMultipleItems = false
        // ピッカーを表示する
        presentViewController(picker, animated: true, completion: nil)
    }

    func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {

        // 選択した曲情報がmediaItemCollectionに入っているので、これをplayerにセット。
        musicPlayer.setQueueWithItemCollection(mediaItemCollection)

        if let url: NSURL = mediaItemCollection.items[0].assetURL {
            do {
                // itemのassetURLからプレイヤーを作成する
                audioPlayer = try AVAudioPlayer(contentsOfURL: url)
            } catch  {
                // エラー発生してプレイヤー作成失敗
                audioPlayer = nil
            }
        } else {
            // urlはnilだった

            // プレイヤーはnilとしておく
            audioPlayer = nil
        }
        audioPlayer.pan = slider.value
        // 再生開始
        audioPlayer.play()
        // ピッカーを閉じ、破棄する
        dismissViewControllerAnimated(true, completion: nil)

    }


    //選択がキャンセルされた場合に呼ばれる
    func mediaPickerDidCancel(mediaPicker: MPMediaPickerController) {
        // ピッカーを閉じ、破棄する
        dismissViewControllerAnimated(true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


関連付けもする。sliderはchangeValueでつなげるところに注意!

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした