BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 01-03-2010, 03:06 PM   #1 (permalink)
New Member
 
Join Date: Jan 2010
Model: N/A
PIN: N/A
Carrier: N/A
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem playing sound file bundled with application

Please Login to Remove!

I am having a problem playing a midi file that I have bundled with my application. I have read the C10_Audio_And_Video_Playback.pdf on the BlackBerry Developer zone, but after following all of the instructions, I still can't play an audio file that is bundled with the app. I also searched this forum extensively and cannot find a solution.

Here is an example of my source code:
__________________________________________________ ______________
public class ProjectName extends UiApplication {
public Player soundPlayer1;
public VolumeControl vc;
public static void main(String[] args)
{
ProjectName theApp = new ProjectName();
theApp.enterEventDispatcher();


}
public ProjectName() {
pushScreen(new ProjectNameScreen());

try {
InputStream is =
getClass().getResourceAsStream("/src/soundfile.mid");
soundPlayer1 = Manager.createPlayer(is, "audio/midi");
soundPlayer1.realize();
soundPlayer1.prefetch();
vc = (VolumeControl)
soundPlayer1.getControl("VolumeControl");
vc.setLevel(50);


} catch (Exception e) {
System.out.println(e.toString());
}

}
______________________________________________
And then in the Start button listener, I have the following code:
_______________________________________________
try {
soundPlayer1.start();
} catch (MediaException e) {
errorField.setText("Error creating InputStream 1");
}

____________________________________________

I keep getting this exception:

JVM Error 104 Uncaught: NullPointerException

I think this is something very simple, such as the relative path passed into getResourceAsStream for soundfile1.mid is wrong. But I have tried multiple relative paths and it hasn't stopped the exception.

For the example code above, the soundfile.mid file is in the ProjectName\src folder.

I am using net.rim.eide.componentpack4.7.0_4.7.0.46

Any help with this task would be greatly appreciated.

Thanks.
Offline  
Old 01-03-2010, 04:25 PM   #2 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

To get the resource, you need to be using the relative package structure, not the project structure on the disk.

Code:
    public void play()
    {
        if (m_player != null){
            stop();
        }
        
        try {
            Class cl = Class.forName("com.foo.Foobar.Foobar");
            InputStream input = cl.getResourceAsStream("/foonoise.mp3");
            m_player = Manager.createPlayer(input,"audio/mpeg");
            m_player.realize();
            m_volumeControl = getVolumeControl(m_player);
            if (m_volumeControl != null)
                m_volumeControl.setLevel(m_volume);
            m_player.prefetch();
            m_player.start();
        }
        catch (Exception e){
            System.out.println("ERR: " + e.toString());
        }
    }
Offline  
Old 01-06-2010, 05:11 PM   #3 (permalink)
New Member
 
Join Date: Jan 2010
Model: N/A
PIN: N/A
Carrier: N/A
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: Problem playing sound file bundled with application

Dougsg38p,

Thanks a lot for your reply and sorry for the delay in this response.

I've tried your solution amd I'm still receiving the null pointer exception, but I'm sure I'm still soing something wrong.

For example, the package which contains my Java source file and sound file is com.rim.app.projectname. I understand that the file structure on the disk is not relevent here, but just for reference, my sound files are located in the same folder as my Java source file. Here is the code I am trying, which still returns a null input stream:

______________________

try {
Class cl = Class.forName("com.rim.app.projectname");
InputStream input = cl.getResourceAsStream("/CowBell1.mid");
soundPlayer1 = Manager.createPlayer(input, "audio/midi");
soundPlayer1.realize();
soundPlayer1.prefetch();
vc = (VolumeControl)
soundPlayer1.getControl("VolumeControl");
vc.setLevel(100);
soundPlayer1.start();

}

catch (Exception e) {
errorField.setText("Error creating InputStream 1");
}

_____________________________

I wonder what I am doing wrong that the getResourceAsStream method keeps returning null.

1) Do I need to import anything to use Class?
- In Eclipse, the following warning is given under Class:
Class is a raw type. References to generic type Class<T> should be parameterized

2) The \ before CowBell1.mid indicates an absolute location. So don't I need to append the package name in front of it such as:

cl.getResourceAsStream("/com.rim.app.projectname/CowBell1.mid");

I realize that something simple is wrong here. So, I may be overlooking something very obvious or just fundamentally doing something wrong.

Another question: This sound will be played repeatedly. Does the entire above code need to execute each time? Or do I need to just call .realize() and .prefetch() every time before executing the Player start() method?

The help is much appreciated.

Thanks.
Offline  
Old 01-06-2010, 07:44 PM   #4 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

If you look at my example again, you will see that you are missing the class name of you app.

Class cl = Class.forName("com.rim.app.projectname.MyClassName ")

AND...don't forget to "Add" the file to your project.
Offline  
Old 01-07-2010, 01:44 PM   #5 (permalink)
New Member
 
Join Date: Jan 2010
Model: N/A
PIN: N/A
Carrier: N/A
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: Problem playing sound file bundled with application

Thanks Dougsg38p.

Adding the class name now allows the input stream to be successfully returned and stored in input.

However, when I get to this line, it fails:

soundPlayer1 = Manager.createPlayer(input, "audio/midi");

Then, of course, .realize(), Volume control settings, .prefetch(), and .start() also fail.

Any ideas why it is successfully finding the sound and creating the input stream but then is unable to successfully execute Manager.createPlayer(input, "audio/midi") ??

Thanks again for all the help.
Offline  
Old 01-07-2010, 02:20 PM   #6 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

What is the exception?

Just a guess, but the device may not support midi.

Try MP3 instead.
Offline  
Old 01-07-2010, 03:18 PM   #7 (permalink)
New Member
 
Join Date: Jan 2010
Model: N/A
PIN: N/A
Carrier: N/A
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: Problem playing sound file bundled with application

Thank you very much for all of your help Dougsg38p. Changing to mp3 worked. I appreciate it.
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.