If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
brent_glad
Posts: 66
Registered: ‎11-23-2009
My Device: Not Specified

media player completely unreliable

 

package com.rim.samples.device.helloworlddemo;

 

import net.rim.device.api.ui.UiApplication;

import net.rim.device.api.ui.container.MainScreen;

import net.rim.device.api.ui.Field;

import net.rim.device.api.ui.component.Dialog;

import net.rim.device.api.ui.component.LabelField;

 

import javax.microedition.io.*;

import java.io.InputStream;

import javax.microedition.media.*;

import javax.microedition.media.Manager;

import javax.microedition.media.MediaException;

import javax.microedition.media.Player;

 

class HelloWorldDemo extends UiApplication {

    public static void main(String[] args) {

        HelloWorldDemo theApp = new HelloWorldDemo();

        theApp.enterEventDispatcher();

    }

 

    private HelloWorldDemo() {

        pushScreen(new HelloWorldScreen());

    }    

}

 

final class HelloWorldScreen extends MainScreen {

    HelloWorldScreen() {

        LabelField title = new LabelField("Hello World Demo" , LabelField.ELLIPSIS | LabelField.FIELD_HCENTER);

        setTitle(title);

        try {

            String murl = "http://homepage.mac.com/brentris/my_song.mp3;deviceside=true";

            HttpConnection conn = (HttpConnection) Connector.open(murl, Connector.READ_WRITE, false);

            InputStream is = conn.openInputStream();

            Player _player = Manager.createPlayer(is, "audio/mpeg");

            _player.start();

        } catch(Exception e) {

            System.out.println("Default error connecting to stream...unrecoverable");

            System.out.println(e.getMessage());

            e.printStackTrace();

            System.out.println("error=8 " + e.getClass());

        }

    }

 

    public void close() {

        System.exit(0);

        super.close();

    }   

}

 

 

----------------------------------------------------------------------

 

Above is my entire code. It's a CLDC app built with JDE 4.6.0.12. I am using the 4.6.0.266 BlackBerry Bold 9000 Simulator and a 4.6.0 Bold 9000 actual device on AT&T.

 

In this sample I am using a Garageband MP3 I created (but my final goal is to stream a 64kbit mp3 stream, I"m hiding that for privacy reasons).

 

This one plays, but only like 50% of the time, maybe. Sometimes it launches and plays the whole song (1 minute, 32 seconds). Sometimes it starts playing but dies after about 5 seconds. Sometimes it refuses to play at all and won't even launch the application.

 

What in the world is going on here? I posted my entire code above. Somebody please help me.

 

Thanks!

Please use plain text.
New Developer
brent_glad
Posts: 66
Registered: ‎11-23-2009
My Device: Not Specified

Re: media player completely unreliable

bump. any ideas why it doesn't work reliably?

Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.2174, Z10 (STL100-3)-10.3.0.700 Dev OS, Z30 (STA100-5)-10.3.0.700 Dev OS, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: media player completely unreliable

Oops, I had skimed this earlier and didn't realize that it was streaming. Try passing the URL directly into the createPlayer function.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
New Developer
brent_glad
Posts: 66
Registered: ‎11-23-2009
My Device: Not Specified

Re: media player completely unreliable

sorry could you write out the code for me?

Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.2174, Z10 (STL100-3)-10.3.0.700 Dev OS, Z30 (STA100-5)-10.3.0.700 Dev OS, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: media player completely unreliable

I have not tried this myself, I just modified your code.

 

package com.rim.samples.device.helloworlddemo;

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;

import javax.microedition.io.*;
import java.io.InputStream;
import javax.microedition.media.*;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;

class HelloWorldDemo extends UiApplication {
    public static void main(String[] args) {
        HelloWorldDemo theApp = new HelloWorldDemo();
        theApp.enterEventDispatcher();
    }

    private HelloWorldDemo() {
        pushScreen(new HelloWorldScreen());
    }
}

final class HelloWorldScreen extends MainScreen {
    HelloWorldScreen() {
        LabelField title = new LabelField("Hello World Demo" , LabelField.ELLIPSIS | LabelField.FIELD_HCENTER);
        setTitle(title);
        try {
            String murl = "http://homepage.mac.com/brentris/my_song.mp3";
            boolean found = false;
            String[] supTypes = Manager.getSupportedContentTypes("http");
            for(int i = 0; i < supTypes.length; i++)
            {
                if(subTypes[i].equals("audio/mpeg"))
                {
                    found = true;
                    break;
                }
            }
            if(!found)
            {
                System.out.println("MP3 not supported.");
                return;
            }
            Player _player = Manager.createPlayer(murl);
            _player.start();
        } catch(Exception e) {
            System.out.println("Default error connecting to stream...unrecoverable");
            System.out.println(e.getMessage());
            e.printStackTrace();
            System.out.println("error=8 " + e.getClass());
        }
    }

    public void close() {
        System.exit(0);
        super.close();
    }
}

 

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
New Developer
brent_glad
Posts: 66
Registered: ‎11-23-2009
My Device: Not Specified

Re: media player completely unreliable

Hmm, it doesn't seem to be playing the audio. Do I need to open an httpconnection and inputstream?

Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.2174, Z10 (STL100-3)-10.3.0.700 Dev OS, Z30 (STA100-5)-10.3.0.700 Dev OS, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: media player completely unreliable

[ Edited ]

I forgot about size. I just tried it myself and got HTTP error 413 (Entity too large). Let me try one more thing and then I will post my results.

 

EDIT: I modified the code back to your original ocde but then edited it again so it buffered the file before creating the player:

HttpConnection conn = (HttpConnection) Connector.open(murl, Connector.READ, false);
	            InputStream is = conn.openInputStream();
	            
	            int len = (int)conn.getLength();
	            byte[] buf = new byte[len];
	            int bytesRead = 0;
				int readIn = 0;
				int bufferSize = 1024;
				while ((bytesRead != len) && (readIn != -1))
				{
					readIn = is.read(buf, bytesRead, bufferSize);
					bytesRead += readIn;
				}
				
	            is.close();
	            conn.close();
	            
	            ByteArrayInputStream barIS = new ByteArrayInputStream(buf);
	            Player _player = javax.microedition.media.Manager.createPlayer(barIS, "audio/mpeg");
	            _player.start();

 

The problem is getLength returns 24, I doubt your MP3 is 24 bytes. I don't know what happened but if you look at the documentation for getLength it might help you find the problem.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.