PDA

View Full Version : Audio samples. Are you kidding me?!




limitedwave
Nov 11, 2008, 06:34 PM
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.



JWBlue
Nov 11, 2008, 07:17 PM
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.

Try this

http://www.idevgames.com/forum/showthread.php?t=15280

limitedwave
Nov 11, 2008, 11:39 PM
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:

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...

SqueegyX
Nov 12, 2008, 01:42 AM
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.

limitedwave
Nov 12, 2008, 09:10 AM
I did add the UIKit.h import, no affect. I'm looking for the Crash Landing example...can someone point me to it? Thx.

jnic
Nov 12, 2008, 09:18 AM
I did add the UIKit.h import, no affect. I'm looking for the Crash Landing example...can someone point me to it? Thx.

http://developer.apple.com/iphone/library/samplecode/CrashLanding/index.html

You might also want to check out oalTouch, just added last week: http://developer.apple.com/iphone/library/samplecode/oalTouch/index.html

limitedwave
Nov 12, 2008, 09:26 AM
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...

limitedwave
Nov 12, 2008, 09:39 AM
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.

jnic
Nov 12, 2008, 09:59 AM
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.

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/library/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/MPMoviePlayerController/MPMoviePlayerController.html), which is far simpler:

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

limitedwave
Nov 12, 2008, 10:27 AM
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.

limitedwave
Nov 12, 2008, 10:30 AM
...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?

jnic
Nov 12, 2008, 10:36 AM
...the link you reference to the Class - doesn't list MP3, only video codecs. Is it simply unlisted?

Yup; works fine for me :)

.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?

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.

limitedwave
Nov 12, 2008, 10:42 AM
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.

jnic
Nov 12, 2008, 10:49 AM
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.

limitedwave
Nov 12, 2008, 10:53 AM
Started blank project. Opened AppDelegate.m file, added the include, and added the code referenced above wihtin the applicationDidFinishLaunching: method. I got 4 errors.

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

?

limitedwave
Nov 12, 2008, 10:53 AM
Great fix idea. Streaming a video would work too.

jnic
Nov 12, 2008, 11:00 AM
Started blank project. Opened AppDelegate.m file, added the include, and added the code referenced above wihtin the applicationDidFinishLaunching: method. I got 4 errors.

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

?

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.:

NSString *url = [[NSBundle mainBundle] pathForResource:@"Florida_State_War_Chant" ofType:@"mp3"];

limitedwave
Nov 12, 2008, 11:15 AM
For now, file can be local or remote. Either way - I get the same errors.

- (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!

jnic
Nov 12, 2008, 11:25 AM
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.

limitedwave
Nov 12, 2008, 11:37 AM
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...

limitedwave
Nov 12, 2008, 11:44 AM
...I located the media player framework, still no MPMoviePlayer framework.

Attached screen shot of the current, single error:

firewood
Nov 12, 2008, 12:45 PM
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.

lapse
Nov 12, 2008, 01:35 PM
OP, you're missing the star in MoviePlayer *moviePlayer.

limitedwave
Nov 12, 2008, 01:40 PM
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.

limitedwave
Nov 12, 2008, 01:50 PM
OP, you're missing the star in MoviePlayer *moviePlayer.

I added the star...though I don't see the exact line you speak of.

Current 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?

PhoneyDeveloper
Nov 12, 2008, 10:45 PM
OP is you, the Original Poster.

If you do a Find in the Finder for the tex MPMoviePlayerController you will find that it's in a header file called MPMoviePlayerController.h. If you look at where that header file is found you'll find it's inside a framework. Add that framework to your project.

limitedwave
Nov 13, 2008, 06:41 PM
Yeah, I'm all over that Frameworks folder. That is in a MediaPlayer framework, and that's already in my project.

I'm gonna attach my project to this thread. I swear...it seems like I've done everything like its been instructed, and weird problems occur. The error on this build definitely seems like its missing a framework or something. Please have a look if you like. I'd so owe someone a huge debt if that figured that out!

Nickel808
Nov 13, 2008, 09:14 PM
I don't know if you're still looking for new code but....

Here's an excellent audio app that uses SoundEngine.cpp.
SleepSound (http://www.appsamuck.com/day14.html)

Get tons of more useful sample code here.
Appsamuck.com (http://www.appsamuck.com/)


HooRah for no more Apple NDA! (http://news.cnet.com/8301-13579_3-10055509-37.html)

jnic
Nov 14, 2008, 04:30 AM
Yeah, I'm all over that Frameworks folder. That is in a MediaPlayer framework, and that's already in my project.

I'm gonna attach my project to this thread. I swear...it seems like I've done everything like its been instructed, and weird problems occur. The error on this build definitely seems like its missing a framework or something. Please have a look if you like. I'd so owe someone a huge debt if that figured that out!

Works for me. Nice tune :)

PhoneyDeveloper
Nov 14, 2008, 09:00 AM
MOkay. It played for me on both sim and device. Not sure why you had a copy of the MediaPlayer framework in the app folder. Definitely remove that.

jnic
Nov 14, 2008, 09:28 AM
MOkay. It played for me on both sim and device. Not sure why you had a copy of the MediaPlayer framework in the app folder. Definitely remove that.

... and the loose mp3 in there, since you're grabbing one from a server. (To use a local file, see post here: http://forums.macrumors.com/showthread.php?p=6596084#post6596084.)