mp3文件后台播放问题,很久都找不到原因

感谢大家关注,自己找到问题的原因了。

虚拟器不支持按下"Home"后继续播放,真机Ok!




在按下设备的 “home”键后,程序在后台运行,希望继续播放乐曲。

各位老大帮看看我哪里做错了?
或缺少什么代码?谢谢。


我已经进行了如下设置:

在程序启动时
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

// Override point for customization after application launch.
//后台运行音乐的尝试
UIDevice* device = [UIDevice currentDevice];
BOOL backgroundSupported = NO;
if ([device respondsToSelector:@selector(isMultitaskingSupported)])
backgroundSupported = device.multitaskingSupported;
NSLog(@\"backgroundSupported[%@]\",backgroundSupported ? @\"YES\" : @\"NO\");

[[AVAudioSession sharedInstance] setDelegate:self];
NSError *audioSessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&audioSessionError];
if (audioSessionError)
NSLog(@\"Error setting audio category: %@\", [audioSessionError localizedDescription]);

[[AVAudioSession sharedInstance] setActive:YES error:&audioSessionError];
if (audioSessionError)
NSLog(@\"Error setting audio active: %@\", [audioSessionError localizedDescription]);


按下home键的代码
- (void) applicationDidEnterBackground:(UIApplication *)application
{
//后台运行的尝试
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];

// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task.
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}


Info.plist局部内容:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>


播放乐曲调用:
NSError *_err;
AVAudioPlayer *_player;
NSString *file = [[NSString alloc ] initWithFormat:@\"/Users/hanmj/Hanmjmusic/halo.mp3\"];
_player = [ [AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:file ] error:&_err ];
[file release];
_player.volume = 1;
[_player play];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值