gpt4 book ai didi

iphone - 无法阻止 AVAudioPlayer 播放

转载 作者:行者123 更新时间:2023-11-29 04:21:03 26 4
gpt4 key购买 nike

这是我的代码

AppDelegate.m我使用此代码来启动 AVAudioPlayer。它工作正常。

#import "AppDelegate.h"

@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{


NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/Helloween.mp3"];
NSLog(@"Path to play: %@", resourcePath);
NSError* err;

//Initialize our player pointing to the path to our resource
backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:&err];

if( err ){
//bail!
NSLog(@"Failed with reason: %@", [err localizedDescription]);
}
else{
//set our delegate and begin playback
backgroundMusic.delegate = self;
[backgroundMusic play];
backgroundMusic.numberOfLoops = -1;
backgroundMusic.currentTime = 0;
backgroundMusic.volume = 1.0;
}
// Override point for customization after application launch.
return YES;
}

在AppDelegate.h中我添加框架和AVAudioPlayer

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate> {
AVAudioPlayer *backgroundMusic;
}

@property(nonatomic,retain) AVAudioPlayer *backgroundMusic;
@property (strong, nonatomic) UIWindow *window;

@end

在 ViewController.m 中,我还添加了框架 AVFoundation 并导入 AppDelegate.h

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import <CoreData/CoreData.h>
#define fName @"data.plist"
#define kNames @"data.plist"
#import <AVFoundation/AVFoundation.h>
#import "AppDelegate.h"


@interface ViewController ()

@end
@implementation ViewController

@synthesize backgroundMusic;
-(IBAction)stopMusic:(id)sender {
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate.backgroundMusic stop];

}

- (void)viewDidLoad
{
[super viewDidLoad];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

//set the position of the button
button.frame = CGRectMake(100, 100, 20, 20);
button.backgroundColor = [UIColor whiteColor];
[button addTarget:self action:@selector(stopMusic:) forControlEvents:UIControlEventTouchUpInside];

//add the button to the view
[self.view addSubview:button];

ViewController.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <CoreData/CoreData.h>
#define kNames @"data.plist"
#import <AVFoundation/AVFoundation.h>


@interface ViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *backgroundMusic;
}

我需要使用按钮停止 AudioPlayer。


这是我的代码

AppDelegate.m我使用此代码来启动 AVAudioPlayer。

#import "AppDelegate.h"

@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{


NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/Helloween.mp3"];
NSLog(@"Path to play: %@", resourcePath);
NSError* err;

//Initialize our player pointing to the path to our resource
backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:&err];

if( err ){
//bail!
NSLog(@"Failed with reason: %@", [err localizedDescription]);
}
else{
//set our delegate and begin playback
backgroundMusic.delegate = self;
[backgroundMusic play];
backgroundMusic.numberOfLoops = -1;
backgroundMusic.currentTime = 0;
backgroundMusic.volume = 1.0;
}
// Override point for customization after application launch.
return YES;
}

AppDelegate.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate> {
AVAudioPlayer *backgroundMusic;
}
-(IBAction)stopMus:(id)sender;
@property(nonatomic,retain) AVAudioPlayer *backgroundMusic;
@property (strong, nonatomic) UIWindow *window;

@end

ViewController.m

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import <CoreData/CoreData.h>
#define fName @"data.plist"
#define kNames @"data.plist"
#import <AVFoundation/AVFoundation.h>
#import "AppDelegate.h"


@interface ViewController ()

@end
@implementation ViewController

@synthesize backgroundMusic;
-(IBAction)stopMusic:(id)sender {
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate.backgroundMusic stop];

}

- (void)viewDidLoad
{
[super viewDidLoad];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

//set the position of the button
button.frame = CGRectMake(100, 100, 20, 20);
button.backgroundColor = [UIColor whiteColor];
[button addTarget:self action:@selector(stopMusic:) forControlEvents:UIControlEventTouchUpInside];

//add the button to the view
[self.view addSubview:button];

ViewController.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <CoreData/CoreData.h>
#define kNames @"data.plist"
#import <AVFoundation/AVFoundation.h>


@interface ViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *backgroundMusic;
}

我需要使用按钮停止 AudioPlayer。

最佳答案

首先,将 UIButton 添加到 UIView 中,如下所示:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

//set the position of the button
button.frame = CGRectMake(0, 0, 0, 0);

[button addTarget:self action:@selector(yourMethod:) forControlEvents:UIControlEventTouchUpInside];

//add the button to the view
[self.view addSubview:button];

那么,

-(IBAction)yourMethod:(id)sender {
[backgroundMusic stop];


}

使用完按钮后,您可以通过[button removeFromSuperView];将其删除我认为,类似的东西应该有效:)请询问您是否对我的答案有疑问。

关于iphone - 无法阻止 AVAudioPlayer 播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12991480/

26 4 0
文章推荐: java - 如何按长字段对 List 进行排序