使用 AVPlayer 播放多个音频文件

发布于 2024-11-11 04:11:58 字数 290 浏览 2 评论 0原文

我正在尝试同时播放多种声音。

我最初采取的方法是创建多个玩家,但这似乎是错误的。

同时播放多个音频文件的最佳方式是什么?

是通过将它们制作为 AVAssets 来实现的吗?但是在这种情况下,我如何随时停下来播放它们。

非常感谢您的帮助。

我需要 AVPlayer 的原因是从 iPod 库中获取声音。

我终于从Apple Dev Team的TechSupport得到了答复,当我决定使用多个AVPlayer时,看来我走上了正确的道路。

I'm trying to play multiple sounds at the same time.

The approach initially I've taken was to create several players , but it seems wrong one.

What's the best way to play several audio files at the same time.

Is it through making them AVAssets, but in this case how would I stop and play them whenever I want.

Really appreciate your help.

The reason I need AVPlayer is to fetch sounds from the iPod Library.

I finally got an answer from TechSupport of Apple Dev Team and it seems I'm on the right track when I decided to use several AVPlayer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

落墨 2024-11-18 04:11:58

对于您想要制作的每种声音,制作一个新的 AVPlayer。

NSURL *url = [NSURL URLWithString:pathToYourFile];

AVPlayer *audioPlayer = [[AVPlayer alloc] initWithURL:url];


[audioPlayer 播放];

For every sound you want to make make a new AVPlayer.

NSURL *url = [NSURL URLWithString:pathToYourFile];

AVPlayer *audioPlayer = [[AVPlayer alloc] initWithURL:url];


[audioPlayer play];

缺⑴份安定 2024-11-18 04:11:58

我从来没有在这里回答过问题,我不知道还有谁在等待这个问题的答案,但这是我的看法...尝试一下,它应该可以工作,我目前正在使用它来播放 12 个以上的同步音频样本。如果我正在做一些新的事情,我深表歉意。

您按下一个按钮,然后运行此代码...

但首先您需要:

  1. 需要将 AVFoundation 导入到项目中,并 #import 到 .h 文件中,然后我们可以用它来播放声音。
  2. 需要输入“AVAudioPlayer *myAudio;”当然在顶部的某个地方(通常在 viewDidLoad 的顶部)没有引号。

那么就...

-(IBAction)playButtonPressed:(id)sender {

    NSURL *yourMusicFile;
    yourMusicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"your_Song_Name" ofType:@"mp3"]];

    myAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
    [myAudio play];
    NSLog(@"Button -playButtonPressed- has been pressed!");
}

I have never answered a question here and I don't know in anyone is still waiting for an answer to this but heres my take... Try this and it should work, I am currently using it to play 12 plus simultaneous audio samples. I apologize if I am doing something newbish..

You press a button and you run this code...

But first you need to:

  1. Need to import AVFoundation to project and #import into .h file as well then we can play sound with this.
  2. Need to put "AVAudioPlayer *myAudio;" without quotation marks of course somewhere on top (usually on top of viewDidLoad).

Then just...

-(IBAction)playButtonPressed:(id)sender {

    NSURL *yourMusicFile;
    yourMusicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"your_Song_Name" ofType:@"mp3"]];

    myAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
    [myAudio play];
    NSLog(@"Button -playButtonPressed- has been pressed!");
}
你与昨日 2024-11-18 04:11:58

嗯,我的解决方案来自经验。如果需要的话,我可以快速制定一个项目。而且,它还需要使用斯坦福大学的 MoMu API。它涉及创建 WvIn 和 WvOut 对象来读取文件。这些对象的音频样本只需输入到输出缓冲区即可同时播放文件。虽然API使用了AVFoundation,但是在这个项目中并没有明确使用AVFoundation。

Well, my solution comes out of experience. I can quickly cook up a project if needed. But also, it requires the use of an MoMu API at Stanford. It involves creating WvIn and WvOut objects for reading the files. The audio samples of these objects simply need to be fed to the output buffer to play the files simultaneously. Although the API uses AVFoundation, there is no explicit use of AVFoundation in this project.

清君侧 2024-11-18 04:11:58

基本上就像其他人所说的那样,确保为每个源创建一个音频播放器。

您还必须做的是对所有玩家对象保持强有力的引用。

如果您不这样做,它们就会被释放并停止播放。

当我只保留对我想播放的最后一个源的引用时,我遇到了这个问题。这意味着我只会听到最后一个数据源,并且我认为问题与同时播放配置有关 - 但实际上,这只是其他播放器被释放,因此播放会停止。

 try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, options: .mixWithOthers)
            try? AVAudioSession.sharedInstance().setActive(true)

在创建和播放音频源之前,我还使用了上面的代码片段。

Basically what everyone else is saying, make sure you create an audio player for each source.

What you also must do is KEEP A STRONG REFERENCE TO ALL THE PLAYER OBJECTS.

If you don't do this they get released and playback stops.

I had this issue when I was only keeping a reference to the last source I wanted to play back. This meant that I would only hear the last data source and I thought the issue was something to do with the simultaneous playback configuration - but in reality it was just the other players were being dealloc'd and thus playback would stop.

 try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, options: .mixWithOthers)
            try? AVAudioSession.sharedInstance().setActive(true)

I also used the above snippet before creating and playing back my audio sources.

ぇ气 2024-11-18 04:11:58

我们可以使用带有多个音频资源的AVMutableComposition来制作mixComposition,然后在AVPlayer上播放。就像:

var player: AVPlayer?

func addMultipleTrack(videoURL1: URL, videoURL2: URL, videoURL3: URL) {
    
    let asset1 = AVURLAsset(url: videoURL1)
    let asset2 = AVURLAsset(url: videoURL2)
    let asset3 = AVURLAsset(url: videoURL3)
   
    
    let composition = AVMutableComposition()
    var timeRange = CMTimeRange(start: .zero, duration: asset1.duration)
    
    if let audioAssetTrack1 = asset1.tracks(withMediaType: .audio).first,
       let compositionAudioTrack1 = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid) {
        
        do {
            try compositionAudioTrack1.insertTimeRange(timeRange, of: audioAssetTrack1, at: .zero)
        }catch {
            print(error)
            return
        }
    }
    
    
    timeRange = CMTimeRange(start: .zero, duration: asset2.duration)
    
    if let audioAssetTrack2 = asset2.tracks(withMediaType: .audio).first,
       let compositionAudioTrack2 = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid) {
        
        do {
            try compositionAudioTrack2.insertTimeRange(timeRange, of: audioAssetTrack2, at: .zero)
        }catch {
            print(error)
            return
        }
    }
    
    
    timeRange = CMTimeRange(start: .zero, duration: asset3.duration)
    if let audioAssetTrack3 = asset3.tracks(withMediaType: .audio).first,
       let compositionAudioTrack3 = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid) {
        
        do {
            try compositionAudioTrack3.insertTimeRange(timeRange, of: audioAssetTrack3, at: .zero)
        }catch {
            print(error)
            return
        }
        
    }
    
    let item = AVPlayerItem(asset: composition)
    
    player = AVPlayer(playerItem: item)
    
    let layer = AVPlayerLayer(player: player!)
    
    tempView.layer.addSublayer(layer) // it just hold layer but not need to show ,So you can hide in your won way
    player?.play()
    
}

We can make mixComposition with AVMutableComposition with multiple audio asset, then play on AVPlayer. like as:

var player: AVPlayer?

func addMultipleTrack(videoURL1: URL, videoURL2: URL, videoURL3: URL) {
    
    let asset1 = AVURLAsset(url: videoURL1)
    let asset2 = AVURLAsset(url: videoURL2)
    let asset3 = AVURLAsset(url: videoURL3)
   
    
    let composition = AVMutableComposition()
    var timeRange = CMTimeRange(start: .zero, duration: asset1.duration)
    
    if let audioAssetTrack1 = asset1.tracks(withMediaType: .audio).first,
       let compositionAudioTrack1 = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid) {
        
        do {
            try compositionAudioTrack1.insertTimeRange(timeRange, of: audioAssetTrack1, at: .zero)
        }catch {
            print(error)
            return
        }
    }
    
    
    timeRange = CMTimeRange(start: .zero, duration: asset2.duration)
    
    if let audioAssetTrack2 = asset2.tracks(withMediaType: .audio).first,
       let compositionAudioTrack2 = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid) {
        
        do {
            try compositionAudioTrack2.insertTimeRange(timeRange, of: audioAssetTrack2, at: .zero)
        }catch {
            print(error)
            return
        }
    }
    
    
    timeRange = CMTimeRange(start: .zero, duration: asset3.duration)
    if let audioAssetTrack3 = asset3.tracks(withMediaType: .audio).first,
       let compositionAudioTrack3 = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid) {
        
        do {
            try compositionAudioTrack3.insertTimeRange(timeRange, of: audioAssetTrack3, at: .zero)
        }catch {
            print(error)
            return
        }
        
    }
    
    let item = AVPlayerItem(asset: composition)
    
    player = AVPlayer(playerItem: item)
    
    let layer = AVPlayerLayer(player: player!)
    
    tempView.layer.addSublayer(layer) // it just hold layer but not need to show ,So you can hide in your won way
    player?.play()
    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文