0

これが私のコードです

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.miに、フレームワーク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を停止する必要があります。

4

1 に答える 1

1

まず、次のように追加UIButtonUIViewます。

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]; 私が思うにそれを削除することができます、そのようなものはうまくいくはずです:)私の答えに問題があるかどうか尋ねてください。

于 2012-10-21T06:22:51.790 に答える