美文网首页
Swift 播放url 和本地 音频文件

Swift 播放url 和本地 音频文件

作者: Youzhicha | 来源:发表于2020-11-02 22:46 被阅读0次

    avplayer 播放URL

    示例中url 地址,不对

        var playerItem:AVPlayerItem!
        var avplayer:AVPlayer!
        var playerLayer:AVPlayerLayer!
        
          guard let url = NSURL(string: "https://courseware.uuabc.com/uuabc/videos/xxxxxx") else { fatalError("连接错误") }
          
          playerItem = AVPlayerItem(url: url as URL) // 创建视频资源
          // 将视频资源赋值给视频播放对象
          avplayer = AVPlayer(playerItem: playerItem)
          // 初始化视频显示layer
          playerLayer = AVPlayerLayer(player: avplayer)
          avplayer.play()
      
    
    

    AVAudioPlayer 播放

     var audioPlayer:AVAudioPlayer!
    guard let url = NSURL(string: "https://courseware.uuabc.com/uuabc/videos/xxxx") else { fatalError("连接错误") }
    
            do {         
                let data = try Data.init(contentsOf: url as URL)
                audioPlayer = try AVAudioPlayer.init(data: data)
                audioPlayer.play() 
                } catch let error{
                    print(error.localizedDescription)
                }
    

    avplayer 播放本地文件

    本地文件: audio57.aac

          var audioPlayer:AVAudioPlayer!
          let path = Bundle.main.path(forResource: "audio57", ofType: "aac")!
          let url = URL(fileURLWithPath: path)
          do {
            print("xxx ")
            audioPlayer =  try AVAudioPlayer(contentsOf: url)
          } catch {
            // can't load file
          }
          audioPlayer.play()
          
    
    

    或者
    本地文件: audio57.aac

         var playerItem:AVPlayerItem!
         var avplayer:AVPlayer!
         var playerLayer:AVPlayerLayer!
    
          let path = Bundle.main.path(forResource: "audio57", ofType: "aac")!
          let url = URL(fileURLWithPath: path)
          
          playerItem = AVPlayerItem(url: url as URL) // 创建视频资源
         // 将视频资源赋值给视频播放对象
         avplayer = AVPlayer(playerItem: playerItem)
         // 初始化视频显示layer
         playerLayer = AVPlayerLayer(player: avplayer)
         avplayer.play()
    

    相关文章

      网友评论

          本文标题:Swift 播放url 和本地 音频文件

          本文链接:https://www.haomeiwen.com/subject/yjyvvktx.html