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
jefero
Posts: 19
Registered: ‎07-22-2008
My Device: Not Specified

Media Exception: Media cannot start while another media is active.

Hi everyone.

 

I'm developing an application in wich I use the J2ME camera (not invoke the BlackBerry camera application) with this code:

 

 

public void initScreen() {
		try{		
			p = Manager.createPlayer("capture://video");
			p.realize();			
			p.prefetch();

			vc = (VideoControl) p.getControl("VideoControl");
			if(vc != null){
				viewFinder = (Field)vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
				vc.setDisplayFullScreen(true);
				vc.setVisible(true);
				viewFinder.select(false);
				
				deleteAll();
	            	            
				add(viewFinder);
				
			} else {
				System.out.println("VideoControl is NULL");
			}

		} catch (Exception me){
			System.err.println(ERR_TAG + "initScreen(): " + me);
		}		
	}

 

 

After taking a photo with the camera I go back to another screen and close the J2ME camera using this code:

 

 

private void goBack(){
		synchronized(Application.getEventLock()){
			this.deleteAll();
			close();
		}
		
		try {
			p.stop();
		} catch (Exception e) {
			System.err.println(ERR_TAG + "goBack(): " + e);
		}

		p.deallocate();
		p.close();
		p = null;
		vc = null;
		viewFinder = null;	
	}	

 

Then in the screen showed after closing the camera I try to play a sound with this method:

 

 

public synchronized void playSoundOk() {
                try {
                        Class cl = Class.forName("main.Main");
                        InputStream is = cl.getResourceAsStream("/ok.wav");

                        // Create an instance of the player from the InputStream
                        Player player = javax.microedition.media.Manager.createPlayer(is,
                                        "audio/wav");
                        player.realize();
                        player.prefetch();
                        //player.setLoopCount(0);
                        player.start();
                } catch (Exception e) {
                        System.out.println("ERR: " + e.toString());
                }
        }

 

But when executing the code player.start(); this exection is thrown:

Media Exception: Media cannot start while another media is active.

I think that the "another media" is the camera. But...


Why the exception is throwed?

I think I'm doing averything ok, because I'm closing the player of the camera as you can see in the code of the method "goBack":

p.deallocate();
p.close();

How can I ensure that the camera is closed?

 

 

I'm testing this code in a BlackBery 8520 with the O.S version 4.6.1.272

 

 

Thank you very much for your help.

 

New Contributor
sidneib
Posts: 6
Registered: ‎03-24-2010
My Device: Bold

Re: Media Exception: Media cannot start while another media is active.

Hi All, 

 

I am getting the same kind of error, could someone please help with why this is happening. Cant find any information about this particular error

 

Thanks

Sid

New Contributor
sidneib
Posts: 6
Registered: ‎03-24-2010
My Device: Bold

Re: Media Exception: Media cannot start while another media is active.

Hi Jefero, i managed to fix my problem regarding this. When i look at your code it seams playSoundOK is a synchronized function so im thinking that its running in a separate thread then the function where you are taking the video recording/image taking.

 

What could be happening is that the video player hasnt correctly deallocated and closed yet so that is why you are getting the problem.

 

My code is similar. I have video taking in a seperate thread then where i play sound and i had to completly switch of the main video player first.

 

doing deallocate() did the job i didnt even have to call close(). I called deallocate as i needed to wakeup the video player again after i played the sound so if i closed it i would need to create the player again as there is no state that leads me back to start from close , though if i used deallocate i could realize the same player.

 

HTH

Sid

New Developer
jefero
Posts: 19
Registered: ‎07-22-2008
My Device: Not Specified

Re: Media Exception: Media cannot start while another media is active.

Thanks for your reply sidneib.

 

So what you are telling me is that in my method goBack() I don't close the player "p" and only deallocate it.

So, now the method goBack will look as following:

private void goBack(){
synchronized(Application.getEventLock()){
this.deleteAll();
close();
}

try {
p.stop();
} catch (Exception e) {
System.err.println(ERR_TAG + "goBack(): " + e);
}

p.deallocate();
p = null;
vc = null;
viewFinder = null;
}

 

Doing this and not closing the player will allow the "player" of this method to start properly:

public synchronized void playSoundOk() {
try {
Class cl = Class.forName("main.Main");
InputStream is = cl.getResourceAsStream("/ok.wav");

// Create an instance of the player from the InputStream
Player player = javax.microedition.media.Manager.createPlayer(is,
"audio/wav");
player.realize();
player.prefetch();
//player.setLoopCount(0);
player.start();
} catch (Exception e) {
System.out.println("ERR: " + e.toString());
}
}

 

I will test this and then I will tell you the results.

 

Thanks.

 

 

New Contributor
sidneib
Posts: 6
Registered: ‎03-24-2010
My Device: Bold

Re: Media Exception: Media cannot start while another media is active.

Hi Jefero,

 

No not exactly, in my testing that i have done, close will not allow you to use that player again because if you consult the documentation .close() is the end state for the player and there is no transition from close to any other state. 

 

So basically if you want to use that player again its advised you just deallocate instead of close.

 

HOWEVER, to answer your question my assumption is that you are calling playSoundOk from a seperate thread to the thread that is doing the video recording etc. I am guessing this because your playSoundOk function is synchronized which im guessing is done so that you have thread protected it.

 

What you need to make sure is that the function where you deallocate/close your video player has completly finished running before you call playSoundOk from another thread.

 

I made this mistake where i had a sound to play in a seperate thread to a thread where i had video recording also and it failed similar to your situation. I had to make sure that the deallocate function in the main thread (that i called on the video player in the main thread) had finished running before i could start another player to play any sound.

 

Main point, confirm that your main video player object has been properly closed/deallocated. :smileyhappy:

Contributor
arirushan
Posts: 13
Registered: ‎03-30-2010
My Device: 8520

Re: Media Exception: Media cannot start while another media is active.

I face similar problem too.

It will be helpful if you can post your code snippet to overcome this problem.

New Member
MikeZ90
Posts: 1
Registered: ‎07-20-2010
My Device: Bold 9650

Re: Media Exception: Media cannot start while another media is active.

Has anyone found a solution to this problem?  I encounter this problem in an application I have written that uses the player interface both to play a .wav file and to capture photos.  The application alternates between playing the .wav file and then taking a photo.  The first .wav plays fine, then the camera takes the photo, but when the .wav is to be played again, this exception is thrown.  I make sure that the player is closed in both places before calling the other player's start method.

 

    public void playerUpdate(Player player, String event, Object eventData)
    {
        if (event.equals(PlayerListener.CLOSED))
        {

             //code for calling the other player

        }

     }

 

The weirdest part is that this problem does not occur on a Bold 9650 but DOES occur on the Bold 9700...  Any help is greatly appreciated.

Contributor
bfin
Posts: 13
Registered: ‎08-11-2010
My Device: Not Specified

Re: Media Exception: Media cannot start while another media is active.

Has anyone figured out this problem?  I am having the same issue.  It works on the 9650 but not the 9700.  Please help!

Contributor
Consilium
Posts: 13
Registered: ‎07-12-2010
My Device: Any

Re: Media Exception: Media cannot start while another media is active.

Im also getting the error.

 

javax.microedition.media.MediaException: Media cannot start while another media is active.

New Developer
hussain_afsar
Posts: 4
Registered: ‎08-30-2010
My Device: Blackberry 8520

Re: Media Exception: Media cannot start while another media is active.

[ Edited ]

Hi,

Has anybody able to solve this issue.I am also getting the same Exception :

 MediaException : Media cannot start while another media is active

 

I am using BB8520 for my development.

I am capturing photo using j2me MMAPI and after that i am playing a video.It is capturing the photo but when it tries to start the player for playing video it is giving exception.

 

I have written method which I am calling before playing video to close the player created for camera.

 

 

public void stopPlayer()
    {
       // removeProgressBar();
        if (player != null) 
        {
            if(videoControl!=null)
            videoControl.setVisible(false);
            videoControl = null;
            try 
            {
                player.removePlayerListener(this);
                closeplayer();
                player = null;
            } 
            catch (MediaException me) 
            {
                me.printStackTrace();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    void closeplayer() throws MediaException 
    {
        if (player != null) 
        {
            if(player.getState() == Player.STARTED) 
            {
                    player.stop();
            }
            if(player.getState() == Player.PREFETCHED) 
            {
                player.deallocate();
            }
            if(player.getState() == Player.REALIZED ||player.getState() == Player.UNREALIZED) 
            {
                player.close();
            }
        }
        player = null;
    }  

 

public void stopPlayer()    { 

 

 

  if (player != null)         

{

        if(videoControl!=null)            

videoControl.setVisible(false);            

videoControl = null;            

try             

{                

player.removePlayerListener(this);                

closeplayer();                

player = null;            

}             

catch (MediaException me)             

{              

  me.printStackTrace();            

}            

catch(Exception e)            

{                

e.printStackTrace();          

 }        

}  

 }    

void closeplayer() throws MediaException     

{        

if (player != null)         

{            

if(player.getState() == Player.STARTED)             

{                    

player.stop();            

}            

if(player.getState() == Player.PREFETCHED)             

{                

player.deallocate();          

 }          

 if(player.getState() == Player.REALIZED ||player.getState() == Player.UNREALIZED)             

{                

player.close();          

 }        

}      

 player = null;    

}  

 

After calling these methods , I am also printing the value of player and videocontrol which is getting null.

Then I am starting the player to play video but it giving "MediaException : Media cannot start while another media is active".

I tried every possible way by keeping in same thread and also with different thread but the result is same.

But the same thing is working fine for BB 8900 without any exception.

 

Please suggest some solution, it very critical for my work to complete.

 

Thanx in advance.