【问题标题】:AVAudioPlayer Play/Pause/Stop local audio file on three different buttonsAVAudioPlayer 在三个不同的按钮上播放/暂停/停止本地音频文件
【发布时间】:2016-02-22 18:49:15
【问题描述】:

我想播放/暂停/停止本地音频文件。所有功能的按钮都不同。检查随附的屏幕截图以供参考。

下面是我的点击事件,

-(IBAction)click_Play:(id)sender {
        NSString *path;
        NSURL *url;

        //where you are about to add sound
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

        path =[[NSBundle mainBundle] pathForResource:@"MySong" ofType:@"mp3"];
        url = [NSURL fileURLWithPath:path];

        player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
        [player setVolume:1.0];
        [player prepareToPlay];
        [player play];

}

-(IBAction)click_Pause:(id)sender {
    [player pause];
}

-(IBAction)click_Stop:(id)sender {
    [player stop];
}

在这里,我遇到了暂停功能的问题。当我再次单击播放按钮时暂停音频后。它从一开始就开始播放,而不是在我暂停它的地方。有什么解决办法吗? 提前致谢!

【问题讨论】:

    标签: objective-c avaudioplayer


    【解决方案1】:

    将您的代码更改为:

    -(IBAction)click_Play:(id)sender {
        if (!player)
        {
            NSString *path;
            NSURL *url;
    
            //where you are about to add sound
            AVAudioSession *audioSession = [AVAudioSession sharedInstance];
            [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
    
            path =[[NSBundle mainBundle] pathForResource:@"MySong" ofType:@"mp3"];
            url = [NSURL fileURLWithPath:path];
    
            player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
            [player setVolume:1.0];
            [player prepareToPlay];
            [player play];
        }
        else {
            [player play];
        }
    }
    

    【讨论】:

    • 嘿!非常感谢!。完美运行! (Y)
    【解决方案2】:

    不要在播放按钮操作中一次又一次地创建AVAudioSessionAVAudioPlayer 的实例。所以,保持简单,在viewDidLoad 中创建AVAudioSessionAVAudioPlayer 的实例

    - (void)viewDidLoad {
        [super viewDidLoad];
        NSString *path;
        NSURL *url;
    
        //where you are about to add sound
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
    
        path =[[NSBundle mainBundle] pathForResource:@"MySong" ofType:@"mp3"];
        url = [NSURL fileURLWithPath:path];
    
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
        [player setVolume:1.0];
        [player prepareToPlay];
    } 
    
    -(IBAction)click_Play:(id)sender {
        [player play];
    }
    
    -(IBAction)click_Pause:(id)sender {
        [player pause];
    }
    
    -(IBAction)click_Stop:(id)sender {
        [player stop];
    }
    

    编辑: 当您更改曲目时调用此方法。我永远不会建议一次又一次地创建对象实例

    -(void)changeTrackWithPath:(NSString *)path{
        AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:path]];
        [player replaceCurrentItemWithPlayerItem:playerItem];
        [player play];
    }
    

    【讨论】:

    • 嘿!谢谢你的帮助。但我在问题中添加的代码只是一个例子。我的音频每次都会根据用户的选择进行更改。所以我无法在 viewDidLoad 中添加这段代码。
    • @Gati 你可以调用这个方法和轨道的路径来改变轨道
    • 嘿,这个方法调用出现错误,replaceCurrentItemWithPlayerItem
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    相关资源
    最近更新 更多