gpt4 book ai didi

ios - 通话后如何恢复音乐

转载 作者:行者123 更新时间:2023-11-29 03:20:56 25 4
gpt4 key购买 nike

我有一个在线音乐播放器。我想给它添加一个功能,如果正在播放歌曲并调用电话(来电或去电),它应该暂停正在通话的音乐,并且在通话断开后,音乐应该重新开始。

这是我的代码:

////FirstViewController.m

#import "FirstViewController.h"
CM_EXPORT const CMTime kCMTimeZero;
@interface FirstViewController ()

@end


@implementation FirstViewController
@synthesize metadatas;
@synthesize toggleButton;
@synthesize slider;
@synthesize mpVolumeView = _mpVolumeView;
@synthesize viewVolume;

- (void)viewDidLoad
{
//[super viewDidLoad];
//slider.transform = CGAffineTransformRotate(slider.transform,270.0/180*M_PI);
//[slider setMaximumValue:2];
//[slider setMinimumValue:0];
//[slider setSelected:YES];


//[[self mpVolumeView] setBackgroundColor:[UIColor clearColor]];

//MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame: [[self mpVolumeView] bounds]];
//[[self mpVolumeView] addSubview:myVolumeView];
//toggleIsOn =TRUE;
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
toggleIsOn=TRUE;
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:self.viewVolume.bounds] ;

[self.viewVolume addSubview:volumeView];

[volumeView sizeToFit];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

-(IBAction)playButtonPressed:(id)sender
{



if(toggleIsOn){

toggleIsOn=!toggleIsOn;

player = nil;
NSString *stringurl = @"";
stringurl = @"http://majestic.wavestreamer.com:6221/listen.pls";
NSURL *url = [NSURL URLWithString:stringurl];
asset = [AVURLAsset URLAssetWithURL:url options:nil];
playerItem = [AVPlayerItem playerItemWithAsset:asset];
player = [AVPlayer playerWithPlayerItem:playerItem];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[playerItem addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:nil];
[playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
[player play];

[self.toggleButton setImage:[UIImage imageNamed:@"reload.png"] forState:UIControlStateNormal];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
else {

[self.toggleButton setImage:[UIImage imageNamed:@"playMusic.png"] forState:UIControlStateNormal];
self->player.rate=0.0;
toggleIsOn=!toggleIsOn;



}


}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}

- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {

[playerItem removeObserver:self forKeyPath:keyPath];


if ([keyPath isEqualToString:@"status"]) {
AVPlayerItem *pItem = (AVPlayerItem *)object;
if (pItem.status == AVPlayerItemStatusReadyToPlay)
{
metadatas.text = @"";
}
}
if ([keyPath isEqualToString:@"timedMetadata"]) {
for (AVAssetTrack *track in playerItem.tracks) {
for (AVPlayerItemTrack *item in player.currentItem.tracks) {
if ([item.assetTrack.mediaType isEqual:AVMediaTypeAudio]) {
NSArray *meta = [playerItem timedMetadata];
for (AVMetadataItem *metaItem in meta) {

NSString *source = metaItem.stringValue;
metadatas.text = source;
}
}
}
}
}

[self.toggleButton setImage:[UIImage imageNamed:toggleIsOn ? @"playMusic.png" :@"stop.png"] forState:UIControlStateNormal];

}


-(IBAction)fbButtonPressed:(id)sender
{

NSURL *url = [NSURL URLWithString:@"http://www.facebook.com"];

if (![[UIApplication sharedApplication] openURL:url])
NSLog(@"%@%@",@"Failed to open url:",[url description]);
}


-(IBAction)inButtonPressed:(id)sender
{

NSURL *url = [NSURL URLWithString:@"http://www.linkedin.com"];

if (![[UIApplication sharedApplication] openURL:url])
NSLog(@"%@%@",@"Failed to open url:",[url description]);
}

-(IBAction)tweetButtonPressed:(id)sender
{

NSURL *url = [NSURL URLWithString:@"http://www.twitter.com"];

if (![[UIApplication sharedApplication] openURL:url])
NSLog(@"%@%@",@"Failed to open url:",[url description]);
}
-(IBAction) sliderChanged:(id)sender
{


}

- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

@end

此外,插入音量 View 的代码也在那里,然后 UI 中也没有音量 Controller 。为什么会这样?

////FirstViewController.m

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

@class AVPlayer;
@class AVPlayerItem;

@interface FirstViewController : UIViewController
{

UIView *viewVolume;
AVAsset *asset;
AVPlayerItem *playerItem;
AVPlayer *player;
NSURL *mURL;
MPVolumeView *_mpVolumeView;
IBOutlet UILabel *metadatas;
IBOutlet UIButton *toggleButton;
BOOL toggleIsOn;
IBOutlet UISlider *slider;


}

-(IBAction)playButtonPressed:(id)sender;
-(IBAction)fbButtonPressed:(id)sender;
-(IBAction)inButtonPressed:(id)sender;
-(IBAction)tweetButtonPressed:(id)sender;
-(IBAction) sliderChanged:(id)sender;
@property (strong, nonatomic) IBOutlet UISlider *slider;
@property (nonatomic, retain) IBOutlet MPVolumeView *mpVolumeView;
@property (nonatomic, retain) IBOutlet UILabel *metadatas;
@property (nonatomic, retain) IBOutlet UIButton *toggleButton;
@property (nonatomic, strong) IBOutlet UIView *viewVolume;

@end

通话后无法启动音乐。请帮助提供可能的解决方案。

最佳答案

需要在FirstViewController中添加观察者

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

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

- (void) resumePlayMusic
{
.......
}

并且不要忘记删除观察者。

关于ios - 通话后如何恢复音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21106856/

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