当我的应用视频在swift 3中播放时,背景音乐如何暂停和播放

时间:2017-03-09 07:37:37

标签: swift3 avplayer avaudiosession

我创建了一个小型视频应用程序但是当我播放视频时,背景音乐暂停。但我希望在视频暂停时再次播放音乐,当我再次播放视频时,音乐播放器将恢复播放。

我对大部分链接进行了分层,但没有得到结果

var audioPlayer = AVAudioPlayer()

try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [.duckOthers])
try! AVAudioSession.sharedInstance().setActive(true) 

当我使用这个比两个都在播放但我想暂停音乐时视频播放和如果视频暂停比背景音乐播放。像Whatsapp

非常感谢任何关于可能方式的想法或反馈!

4 个答案:

答案 0 :(得分:1)

var audioPlayer = AVAudioPlayer()

try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [.duckOthers])
try! AVAudioSession.sharedInstance().setActive(true) 

看起来你每次都创建了实例, 每次初始化它,如果是这样,那么它每次都会从第一次开始播放音乐。

所以尽量让它全球化。

答案 1 :(得分:0)

此最新swift3代码

你可以使用avPlayerViewController它可以是默认暂停和播放所有设置都可以。所以你可以只为项目添加两个框架。这些框架是

1)AV Kit Framework                2)AV基础框架 将这些框架导入到您的类中并为玩家创建对象,并将mp3文件或mp4文件添加到项目导航中,或者从也使用的URL添加

         func playButton(_ sender: AnyObject) {
    let url = Bundle.main.url(forResource: "01 O Rangula Chilaka", withExtension: "mp3")!
    let player = AVPlayer(url: url as URL)
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player
    self.present(playerViewController, animated: true) {




        playerViewController.player!.play()

    }


}

播放歌曲使用此代码

    let videoURL = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
    let player = AVPlayer(url: videoURL! as URL)
    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = vedioPlayer.bounds
    vedioPlayer.layer.addSublayer(playerLayer)
    player.play()

播放视频使用此代码将其mp4文件添加到您的项目或url也用于播放视频

      let url = Bundle.main.url(forResource: "Shakthi - Premadesam Yuvarani", withExtension: "mp4")!
    avPlayerLayer = AVPlayerLayer(player: avPlayer)
    videoPlayer.layer.insertSublayer(avPlayerLayer, at: 0)//videoPlayer is the uiview  the vide0plays only that view.
   let playerViewController = AVPlayerViewController()
    playerViewController.player = player
    self.present(playerViewController, animated: true) {




        playerViewController.player!.play()

    }

此代码用于项目中的mp4文件

public class DragDrop : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {


private Vector3 startPosition;
private Vector3 endPosition;

private float distanceToDrag;

public void OnBeginDrag(PointerEventData eventData)
{
    startPosition = Input.GetTouch(0).position; // get current Tocuh position
}

public void OnDrag(PointerEventData eventData)
{
    //Drag your UI as you wish
}

public void OnEndDrag(PointerEventData eventData)
{
    endPosition = Input.GetTouch(0).position; // get current Tocuh position
    float distance = Vector2.distance(startPosition,endPosition);

    if (distance > distanceToDrag)
        //start the game
    else
        //Put the UI back.
}
}

答案 2 :(得分:0)

在AppDelegate中快速4:

func applicationDidBecomeActive(application: UIApplication) {
    do {
        try AVAudioSession.sharedInstance().setCategory(.ambient, mode: .default)
        try AVAudioSession.sharedInstance().setActive(true)
    } catch {
        print(error)
    }
        // ...
}

答案 3 :(得分:-1)

最终获得解决方案,但仅限于默认系统音乐播放器

reference link

首先允许音乐播放器...

<key>NSAppleMusicUsageDescription</key>
<string>My description about why I need this capability</string>

您可以暂停或播放默认音乐播放器,这是代码

import MediaPlayer

var defaultPlayer = MPMusicPlayerController.systemMusicPlayer()
defaultPlayer.play() 
//you can also pause defaultPlayer.pause()
//get current title defaultPlayer.nowPlayingItem?.title
//get current playbacktime defaultPlayer.player.currentPlaybackTime