gpt4 book ai didi

iphone - iOS AVPlayer 流媒体音乐问题

转载 作者:可可西里 更新时间:2023-11-01 05:00:16 24 4
gpt4 key购买 nike

我在使用 AVPlayer 时遇到了一些非常奇怪的问题。我有一个非常简单的音乐播放器(它从 iTunes 商店流式传输示例音乐)在模拟器(iPhone 和 iPad with iOS 5.1)上正确使用它,但它在真实设备上表现异常。

在装有 iOS 5.1.1 的 iPad 2 上,即使我将耳机连接到设备,它也能正常播放。但是一旦我断开它们,它就不会通过扬声器播放声音(即使我再次连接它们我可以听这首歌)。

在安装了 iOS 5.1 的 iPhone 4 上,它似乎根本无法通过扬声器播放,但我可以通过耳机收听音乐。虽然它似乎不是通过扬声器播放的,但我偶尔可以听到音乐播放的一小段时间(并且可以确认它实际上正在播放,因为我的 UI 会做出相应的响应),尽管它似乎是随机的。

我正在使用 AVPlayer 因为它似乎符合要求。我应该使用另一个库吗?我需要手动路由声音吗?为什么我会遇到这些类型的问题?我是否正确使用了 Audio Session ?

媒体.h:

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

#define NOT_PLAYING -1

@protocol MediaDelegate <NSObject>
@optional
- (void) didEndPlayingAtIndex:(int) index;
- (void) startedToPlayAtIndex:(int) index;
- (void) stoppedToPlayAtIndex:(int) index;
- (void) startedToPlayAtIndex:(int) to fromIndex:(int) from;
- (void) pausedAtIndex:(int) index;
@end

@interface Media : NSObject <AVAudioSessionDelegate>

@property (nonatomic, assign) int currentItem;
@property (nonatomic, strong) NSURL *url;
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, assign) id<MediaDelegate> delegate;

- (void) toggle:(int) index;
- (void) stop;

@end

媒体.m:

#import "Media.h"

@implementation Media

@synthesize currentItem;
@synthesize player;
@synthesize delegate;
@synthesize url;

- (id)init
{
self = [super init];
if (self) {
NSError *activationError = nil;
NSError *setCategoryError = nil;
AVAudioSession *session = [AVAudioSession sharedInstance];
session.delegate = self;
[session setActive:YES error:&activationError];
[session setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];

if(activationError || setCategoryError)
NSLog(@"ERROR: %@, %@",activationError,setCategoryError);

self.currentItem = NOT_PLAYING;
}
return self;
}

- (void)dealloc{
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive:NO error:&activationError];

if(activationError)
NSLog(@"ERROR: %@",activationError);

[self.player removeObserver:self forKeyPath:@"status"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
switch (player.status) {
case AVPlayerItemStatusReadyToPlay:
[self.player play];
if([self.delegate respondsToSelector:@selector(startedToPlayAtIndex:)])
[self.delegate startedToPlayAtIndex:self.currentItem];
break;
default:
break;
}
}

- (void) toggle:(int) index{
if (self.currentItem == NOT_PLAYING) {
self.player = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithURL:self.url]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:self.player];
self.currentItem = index;
[self.player addObserver:self forKeyPath:@"status" options:0 context:nil];
}
else {
if (self.currentItem == index) {
[self.player pause];
if([self.delegate respondsToSelector:@selector(stoppedToPlayAtIndex:)])
[self.delegate stoppedToPlayAtIndex:index];
self.currentItem = NOT_PLAYING;
[self.player removeObserver:self forKeyPath:@"status"];
self.player = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
else{
[self.player replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithURL:self.url]];
if([self.delegate respondsToSelector:@selector(startedToPlayAtIndex:fromIndex:)])
[self.delegate startedToPlayAtIndex:index fromIndex:self.currentItem];
self.currentItem = index;
}
}
}

- (void) stop{
[self.player pause];
if([self.delegate respondsToSelector:@selector(stoppedToPlayAtIndex:)])
[self.delegate stoppedToPlayAtIndex:self.currentItem];
}

-(void) didEnd:(id)sender{
if([self.delegate respondsToSelector:@selector(didEndPlayingAtIndex:)])
[self.delegate didEndPlayingAtIndex:self.currentItem];
self.currentItem = NOT_PLAYING;
[self.player removeObserver:self forKeyPath:@"status"];
self.player = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - AVAudioSession delegate

-(void)beginInterruption{
NSLog(@"Interruption");
if(self.currentItem != NOT_PLAYING){
[self.player pause];
if([self.delegate respondsToSelector:@selector(pausedAtIndex:)])
[self.delegate pausedAtIndex:self.currentItem];
}
}

-(void)endInterruption{
NSLog(@"Ended interruption");
if(self.currentItem != NOT_PLAYING){
[self.player play];
if([self.delegate respondsToSelector:@selector(startedToPlayAtIndex:)])
[self.delegate startedToPlayAtIndex:self.currentItem];
}
}

@end

最佳答案

看起来您在正确的音频路由方面遇到了问题。我建议添加一个音频路由更改监听器回调。首先声明一个方法:

void audioRouteChangeListenerCallback(void *inUserData, AudioSessionPropertyID inPropertyID, UInt32 inPropertyValueSize, const void *inPropertyValue);

然后在你的初始化方法中添加一个回调:

// Prevent from audio issues when you pull out earphone
AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, audioRouteChangeListenerCallback, (__bridge void *)(self));

最后,添加一个回调实现:

void audioRouteChangeListenerCallback(void *inUserData, AudioSessionPropertyID inPropertyID, UInt32 inPropertyValueSize, const void *inPropertyValue) {
if (inPropertyID != kAudioSessionProperty_AudioRouteChange) {
return;
}

CFDictionaryRef routeChangeDictionary = inPropertyValue;
CFNumberRef routeChangeReasonRef = CFDictionaryGetValue(routeChangeDictionary, CFSTR(kAudioSession_AudioRouteChangeKey_Reason));
SInt32 routeChangeReason;
CFNumberGetValue(routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);

if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {
// Headset is unplugged..
NSLog(@"Headset is unplugged..");
// Delayed play: 0.5 s.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5f * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[CORE.player play]; // NOTE: change this line according your current player implementation
NSLog(@"resumed play");
});
}
if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable) {
// Headset is plugged in..
NSLog(@"Headset is plugged in..");
}
}

希望对您有所帮助!至少对于其他人来说,这将是有用的提示。

关于iphone - iOS AVPlayer 流媒体音乐问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10515182/

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