Iphone App mit mehreren Sounddateien

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Iphone App mit mehreren Sounddateien

    Hallo alle zusammen,

    ich wende mich an euch weil ich mehrere Problem bei der Entwicklung einer Iphone App habe. Ich hab zwar im Forum schon was dazu gefunden was mir aber nicht sehr weitergeholfen hat.

    Das UI ist so aufgebaut dass man eben mehrere mit Photoshop erstellte Buttons anklicken kann. Die sind dann eben über die Seite verteilt und am unteren Ende gibt es noch eine Toolbar mit mehreren Seiten inden man sich bewegen kann.
    Nun zu meinen Problem:
    Wenn ich jetzt eine Sounddatei anklicke (mp3) und dann kurz darauf noch eine weitere hinzu, dann überlappen die sich und es entsteht ein Chaos an verschiedenen Stimmen =)

    Nun würde ich das ganz gern so haben,dass wenn man auf einen Button klickt der Sound wie gehabt abgespielt wird. Wenn noch ein 2ter hinzukommt sollte der erste stoppen und der zweite eben Anfangen. Im Prinzip einfach, nur ich hab das ganze gegoogelt und das einzige was rauskommt ist wie man sie abspielt, aber nicht wie man sie eben aufeinander "abstimmt". So ich hoffe ich verlange da nicht zuviel von euch ;)
    Falls es wichtig sein sollte : Meine Xcode Version ist 3.2.6 auf dem Schnee Leoparden :D

    Den Quellcode füge ich noch hinzu :) So dann bedanke ich mich schon mal bei euch und hoffe das mir einer von euch bei meinem Problem helfen kann.


    Quellcode

    1. #import <UIKit/UIKit.h>
    2. #import <AudioToolbox/AudioToolbox.h>
    3. #import <AVFoundation/AVFoundation.h>
    4. @interface SoundViewController : UIViewController {
    5. }
    6. -(IBAction)buttonPressed1;
    7. -(IBAction)buttonPressed2;
    8. @end
    Alles anzeigen


    Quellcode

    1. #import "SoundViewController.h"
    2. @implementation SoundViewController
    3. -(IBAction)buttonPressed1 {
    4. NSString *path = [[NSBundle mainBundle] pathForResource:@"Testsound1" ofType:@"wav"];
    5. AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    6. theAudio.delegate = self;
    7. [theAudio play];
    8. }
    Alles anzeigen



    Grüße
  • Ich würde es ungefähr so lösen:

    Quellcode

    1. -(IBAction)button1
    2. {
    3. [self thePlayerPlayWith:@"Pfad Sounddatei"];
    4. }
    5. -(void)thePlayerPlayWith:(NSString*)thePath
    6. {
    7. if ([self.theAVPlayer isPlaying]
    8. {
    9. NSLog (@"Ja, dann Stop");
    10. }
    11. else
    12. {
    13. NSLog (@"Nein, neuer Titel");
    14. }
    15. ....
    16. }
    Alles anzeigen
  • B-Man schrieb:

    Hallo alle zusammen,

    Quellcode

    1. #import "SoundViewController.h"
    2. @implementation SoundViewController
    3. -(IBAction)buttonPressed1 {
    4. NSString *path = [[NSBundle mainBundle] pathForResource:@"Testsound1" ofType:@"wav"];
    5. AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    6. theAudio.delegate = self;
    7. [theAudio play];
    8. }
    Alles anzeigen



    Grüße


    Frage an unsere ARC Experten. Der AudioPlayer wird doch hier am Ende der Methode wieder released oder nicht ? Also kann doch gar keine Delegate Methode aufgerufen werden, oder ?
    Sprich so wie es ist, ist es eh total quatsch, da man diesen Player doch gar nicht wieder anhalten kann ?

    Gruß

    Claus
    2 Stunden Try & Error erspart 10 Minuten Handbuchlesen.

    Pre-Kaffee-Posts sind mit Vorsicht zu geniessen :)
  • ramo schrieb:

    Ich würde es ungefähr so lösen:

    Quellcode

    1. -(IBAction)button1
    2. {
    3. [self thePlayerPlayWith:@"Pfad Sounddatei"];
    4. }
    5. -(void)thePlayerPlayWith:(NSString*)thePath
    6. {
    7. if ([self.theAVPlayer isPlaying]
    8. {
    9. NSLog (@"Ja, dann Stop");
    10. }
    11. else
    12. {
    13. NSLog (@"Nein, neuer Titel");
    14. }
    15. ....
    16. }
    Alles anzeigen

    Ich hab leider keine Ahnung was ich genau das für das NSLog schreiben sollte ?(
  • Ungefähr so:

    Quellcode

    1. #import <UIKit/UIKit.h>
    2. #import <AVFoundation/AVFoundation.h>
    3. @interface ViewController : UIViewController <AVAudioPlayerDelegate>
    4. @property (nonatomic, retain) AVAudioPlayer *theAVPlayer;
    5. - (IBAction)button1:(id)sender;
    6. - (IBAction)button2:(id)sender;
    7. - (IBAction)button3:(id)sender;
    8. -(void)playWithSound:(NSString *)thePath;
    9. . . . .
    10. #import "ViewController.h"
    11. @implementation ViewController
    12. @synthesize theAVPlayer;
    13. - (IBAction)button1:(id)sender
    14. {
    15. NSString *path = [[NSBundle mainBundle] pathForResource:@"1-welcome" ofType:@"wav"];
    16. [self playWithSound:path];
    17. }
    18. - (IBAction)button2:(id)sender
    19. {
    20. NSString *path = [[NSBundle mainBundle] pathForResource:@"sirene24s" ofType:@"wav"];
    21. [self playWithSound:path];
    22. }
    23. - (IBAction)button3:(id)sender
    24. {
    25. }
    26. -(void)playWithSound:(NSString *)thePath
    27. {
    28. if ([self.theAVPlayer isPlaying])
    29. {
    30. [self.theAVPlayer stop];
    31. self.theAVPlayer = nil;
    32. self.theAVPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:thePath] error:NULL]autorelease];
    33. [self.theAVPlayer play];
    34. }
    35. else
    36. {
    37. self.theAVPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:thePath] error:NULL]autorelease];
    38. [self.theAVPlayer setDelegate:self];
    39. [self.theAVPlayer play];
    40. }
    41. }
    Alles anzeigen
  • Ah ok danke! :)Und wo genau muss der Code rein? AppDelegate.m? Entschuldige wenn ich so dumm frage :D

    Quellcode

    1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    2. sleep(3);
    3. // Override point for customization after application launch.
    4. // Set the view controller as the window's root view controller and display.
    5. self.window.rootViewController = self.viewController;
    6. [self.window makeKeyAndVisible];
    7. return YES;
    8. }
    9. - (void)applicationWillResignActive:(UIApplication *)application {
    10. /*
    11. Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    12. Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    13. */
    14. }
    15. - (void)applicationDidEnterBackground:(UIApplication *)application {
    16. /*
    17. Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    18. If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
    19. */
    20. }
    21. - (void)applicationWillEnterForeground:(UIApplication *)application {
    22. /*
    23. Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
    24. */
    25. }
    26. - (void)applicationDidBecomeActive:(UIApplication *)application {
    27. /*
    28. Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    29. */
    30. }
    31. - (void)applicationWillTerminate:(UIApplication *)application {
    32. /*
    33. Called when the application is about to terminate.
    34. See also applicationDidEnterBackground:.
    35. */
    36. }
    37. #pragma mark -
    38. #pragma mark Memory management
    39. - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    40. /*
    41. Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
    42. */
    43. }
    44. - (void)dealloc {
    45. [viewController release];
    46. [window release];
    47. [super dealloc];
    48. }
    49. @end
    Alles anzeigen

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von B-Man ()

  • Undefined symbols for architecture i386:
    "_OBJC_CLASS_$_AVAudioPlayer", referenced from:
    objc-class-ref in WasLaberschDuViewController.o
    ld: symbol(s) not found for architecture i386
    collect2: ld returned 1 exit status
    Ich hab den Code jetzt drin und er gibt mir die Fehlermeldung...Was soll ich tun? Ich verwende jetzt Xcode 4.

    Quellcode

    1. #import "WasLaberschDuViewController.h"
    2. @implementation WasLaberschDuViewController
    3. @synthesize theAVPlayer;
    4. - (IBAction)button1:(id)sender
    5. {
    6. NSString *path = [[NSBundle mainBundle] pathForResource:@"angelo" ofType:@"wmp3"];
    7. [self playWithSound:path];
    8. }
    9. - (IBAction)button2:(id)sender
    10. {
    11. NSString *path = [[NSBundle mainBundle] pathForResource:@"anDenEiern" ofType:@"mp3"];
    12. [self playWithSound:path];
    13. }
    14. -(IBAction)button3:(id)sender {
    15. }
    16. -(void)playWithSound:(NSString *)thePath
    17. {
    18. if ([self.theAVPlayer isPlaying])
    19. {
    20. [self.theAVPlayer stop];
    21. self.theAVPlayer = nil;
    22. self.theAVPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:thePath] error:NULL]autorelease];
    23. [self.theAVPlayer play];
    24. }
    25. else
    26. {
    27. self.theAVPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:thePath] error:NULL]autorelease];
    28. [self.theAVPlayer setDelegate:self];
    29. [self.theAVPlayer play];
    30. }
    31. }
    Alles anzeigen


    Quellcode

    1. #import <UIKit/UIKit.h>
    2. #import <AVFoundation/AVFoundation.h>
    3. @interface WasLaberschDuViewController : UIViewController <AVAudioPlayerDelegate>
    4. @property (nonatomic, retain) AVAudioPlayer *theAVPlayer;
    5. -(IBAction)button1:(id)sender;
    6. -(IBAction)button2:(id)sender;
    7. -(IBAction)button3:(id)sender;
    8. -(void)playWithSound:(NSString *)thePath;
    9. @end
    Alles anzeigen
  • Thallius schrieb:

    B-Man schrieb:

    Quellcode

    1. #import "SoundViewController.h"
    2. @implementation SoundViewController
    3. -(IBAction)buttonPressed1 {
    4. NSString *path = [[NSBundle mainBundle] pathForResource:@"Testsound1" ofType:@"wav"];
    5. AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    6. theAudio.delegate = self;
    7. [theAudio play];
    8. }
    Alles anzeigen



    Grüße


    Frage an unsere ARC Experten. Der AudioPlayer wird doch hier am Ende der Methode wieder released oder nicht ?

    Richtig.

    Thallius schrieb:

    Also kann doch gar keine Delegate Methode aufgerufen werden, oder ?
    Sprich so wie es ist, ist es eh total quatsch, da man diesen Player doch gar nicht wieder anhalten kann ?

    Wäre hier ARC eingeschaltet, würde man nicht besonders viel von dem Sound hören. Denn wie Du richtig sagst, wird der Player am Ende der Methode released. Damit stellt er das Abspielen aber auch sofort ein.

    Michael
  • Michael schrieb:

    Thallius schrieb:

    B-Man schrieb:

    Quellcode

    1. #import "SoundViewController.h"
    2. @implementation SoundViewController
    3. -(IBAction)buttonPressed1 {
    4. NSString *path = [[NSBundle mainBundle] pathForResource:@"Testsound1" ofType:@"wav"];
    5. AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    6. theAudio.delegate = self;
    7. [theAudio play];
    8. }
    Alles anzeigen



    Grüße


    Frage an unsere ARC Experten. Der AudioPlayer wird doch hier am Ende der Methode wieder released oder nicht ?

    Richtig.

    Thallius schrieb:

    Also kann doch gar keine Delegate Methode aufgerufen werden, oder ?
    Sprich so wie es ist, ist es eh total quatsch, da man diesen Player doch gar nicht wieder anhalten kann ?

    Wäre hier ARC eingeschaltet, würde man nicht besonders viel von dem Sound hören. Denn wie Du richtig sagst, wird der Player am Ende der Methode released. Damit stellt er das Abspielen aber auch sofort ein.

    Michael


    Danke, ich war jetzt mal von der Annahme ausgegangen das ARC an ist, weil sonst würde der Code ja mächtig leaken.

    Aber egal wie, der Code ist falsch.

    Gruß

    Claus
    2 Stunden Try & Error erspart 10 Minuten Handbuchlesen.

    Pre-Kaffee-Posts sind mit Vorsicht zu geniessen :)