V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
parkman
V2EX  ›  iDev

iOS 如何让音频文件不要同时播放。

  •  
  •   parkman · 2014-07-09 20:50:17 +08:00 · 4627 次点击
    这是一个创建于 3585 天前的主题,其中的信息可能已经有所发展或是发生改变。
    下列代码SongA 与SongB同时播放了,有没有办法让它们不要同时播放。


    NSString *songA = [[NSBundle mainBundle] pathForResource:@"songA" ofType:@"mp3"];
    NSError *soundError = nil;
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:songA] error:&soundError];
    if(self.player == nil)
    NSLog(@"%@",soundError);
    else
    {
    [self.player setDelegate:self];
    [self.player setVolume:0.75];
    [self.player play];
    }

    NSString *songB = [[NSBundle mainBundle] pathForResource:@"songB" ofType:@"mp3"];
    soundError = nil;
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:songB] error:&soundError];
    if(self.player == nil)
    NSLog(@"%@",soundError);
    else
    {
    [self.player setDelegate:self];
    [self.player setVolume:0.25];
    [self.player play];
    }
    8 条回复    2014-07-11 15:55:49 +08:00
    jevonszmx
        1
    jevonszmx  
       2014-07-09 22:28:30 +08:00
    单例模式啊。。。
    parkman
        2
    parkman  
    OP
       2014-07-10 10:01:11 +08:00
    @jevonszmx 设置什么类 单根了 。
    PrideChung
        3
    PrideChung  
       2014-07-10 11:22:38 +08:00
    这问题在卖萌么
    把这行以及在其之后的代码删掉:
    NSString *songB = [[NSBundle mainBundle] pathForResource:@"songB" ofType:@"mp3"];
    sobigfish
        4
    sobigfish  
       2014-07-10 13:09:35 +08:00
    @PrideChung 呃,lz是想播放完songA放songB吧
    PrideChung
        5
    PrideChung  
       2014-07-10 13:44:51 +08:00
    @sobigfish 我仔细审题了,没看出楼主有这样的意思
    jevonszmx
        6
    jevonszmx  
       2014-07-10 17:54:46 +08:00
    @PrideChung
    @sobigfish
    @parkman

    额,那我回答少了:
    1、首先播放音乐,要写成单例类,保证同一时间只能有一个音乐在播放;
    2、有个播放队列,songA、songB依次加入播放队列,然后才开始播放,监听播放进度,播完songA再songB,循环??

    对吗?其实我也是新手XD
    parkman
        7
    parkman  
    OP
       2014-07-11 15:52:34 +08:00
    @jevonszmx
    @sobigfish
    @PrideChung

    我自己重新换了一种思路, @PrideChung 设置一个专门用来播放的队列,不断的更新播放的当前文件,并及时做队列的删除与插入,使用delegate 方法来播放当前的队列。从播放队列中清除当前的文件。
    parkman
        8
    parkman  
    OP
       2014-07-11 15:55:49 +08:00
    @jevonszmx
    @sobigfish
    @PrideChung
    这个方法控制播放下一个audio
    - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2031 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 01:19 · PVG 09:19 · LAX 18:19 · JFK 21:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.