Audio samples. Are you kidding me?!

Discussion in 'iOS Programming' started by limitedwave, Nov 11, 2008.

  1. macrumors newbie

    Joined:
    Nov 11, 2008
    #1
    Greetings - I've been trying to stream a single mp3 to an app for days now. There are no working examples anywhere it seems, and this baffles me! Can it be true? I can play "system sounds" easy, but not a full mp3. The only thing working so far, is working backwards and stripping out things from the SpeakHere sample app. That is an exercise!

    So the question is for the gurus - what will it take for us programmers just getting into Cocoa, to get a nice simple sample? ! From the other posts I've seen I think someone might be willing to donate an icon. I'd probably toss out a void. Thanks.
     
  2. macrumors member

    Joined:
    Aug 21, 2008
    #2
    Try this

    http://www.idevgames.com/forum/showthread.php?t=15280
     
  3. thread starter macrumors newbie

    Joined:
    Nov 11, 2008
    #3
    Thanks for the quick response. It failed for me, here's what I did.

    I started a new blank window based application. I added an NSObject subclass to the project, named it GBMusicTrack, it made the .h file too of course. There was an import of AppKit.h, and the compiler wasn't finding it. I assumed that was maybe something replaced by Foundation or something in Cocoa, and removed the import. The project compiles without error until I add the call lines. The forum thread doesn't really say where to put those lines either, and I put them in the applicationDidFinishLaunching() method. It is this chunk that I get errors from:

    Code:
    GBMusicTrack *song = [[GBMusicTrack alloc] initWithPath:[[NSBundle mainBundle] pathForResource:@"WarChant" ofType:@"mp3"]];
    	[song setRepeat:YES];
    	[song play];
    
    error: 'GBMusicTrack' undeclared(first use in this function) 
    error: 'song' undeclared(first use in this function) 
    Am I putting "the code" as he calls it, in the wrong place? Do I simply need to declare that stuff and if so, where? Thanks...
     
  4. macrumors regular

    Joined:
    Mar 24, 2008
    #4
    Use the sound engine from the Crash Landing example. Its pretty robust, and has support for both music and localized sound effects.

    EDIT: Also, in the above example, try replacing the Appkit.h require with a UIKit.h instead.
     
  5. thread starter macrumors newbie

    Joined:
    Nov 11, 2008
    #5
    I did add the UIKit.h import, no affect. I'm looking for the Crash Landing example...can someone point me to it? Thx.
     
  6. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #6
  7. thread starter macrumors newbie

    Joined:
    Nov 11, 2008
    #7
    Cool, thanks for the link. I did try the oalTouch sample too and it wouldn't run in the simulator for me. I'll look at this Crash Landing one now. Thx.

    EDIT.
    Crash Landing only on device, I only have simulator. My company hasn't yet paid for the developer license and therefore I can't transfer to a device. It looks like I may be able to dig out what I need...
     
  8. thread starter macrumors newbie

    Joined:
    Nov 11, 2008
    #8
    Uh, ok....I'm a complete TOOL. If the Crash Landing project is something I should be able to deconstruct, or even remotely understand, I am in the wrong place! That is the most complicated thing I've seen yet. The more I learn, the harder this gets. Thx.
     
  9. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #9
    Yep, it's pretty nasty I'm afraid. If you just want to play an mp3 and don't care about what's on the screen, you could try MPMoviePlayerController instead (https://developer.apple.com/iphone/...PlayerController/MPMoviePlayerController.html), which is far simpler:

    Code:
    NSString *url = @"http://www.example.com/path/to/song.mp3";
    MPMoviePlayerController moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:url]];
    [moviePlayer play];
     
  10. thread starter macrumors newbie

    Joined:
    Nov 11, 2008
    #10
    Seriously? You can use the media player for an mp3? First I've heard of that. Yes, my task is that simple. I just need to play an mp3. Don't even need t stop it!!!

    I'll have a look at this one. Thanks again, I really appreciate your advice.
     
  11. thread starter macrumors newbie

    Joined:
    Nov 11, 2008
    #11
    ...the link you reference to the Class - doesn't list MP3, only video codecs. Is it simply unlisted?

    Also - you provided what looks like the implement code, that I would place I assume in the appFinishedLaunching() method ? I would include/import the MediaPlayer framework. Is there anything else I would need to do to make that code work?
     
  12. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #12
    Yup; works fine for me :)

    That's it really. Import the framework and #import <MediaPlayer/MediaPlayer.h>in your header. Apple have some sample code which includes examples of notifications here: https://developer.apple.com/iphone/library/samplecode/MoviePlayer_iPhone/index.html. In their example they're loading a local file, but URLs as in the above example work fine too.
     
  13. thread starter macrumors newbie

    Joined:
    Nov 11, 2008
    #13
    Local file is fine. One more question as I start into this one, it reads:
    If you use this class to play audio files, it displays a black screen while the audio plays.

    Does it only display a black screen, period? That would suffice I think, but realy need a static image to show while playing. No interaction though.
     
  14. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #14
    On the device it will show the QuickTime logo and a few interface bits (volume control, position in audio, play/pause, exit); the simulator is a bit variable! Unfortunately you don't get to control the background image, though I guess you could replace the mp3 with a video that has a still-image background as a quick fix.
     
  15. thread starter macrumors newbie

    Joined:
    Nov 11, 2008
    #15
    Started blank project. Opened AppDelegate.m file, added the include, and added the code referenced above wihtin the applicationDidFinishLaunching: method. I got 4 errors.

    Code:
    NSString *url = @"Florida_State_War_Chant.mp3";
    	MPMoviePlayerController moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:url]];
    	[moviePlayer play];

    error: variable sized oject can not be initiated
    error: statically allocated instance of Obj-C class 'MPMoviePlayerController'
    error: statically allocated instance of Obj-C class 'MPMoviePlayerController'
    error: cannot convert to a pointer type

    ?
     
  16. thread starter macrumors newbie

    Joined:
    Nov 11, 2008
    #16
    Great fix idea. Streaming a video would work too.
     
  17. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #17
    For a local file you need to follow the example from the sample code (https://developer.apple.com/iphone/library/samplecode/MoviePlayer_iPhone/index.html), i.e.:

    Code:
    NSString *url = [[NSBundle mainBundle] pathForResource:@"Florida_State_War_Chant" ofType:@"mp3"];
     
  18. thread starter macrumors newbie

    Joined:
    Nov 11, 2008
    #18
    For now, file can be local or remote. Either way - I get the same errors.

    Code:
    - (void)applicationDidFinishLaunching:(UIApplication *)application {    
    
        // Override point for customization after application launch
        [window makeKeyAndVisible];
    	
    	NSString *url = @"http://www.limitedwave.com/mp3/FailingForward.mp3";
    	MPMoviePlayerController moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:url]];
    	[moviePlayer play];	
    }
    I zipped and attached my test project. Feel free to send whatever you have working back at me. I hate to keep pestering you with this back and forth, I'm sure you have better things to do!
     

    Attached Files:

  19. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #19
    You need to import the MoviePlayer framework itself into your project (Add > Existing Frameworks...).

    Also, MPMoviePlayerController moviePlayer should be MPMoviePlayerController *moviePlayer since it's not static.
     
  20. thread starter macrumors newbie

    Joined:
    Nov 11, 2008
    #20
    Almost there huh! Man - I can't import the MoviePlayer Framework cause it isn't there! ? Not sure what's going on with that. I d/l'd and installed all of this within the last 7 days. Attached is a screen shot of what see in that dialog...
     

    Attached Files:

  21. thread starter macrumors newbie

    Joined:
    Nov 11, 2008
    #21
    ...I located the media player framework, still no MPMoviePlayer framework.

    Attached screen shot of the current, single error:
     

    Attached Files:

  22. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #22
    Most of the OP's problems have nothing to do with audio. They have to do with knowing how to write Obj-C apps. I suggest basic Obj-C and Cocoa tutorials until you have them down pat in your sleep. Then the OP will know were all the pieces (declarations, functions, frameworks, library includes, pointer references, etc., etc.) go. There are dozens and dozens of "silly" little Obj-C details which are trivial to get right when you know what your doing, and always end up in the wrong place if you don't.


    YMMV.
     
  23. macrumors newbie

    Joined:
    Nov 1, 2008
    #23
    OP, you're missing the star in MoviePlayer *moviePlayer.
     
  24. thread starter macrumors newbie

    Joined:
    Nov 11, 2008
    #24
    Thanks for the advice senor firewood. I couldn't agree with you more. My reality is this - for starters, I can barely decode your response. That's barely English to me! In general, I learn better by doing. I have spent one day working through the Object Oriented Programming primer, a day on the Obj-C primer, a day on the Cocoa primer, and then a week building small Hello World type apps - subbing text strings, triggering methods on orientation changes, simple stuff. The task at hand for me is to simply play an audio file at the touch of a button, change of orientation or change in acceleration. This is just one task among many for me, and for this one I am fast running out of time! I don't like to fail at things, so likely I'll be hammering at this after work hours for some time to come, if i do in fact fail. Right now I'm hoping Mr. jnic might continue to work me through my problem.

    If anyone has a simple project in which a complete audio file can be triggered and player, please zip and post it! I'm not the only person who'd wet their pants for that. Thx.
     
  25. thread starter macrumors newbie

    Joined:
    Nov 11, 2008
    #25
    I added the star...though I don't see the exact line you speak of.

    Current Code:
    Code:
    NSString *url = @"http://www.limitedwave.com/mp3/FailingForward.mp3";
    	MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:url]];
    	[moviePlayer play];
    ...gives me the attached error(image).

    What/who is OP?
     

    Attached Files:

Share This Page