Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Simple way to play background music

Simple way to play background music
Thread Tools
DrLex
Fresh-Faced Recruit
Join Date: Jul 2003
Location: Belgium
Status: Offline
Reply With Quote
Jul 8, 2003, 09:22 AM
 
I am looking for a simple way to play some background music in a game. The music is in MIDI format and the most obvious choice would be to use QuickTime. The game is based on the SDL library, which also offers music playback, but there are weird problems with this and the quality is not that good, so it would be better to send the music directly to QuickTime.

I already managed to write a simple working test application which uses NSMovieView. About 4 lines of code do the trick, which is really neat. But, there is one problem: the music refuses to loop, although I used setLoopMode, and loopMode indicates that the loop mode is set successfully.

I found another thread in this forum which was about the same problem. The proposed solution was to create a "real NSMovieView" instead of using [[NSMovieView alloc] init] like I did. However, I am a total novice concerning Cocoa programming, and worse is that - as far as I understand what is meant here - I'll need to draw a 'physical' NSMovieView in some window in Interface Builder. The problem is that there are no windows in the project because SDL handles video output directly. How do I solve this?
     
Gametes
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status: Offline
Reply With Quote
Jul 8, 2003, 04:32 PM
 
I don't understand. You want to play music, right? Then why are you making a movieView?

The simplest way to play music is with NSSound.
Read about
Code:
- (id)initWithContentsOfFile: (NSString *)path byReference: (BOOL)byRef
to create your sound file,
Code:
- (BOOL)play
to play it, and the delegate method
Code:
- (void)sound: (NSSound *)sound didFinishPlaying: (BOOL)aBool
to loop it.

Or am I missing something?
you are not your signature
     
Angus_D
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Jul 8, 2003, 04:46 PM
 
Doesn't SDL have some sort of audio stuff?
     
DrLex  (op)
Fresh-Faced Recruit
Join Date: Jul 2003
Location: Belgium
Status: Offline
Reply With Quote
Jul 9, 2003, 08:46 AM
 
Originally posted by Angus_D:
Doesn't SDL have some sort of audio stuff?
Yes, but for MIDI playback, it does some weird conversions in order to be compatible with OS 9, and ths decreases the quality of the music.
Moreover, I get weird malloc errors when loading a new song, and when I debug, this error seems to happen within a standard QuickTime function call. So I think it's easier to route the sound directly to QuickTime instead.
Anyway, I'll try it the NSSound way, thanks for the advice.
     
DrLex  (op)
Fresh-Faced Recruit
Join Date: Jul 2003
Location: Belgium
Status: Offline
Reply With Quote
Jul 9, 2003, 09:51 AM
 
Originally posted by Gametes:
I don't understand. You want to play music, right? Then why are you making a movieView?

The simplest way to play music is with NSSound.
Hmm, it appears that NSSound can only play AIFF, WAV and NeXT SND. I need to play MIDI files...
     
Rickster
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
Jul 9, 2003, 05:41 PM
 
You might be able to create an NSMovieView in a window that's never put onscreen (or is effectively obscured by the fullscreen graphics context your game uses). Haven't tried it, but it might work.

And of course, you can always drop down to the actual QuickTime APIs...
Rick Roe
icons.cx | weblog
     
Gametes
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status: Offline
Reply With Quote
Jul 9, 2003, 06:44 PM
 
I think you can just create a movieView and never insert it into a view hierarchy. It can still be called, though. i haven't tried this.
If you must insert it, just have it set on hidden (10.3)
you are not your signature
     
Brass
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Jul 9, 2003, 08:14 PM
 
A window can be open but not displayed, if that helps (ie, orderOut:)
     
Ghoser777
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Jul 9, 2003, 11:23 PM
 
This isn't documented, but the trick is to create a window with Rect {0,0,0,0}, add an NSMovieView instance to the contentView's subviews, and then order the window front. Just creating the NSMovieView instance will cause no music to be played, as I had to find out the hard way.

Matt Fahrenbacher
     
Tim2 at Omni
Dedicated MacNNer
Join Date: Aug 2001
Location: Seattle, WA
Status: Offline
Reply With Quote
Jul 10, 2003, 11:45 PM
 
I had to do this once. My solution was not to create a hidden NSMovieView, but to create an NSMovie instance:

movie = [[NSMovie alloc] initWithURL:[NSURL fileURLWithPath:moviePath] byReference:NO];

Take a look at NSMovie.h and you'll see the following method:

- (void*/*Movie*/)QTMovie;

You can pass this directly into QuickTime, like so:

qtMovie = [movie QTMovie];
StartMovie(qtMovie);
...
if (IsMovieDone(qtMovie))
StopMovie(qtMovie);

Check out Movies.h (in the QuickTime framework) for more info.

Hope that helps!
Tim Omernick
Engineer, The Omni Group
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 06:09 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,