iPhone 应用程序:未在 AVAudio Player 中恢复音频会话

发布于 2024-12-06 20:22:22 字数 6902 浏览 2 评论 0 原文

在我的 iPhone 应用程序中,我使用 AVAudioPlayer 播放音乐。当我的应用程序在后台运行时,它也在运行,因为我

在 AppDelegate 中编写了这段代码,

-- application didFinishLaunchingWithOptions

  audioManager= [[AudioManager alloc] init];
  audioManager.delegate=self;
   audioManager.soundFile=[[[NSBundle mainBundle] pathForResource:@"mysong" ofType:@"wav"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  [audioManager setFileForPlay];
  [audioManager.player setNumberOfLoops:-1];
  [audioManager.player setVolume:.5];
  [audioManager play];



 -(void) playerBeginInterruption:(AVAudioPlayer *)p{


[usdef setValue:@"START" forKey:@"LOCATIONMANAGER"];

[locationManager startUpdatingLocation];
[p pause];
// audioManager=nil;

  }

 -(void) playerBeginInterruptionInBackground:(AVAudioPlayer *)p{

 [usdef setValue:@"START" forKey:@"LOCATIONMANAGER"];
[locationManager startUpdatingLocation];
[p pause];
//audioManager=nil;

}

 -(void) playerEndInterruption:(AVAudioPlayer *)p{
 NSLog(@"Inteerpt END");

  [p prepareToPlay];
  [p play];
  [usdef setValue:@"STOP" forKey:@"LOCATIONMANAGER"];



}

 -(void) playerEndInterruptionInBackgroud:(AVAudioPlayer *)p{
  NSLog(@"Inteerpt END BG");
 // [self performSelector:@selector(allocaudioManager)];  
  [p prepareToPlay];
  [p play];

  [usdef setValue:@"STOP" forKey:@"LOCATIONMANAGER"];
  // [locationManager stopUpdatingLocation];

}

现在我已经为处理委托方法的音频管理器创建了单独的类。

在 .h 文件中

  #import <Foundation/Foundation.h>
  #import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
 @protocol AudioManagerDelegate;
 @interface AudioManager : NSObject <AVAudioPlayerDelegate> {
AVAudioPlayer                       *player;

NSString                            *soundFile;
BOOL                                inBackground;
id<AudioManagerDelegate>            delegate;

 }
@property (nonatomic, assign)   id<AudioManagerDelegate>delegate;
@property (nonatomic, retain)   NSString        *soundFile;

@property (nonatomic, assign)   AVAudioPlayer   *player;
@property (nonatomic, assign)   BOOL            inBackground;
 - (void)play;
 - (void)pause;
 - (void)stop;
 - (void)registerForBackgroundNotifications;
 -( void) setFileForPlay;
  @end
  @protocol AudioManagerDelegate <NSObject>
  @optional
   - (void)playerDidFinishPlayingInBackground:(AVAudioPlayer*)p;
  - (void)playerDidFinishPlaying:(AVAudioPlayer*)p;
  - (void)playerBeginInterruption:(AVAudioPlayer*)p;
  - (void)playerBeginInterruptionInBackground:(AVAudioPlayer*)p;
  - (void)playerEndInterruption:(AVAudioPlayer *)p;
   - (void)playerEndInterruptionInBackgroud:(AVAudioPlayer *)p;
 @end

在 .m 文件中

    @implementation AudioManager
  @synthesize delegate;
  @synthesize player;
  @synthesize inBackground;
  @synthesize soundFile;
  
   void RouteChangeListener(void *inClientData, AudioSessionPropertyID  inID,UInt32 inDataSize, const void *inData);

 -(id) init
{
     if((self=[super init]))
    {
    
    OSStatus result = AudioSessionInitialize(NULL, NULL, NULL, NULL);
    if (result)
        NSLog(@"Error initializing audio session! %ld", result);
    
    [[AVAudioSession sharedInstance] setDelegate: self];
    NSError *setCategoryError = nil;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
    if (setCategoryError)
        NSLog(@"Error setting category! %@", setCategoryError);
    
    result = AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange, RouteChangeListener, self);
    if (result) 
        NSLog(@"Could not add property listener! %ld", result);
    
    return self;
}
return nil;
 }


 -(void) setFileForPlay{
     self.player=nil;
       player = [[AVAudioPlayer alloc]   initWithData:[NSData dataWithContentsOfFile:self.soundFile] error:nil];//initWithContentsOfURL:[NSURL URLWithString:self.soundFile]  error:nil];

    if (self.player)
     {
          player.delegate = self;
      }

   }


 -(void) play{

  [self.player play];

  }
  -(void)pause{
    [self.player pause];
    NSLog(@"in Pause");
  }
 -(void)stop{
    [self.player stop];
     NSLog(@"in stop");
  }



              void RouteChangeListener(void *inClientData, AudioSessionPropertyID   inID,UInt32 inDataSize, const void *inData){
      AudioManager* This = (AudioManager*)inClientData;

        if (inID == kAudioSessionProperty_AudioRouteChange) {
    
        CFDictionaryRef routeDict = (CFDictionaryRef)inData;
    NSNumber* reasonValue = (NSNumber*)CFDictionaryGetValue(routeDict, CFSTR(kAudioSession_AudioRouteChangeKey_Reason));
    
    int reason = [reasonValue intValue];
    
    if (reason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {
         
        [This stop];
    }
}

  }
   - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)p successfully:(BOOL)flag
    {
if (flag == NO)
    NSLog(@"Playback finished unsuccessfully");

[p setCurrentTime:0.];
if (inBackground)
{
    [delegate playerDidFinishPlayingInBackground:p];
}
else
{
    [delegate playerDidFinishPlaying:p];
}
    }
     - (void)playerDecodeErrorDidOccur:(AVAudioPlayer *)p error:(NSError *)error
   {
NSLog(@"ERROR IN DECODE: %@\n", error); 
    }
  // we will only get these notifications if playback was interrupted
  - (void)audioPlayerBeginInterruption:(AVAudioPlayer *)p
    {
NSLog(@"Interruption begin ");
// the object has already been paused,  we just need to update UI
if (inBackground)
{
    [delegate playerBeginInterruptionInBackground:p];
}
else
{
    [delegate playerBeginInterruption:p];
}

    }


     - (void)audioPlayerEndInterruption:(AVAudioPlayer *)p withFlags:(NSUInteger)flags
    {
    if(inBackground)
      {
      [delegate playerEndInterruptionInBackgroud:p];
    }
        else
    {
    [delegate playerEndInterruption:p];
    }
        NSLog(@"Interruption ended. Resuming playback");
     }

    - (void)registerForBackgroundNotifications
    {
[[NSNotificationCenter defaultCenter] addObserver:self
                                                    selector:@selector(setInBackgroundFlag)
                                                   name:UIApplicationWillResignActiveNotification
                                             object:nil];

      [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(clearInBackgroundFlag)
                                               name:UIApplicationWillEnterForegroundNotification
                                           object:nil];
  }

 - (void)setInBackgroundFlag
   {
inBackground = true;
   }

- (void)clearInBackgroundFlag
   {
inBackground = false;
   }

   @end

现在的问题是,如果我正在播放 iPod 库中的任何歌曲并停止该歌曲并关闭 iPod 库,我在启动 ipos 库之前播放的原始 avaudioplayer 不会恢复意味着它没有播放歌曲再次。

可能出什么问题了?

是否有任何可以提供帮助的演示项目或示例代码?

In my iPhone app I am playing music using AVAudioPlayer. When my app runs in background that time also it is running for that I wrote this code

in AppDelegate on

-- application didFinishLaunchingWithOptions

  audioManager= [[AudioManager alloc] init];
  audioManager.delegate=self;
   audioManager.soundFile=[[[NSBundle mainBundle] pathForResource:@"mysong" ofType:@"wav"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  [audioManager setFileForPlay];
  [audioManager.player setNumberOfLoops:-1];
  [audioManager.player setVolume:.5];
  [audioManager play];



 -(void) playerBeginInterruption:(AVAudioPlayer *)p{


[usdef setValue:@"START" forKey:@"LOCATIONMANAGER"];

[locationManager startUpdatingLocation];
[p pause];
// audioManager=nil;

  }

 -(void) playerBeginInterruptionInBackground:(AVAudioPlayer *)p{

 [usdef setValue:@"START" forKey:@"LOCATIONMANAGER"];
[locationManager startUpdatingLocation];
[p pause];
//audioManager=nil;

}

 -(void) playerEndInterruption:(AVAudioPlayer *)p{
 NSLog(@"Inteerpt END");

  [p prepareToPlay];
  [p play];
  [usdef setValue:@"STOP" forKey:@"LOCATIONMANAGER"];



}

 -(void) playerEndInterruptionInBackgroud:(AVAudioPlayer *)p{
  NSLog(@"Inteerpt END BG");
 // [self performSelector:@selector(allocaudioManager)];  
  [p prepareToPlay];
  [p play];

  [usdef setValue:@"STOP" forKey:@"LOCATIONMANAGER"];
  // [locationManager stopUpdatingLocation];

}

Now I have created separate classes for audio manager which handles the delegate methods.

in .h file

  #import <Foundation/Foundation.h>
  #import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
 @protocol AudioManagerDelegate;
 @interface AudioManager : NSObject <AVAudioPlayerDelegate> {
AVAudioPlayer                       *player;

NSString                            *soundFile;
BOOL                                inBackground;
id<AudioManagerDelegate>            delegate;

 }
@property (nonatomic, assign)   id<AudioManagerDelegate>delegate;
@property (nonatomic, retain)   NSString        *soundFile;

@property (nonatomic, assign)   AVAudioPlayer   *player;
@property (nonatomic, assign)   BOOL            inBackground;
 - (void)play;
 - (void)pause;
 - (void)stop;
 - (void)registerForBackgroundNotifications;
 -( void) setFileForPlay;
  @end
  @protocol AudioManagerDelegate <NSObject>
  @optional
   - (void)playerDidFinishPlayingInBackground:(AVAudioPlayer*)p;
  - (void)playerDidFinishPlaying:(AVAudioPlayer*)p;
  - (void)playerBeginInterruption:(AVAudioPlayer*)p;
  - (void)playerBeginInterruptionInBackground:(AVAudioPlayer*)p;
  - (void)playerEndInterruption:(AVAudioPlayer *)p;
   - (void)playerEndInterruptionInBackgroud:(AVAudioPlayer *)p;
 @end

in .m file

    @implementation AudioManager
  @synthesize delegate;
  @synthesize player;
  @synthesize inBackground;
  @synthesize soundFile;
  
   void RouteChangeListener(void *inClientData, AudioSessionPropertyID  inID,UInt32 inDataSize, const void *inData);

 -(id) init
{
     if((self=[super init]))
    {
    
    OSStatus result = AudioSessionInitialize(NULL, NULL, NULL, NULL);
    if (result)
        NSLog(@"Error initializing audio session! %ld", result);
    
    [[AVAudioSession sharedInstance] setDelegate: self];
    NSError *setCategoryError = nil;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
    if (setCategoryError)
        NSLog(@"Error setting category! %@", setCategoryError);
    
    result = AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange, RouteChangeListener, self);
    if (result) 
        NSLog(@"Could not add property listener! %ld", result);
    
    return self;
}
return nil;
 }


 -(void) setFileForPlay{
     self.player=nil;
       player = [[AVAudioPlayer alloc]   initWithData:[NSData dataWithContentsOfFile:self.soundFile] error:nil];//initWithContentsOfURL:[NSURL URLWithString:self.soundFile]  error:nil];

    if (self.player)
     {
          player.delegate = self;
      }

   }


 -(void) play{

  [self.player play];

  }
  -(void)pause{
    [self.player pause];
    NSLog(@"in Pause");
  }
 -(void)stop{
    [self.player stop];
     NSLog(@"in stop");
  }



              void RouteChangeListener(void *inClientData, AudioSessionPropertyID   inID,UInt32 inDataSize, const void *inData){
      AudioManager* This = (AudioManager*)inClientData;

        if (inID == kAudioSessionProperty_AudioRouteChange) {
    
        CFDictionaryRef routeDict = (CFDictionaryRef)inData;
    NSNumber* reasonValue = (NSNumber*)CFDictionaryGetValue(routeDict, CFSTR(kAudioSession_AudioRouteChangeKey_Reason));
    
    int reason = [reasonValue intValue];
    
    if (reason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {
         
        [This stop];
    }
}

  }
   - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)p successfully:(BOOL)flag
    {
if (flag == NO)
    NSLog(@"Playback finished unsuccessfully");

[p setCurrentTime:0.];
if (inBackground)
{
    [delegate playerDidFinishPlayingInBackground:p];
}
else
{
    [delegate playerDidFinishPlaying:p];
}
    }
     - (void)playerDecodeErrorDidOccur:(AVAudioPlayer *)p error:(NSError *)error
   {
NSLog(@"ERROR IN DECODE: %@\n", error); 
    }
  // we will only get these notifications if playback was interrupted
  - (void)audioPlayerBeginInterruption:(AVAudioPlayer *)p
    {
NSLog(@"Interruption begin ");
// the object has already been paused,  we just need to update UI
if (inBackground)
{
    [delegate playerBeginInterruptionInBackground:p];
}
else
{
    [delegate playerBeginInterruption:p];
}

    }


     - (void)audioPlayerEndInterruption:(AVAudioPlayer *)p withFlags:(NSUInteger)flags
    {
    if(inBackground)
      {
      [delegate playerEndInterruptionInBackgroud:p];
    }
        else
    {
    [delegate playerEndInterruption:p];
    }
        NSLog(@"Interruption ended. Resuming playback");
     }

    - (void)registerForBackgroundNotifications
    {
[[NSNotificationCenter defaultCenter] addObserver:self
                                                    selector:@selector(setInBackgroundFlag)
                                                   name:UIApplicationWillResignActiveNotification
                                             object:nil];

      [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(clearInBackgroundFlag)
                                               name:UIApplicationWillEnterForegroundNotification
                                           object:nil];
  }

 - (void)setInBackgroundFlag
   {
inBackground = true;
   }

- (void)clearInBackgroundFlag
   {
inBackground = false;
   }

   @end

Now the problem is if I am Playing any songs form iPod library and stop the song and closing the iPod library, my original avaudioplayer which was playing before starting ipos library is not resuming means it is not playing song again.

What could be wrong?

Is there any demo project or sample code which can help?

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

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

发布评论

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

评论(1

给不了的爱 2024-12-13 20:22:22

您不必在 audioPlayerBeginInterruption: 播放器中的 AVAudioPlayer(代码中的 p)上调用 pause由于中断,不应在该阶段进行比赛。在 audioPlayerEndInterruption: 中,以下内容通常对我有用:

[p prepareToPlay];
[p play];

Audio Session Cookbook 有一个关于“使用 AVAudioPlayer 处理中断类”也应该有帮助。

You shouldn't have to call pause on the AVAudioPlayer (p in your code) in audioPlayerBeginInterruption: the player should not be playing at that stage because of the interruption. In audioPlayerEndInterruption: the following usually works for me:

[p prepareToPlay];
[p play];

The Audio Session Cookbook in the iOS docs has a section on "Handling Interruptions with the AVAudioPlayer Class" that should also help.

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