【问题标题】:how to restart the music player after play a sound in my app in xcode如何在 xcode 中的应用程序中播放声音后重新启动音乐播放器
【发布时间】:2014-01-18 10:13:06
【问题描述】:

使用 AVAudioPlayer 播放我的声音时,后台的音乐播放器会停止,当我的声音结束时,音乐播放器无法恢复,我该如何恢复音乐播放器?

【问题讨论】:

  • 你有自己的音乐播放器还是原生的?
  • 原生音乐播放器应用
  • 这可能对你有帮助stackoverflow.com/questions/5549756/…
  • 对不起,但我不明白如何恢复在我的声音播放之前播放的原生音乐播放器应用程序的轨道
  • 所以澄清一下,您的音乐播放器是否在播放声音,然后停止播放第二个声音?如果是这样,您是否使用 AVAudioPlayer 来播放两种声音(或 2 个 AVAudioPlayer)?

标签: ios objective-c xcode avaudioplayer


【解决方案1】:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController () <AVAudioPlayerDelegate>

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"ttt" ofType:@"mp3"]] error:nil];
_audioPlayer.delegate = self;
}

- (void)play:(id)sender
{
    [_audioPlayer play];
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    NSError *error = nil;
    if (![audioSession setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error]) {
        NSLog(@"resume music player failed, error=%@", error);
    }
}

@end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    相关资源
    最近更新 更多