如何在iOS 4中后台播放多个音频文件?

发布于 2024-09-24 06:42:13 字数 369 浏览 6 评论 0原文

我使用 AVAudioPlayer 和 AVAudioSession 在 iPhone 应用程序的后台运行音频...

现在我的问题是我已经有了一个编写的代码,它接受用户输入的多首歌曲并使用 AVAudioPlayer 逐个播放它们。 问题是,

当应用程序在播放其中一个文件时进入后台时,音频会继续,但完成后,声音就会消失......在前台,当文件完成时,我只需执行一些代码即可播放。下一个,但在后台这是不可能的...

我考虑过请求一些时间来完成此操作并运行我的代码,但我不能这样做,因为音频文件总是会变化并且它们可能很长...所以请告诉我该怎么办?是否有作为背景音频运行的内置播放列表播放器?或者还有其他解决方案吗?我真的需要把它投入工作......

非常感谢

I ran audio in the background in my iPhone app using AVAudioPlayer and AVAudioSession...

Now my problem is that I already have a written code that takes a number of songs as input from the user and plays them one after the other using AVAudioPlayer...

The problem is that when the app goes to the background while one of the files is being played, the audio continues but when it's done, the sound disappears... In foreground when a file is done, I just do some code to play the next one but in the background this is not possible...

I thought about requesting some time to get this done and run my code but I can't do this as the audio files always change and they might be long... So please tell me what should I do? Is there any built-in playlist player that run as a background audio? or is there any other solution? I really need to get this into work...

thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫腻 2024-10-01 06:42:13

我遇到了同样的问题,但还没有完全解决。 audioDidFinishPlaying 委托方法仍然会触发,您可以使用 UIAplication 的 beginBackgroundTask 创建 AVaudioPlayer 的新实例,确保 AVAudioSession 仍然处于活动状态等。但是当我尝试播放新的 AVAudioPlayer 实例时,它会立即暂停。我怀疑/担心的是,无法从后台启动音频,只能在应用程序状态转换发生时保持播放。但我很愿意错。

I ran into the same issue and haven't completely resolved it yet. The audioDidFinishPlaying delegate method still fires, and you can use UIAplication's beginBackgroundTask to create a new instance of AVaudioPlayer, make sure AVAudioSession is still active, etc. But when I try to play the new AVAudioPlayer instance, it pauses immediately. My suspicion/fear is that there is no way to start the audio FROM the background, only keep it playing when the application state transition happens. I'd love to be wrong, though.

悸初 2024-10-01 06:42:13

这很容易..如果您使用的音频会话是...

http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategories/AudioSessionCategories.html

该链接具有类别...因此,使用其中之一,您就可以在后台启动声音:
AVAudioSessionCategory回放
kAudioSessionCategory_MediaPlayback

  • 环境播放可能不起作用。

this is easy ..just change the cateogry if your using an audio session that is ...

http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategories/AudioSessionCategories.html

the link has the categories ...so use one of these and you be able to start the sound in the background:
AVAudioSessionCategoryPlayback
kAudioSessionCategory_MediaPlayback

  • the ambient one might not work.
凉风有信 2024-10-01 06:42:13
//ViewController.h ,write below code
@interface ViewController :   UIViewController<AVAudioRecorderDelegate,AVAudioPlayerDelegate>

//assign property to player
 @property(nonatomic,retain) AVAudioPlayer *player;

//then write in ViewController.m file in ViewDidLoad Method below code

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

NSError *soundError;
NSString *path=[[NSBundle mainBundle]pathForResource:@"soundFileName" ofType:@"mp3"]; //.mp3 file for player
NSURL *file=[[NSURL alloc]initFileURLWithPath:path]; //path
_player=[[AVAudioPlayer alloc]initWithContentsOfURL:file error:&soundError];       //player Object

if(_player == nil)
{
    NSLog(@"player is empty because of %@",soundError);
}
else
{
    [_player play];
    _player.volume=1.0;
    [_player setDelegate:self];
}

// 对于停止播放器,您可以使用
// [_player 停止]; //当你想停止它时取消注释这一行。

//ViewController.h ,write below code
@interface ViewController :   UIViewController<AVAudioRecorderDelegate,AVAudioPlayerDelegate>

//assign property to player
 @property(nonatomic,retain) AVAudioPlayer *player;

//then write in ViewController.m file in ViewDidLoad Method below code

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

NSError *soundError;
NSString *path=[[NSBundle mainBundle]pathForResource:@"soundFileName" ofType:@"mp3"]; //.mp3 file for player
NSURL *file=[[NSURL alloc]initFileURLWithPath:path]; //path
_player=[[AVAudioPlayer alloc]initWithContentsOfURL:file error:&soundError];       //player Object

if(_player == nil)
{
    NSLog(@"player is empty because of %@",soundError);
}
else
{
    [_player play];
    _player.volume=1.0;
    [_player setDelegate:self];
}

// for stop player you can use
// [_player stop]; //uncomment this line when you wants to stop it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文