gpt4 book ai didi

audio - 如何支持在后台播放音频文件

转载 作者:行者123 更新时间:2023-12-03 02:19:33 24 4
gpt4 key购买 nike

我的项目中有一个功能,即使在后台状态下也支持播放新的音频文件。我使用MPMoviePlayerController类来播放音频文件。
但是我发现它是在 Activity 状态下启动的,然后它将在后台状态下继续播放相同的音频(如果音频文件未完成);但是在后台状态下不会启动新的音频。

如何支持在backgound状态下播放新音频?
我发现,当设置runloop或随机播放模式时,iphone的音乐播放器可以在后台状态下播放新音频。

以下是我的代码以证明实际上的外观:

(void)applicationDidEnterBackground:(UIApplication *)application
{

if (pCotnroller1) {
[pCotnroller1 release];
}
pCotnroller1 = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://r.yhiker.com:9001/0086/32/05/060009/Data/6/00863205060009006_24Kbps.mp3"]];
pCotnroller1.shouldAutoplay = YES;
[pCotnroller1 play];
//it wasn't played here
}


(void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
if (pCotnroller1) {
switch (pCotnroller1.playbackState) {
case MPMoviePlaybackStateStopped:
NSLog(@"播放地址");
break;

case MPMoviePlaybackStatePaused:
NSLog(@"播放暂停");
break;

case MPMoviePlaybackStateInterrupted:
NSLog(@"播放中断");
break;

case MPMoviePlaybackStateSeekingBackward:
break;

case MPMoviePlaybackStateSeekingForward:
break;

default:
break;
}

if (pCotnroller1.playbackState == MPMoviePlaybackStatePlaying) {
//it will come here,and play autoed here

// [pCotnroller1 play];
}
else
{

}
}
}

最佳答案

我尝试了一些在网上找到的方法,但是没有用。有人用这种方法解决了他的问题。我不知道为什么。

以下是我的代码:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
[application beginReceivingRemoteControlEvents];
[self updateUserLocation];
}

- (void)updateUserLocation
{
if (!pCotnroller1) {
// pCotnroller1 = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://r.yhiker.com:9001/0086/32/05/PP0041/Data/13/00863205PP0041013_24Kbps.mp3"]];

NSString *audioDiskPath = [HelpTools returnSpotAudioDiskPath:@"00863205PP0041013"];
pCotnroller1 = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:audioDiskPath/*@"/var/mobile/Applications/E7A06CB2-0856-407D-ABA1-2A846B65CD9D/Library/Caches/Data/0086/32/05/PP0041/Data/13/00863205060005016_24Kbps.mp3"*/]];

pCotnroller1.shouldAutoplay = YES;
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
[pCotnroller1 play];
NSLog(@"进入后台");
UIApplication *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier taskID = [app beginBackgroundTaskWithExpirationHandler:nil];
if (bgTask != UIBackgroundTaskInvalid) {
[app endBackgroundTask:bgTask];
NSLog(@"允许后台播放");
}

bgTask = taskID;
}
else
{
NSLog(@"飞后台进行播放");
[pCotnroller1 play];
}
}
}

控制台打印:
2013-03-18 16:20:11.844 Sz [1665:907] [MPAVController]自动播放:为暂停禁用自动播放
2013-03-18 16:20:11.845 Sz [1665:907] [MPAVController]自动播放:禁用自动播放
2013-03-18 16:20:13.489 Sz [1665:907]文件存在
2013-03-18 16:20:18.017 Sz [1665:907] [MPAVController]自动播放:跳过自动播放,已禁用(对于当前项目:1,播放器:0)
2013-03-18 16:20:18.998 Sz [1665:907]进入后台
2013-03-18 16:20:21.269 Sz [1665:907]播放地址
2013-03-18 16:20:26.251 Sz [1665:907] [MPAVController]自动播放:启用自动播放
2013-03-18 16:20:26.307 Sz [1665:907] [MPAVController]自动播放:可能会保持或充满缓冲区:0
2013-03-18 16:20:26.309 Sz [1665:907] [MPAVController]自动播放:跳过自动播放,缓冲不足以跟上。
2013-03-18 16:20:26.319 Sz [1665:907] [MPAVController]自动播放:启用自动播放
2013-03-18 16:20:26.338 Sz [1665:907] [MPCloudAssetDownloadController]媒体项目ID:0的优先级请求

关于audio - 如何支持在后台播放音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15470970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com