我可以在xcode的不同文件夹中使用相同的文件名,以及如何使用ios Swift语言访问它

时间:2014-11-10 07:03:10

标签: xcode audio swift ios8

我正在使用Swift语言在iOS 8中处理音频播放器。

我的音频文件名为“apple.mp3”,男声和女声都有相同的音频名称。这两个文件是两个不同的组,男性男性组和女性音频女性组。因此路径将是male / apple.mp3和female / apple.mp3。

我正在使用以下代码播放音频文件

    var audioPlayer = AVAudioPlayer()
    var currentAudioPath:NSURL!
    currentAudioPath = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(audioName, ofType: "mp3")!)

    AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
    AVAudioSession.sharedInstance().setActive(true, error: nil)
    UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
    audioPlayer = AVAudioPlayer(contentsOfURL: currentAudioPath, error: nil)
    audioPlayer.delegate = self
    audioPlayer.play()

    audioPlayer.prepareToPlay()

如何使用特定路径单独播放两个音频?

1 个答案:

答案 0 :(得分:5)

据我所知,如果项目中有任何地方的类似文件名,无论子文件夹有多深,Xcode都会抱怨重复的资源。 当你按名称要求一个图像时,Xcode'看到'整个项目,这是好的一面。

简而言之,不,您不能在同一个项目中拥有两个名称相同的资源。建议为每个人命名。