相关疑难解决方法(0)

使用AVAudioPlayer播放声音后恢复背景音频

在我的应用程序使用AVAudioPlayer播放自己的声音后,我正在寻找一种方法来恢复另一个应用程序播放的音频.现在,如果我正在听音乐并启动我的应用程序,它会暂停音乐,播放我的声音,但不会恢复背景音乐.

这是我正在做的生成声音播放:

myChime = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"chime" ofType:@"mp3"]] error:nil];
myChime.delegate = self;
[myChime play];
// Here I am looking to resume the audio that the system was playing before my application played its sound.  I have seen many applications do this such as, GPS apps that speak a voice direction and then resume the music.
Run Code Online (Sandbox Code Playgroud)

audio avaudioplayer ios

7
推荐指数
1
解决办法
5967
查看次数

如何从iPhone应用程序停止和恢复背景音频?

我在基于Messaging的iPhone应用程序中工作.我添加了Beep声音以接收来自其他人的消息.我正在使用嘟嘟声AVAudioPlayer.我的问题是当用户在后台从其他应用程序听到歌曲时,如果他们从我的应用程序接收到消息,则将发出哔声并且后台歌曲/音频停止不恢复.

我希望通过暂停背景歌曲/音频播放我的哔声,然后我必须从我的应用程序中恢复该音频/歌曲.在iOS开发中有可能吗?谁能帮帮我吗?

编辑:

这是我的示例代码.

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Music";
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

    NSString *urlString = [[NSBundle mainBundle] pathForResource:@"basicsound" ofType:@"wav"];
    NSURL *audioURL = [[NSURL alloc] initWithString:urlString];
    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
    [audioPlayer setDelegate:self];
    [audioPlayer prepareToPlay];

    audioSession = [AVAudioSession sharedInstance];
    [audioSession setActive:YES error:&error];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:&error];
    [audioPlayer play];    
}

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    if (flag == YES) 
    {
        NSLog(@"Audio Played successfully");
        NSError *error;
        [audioSession setActive:NO error:&error];
    }
}
Run Code Online (Sandbox Code Playgroud)

audio resume objective-c avaudioplayer ios

2
推荐指数
2
解决办法
1万
查看次数

标签 统计

audio ×2

avaudioplayer ×2

ios ×2

objective-c ×1

resume ×1