×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Jun 2010
    Location
    Palestine/Ramallah
    Posts
    76

    Question OutOfMemorException when playing 800KB file

    I use lwuit
    and i run my application on N73
    when i play amedia more than 3 time an OutOfMemorException raised
    this is code snippet in command

    try
    {
    Thread thread=new Thread(new Runnable() {
    public void run() {
    matchingMenuButtonsMediaPlayer=null;
    matchingMenuButtonsMediaPlayer=new MediaPlayer(videoFormTitle, me, videoFileName, false, true);
    matchingMenuButtonsMediaPlayer.show();

    }
    });

    thread.start();
    } catch (Exception e)
    {
    e.printStackTrace();
    parentForm.show();
    }

    The media player class is here


    package com.alameenTech.systemsOfTheHumanBody.digestiveSystem;

    import com.sun.lwuit.Button;
    import com.sun.lwuit.Command;
    import com.sun.lwuit.Container;
    import com.sun.lwuit.Form;
    import com.sun.lwuit.MediaComponent;
    import com.sun.lwuit.events.ActionEvent;
    import com.sun.lwuit.events.ActionListener;
    import com.sun.lwuit.layouts.BorderLayout;
    import com.sun.lwuit.layouts.GridLayout;
    import java.io.IOException;
    import java.io.InputStream;
    import javax.microedition.media.Manager;
    import javax.microedition.media.MediaException;
    import javax.microedition.media.Player;
    import javax.microedition.media.PlayerListener;
    import javax.microedition.media.control.VolumeControl;

    public class MediaPlayer extends Form implements PlayerListener
    {
    private MediaComponent mediaComponent=null;
    private Player player=null;
    private Form parentForm=this;
    private Button pauseButton=null,increaseVolume=null,decreaseVolume=null;
    private boolean returnWhenFinished=true,viewControls=false,isBackAdded=false;
    private String mediaFileName=null;
    private Container controls=null;
    private Command back=null;
    private VolumeControl vc=null;

    public MediaPlayer( String title,final Form parentForm,String mediaName,boolean returnWhenFinished, boolean viewControls)
    {
    super(title);
    this.parentForm=parentForm;
    this.returnWhenFinished=returnWhenFinished;
    this.viewControls=viewControls;
    this.mediaFileName=mediaName;
    setLayout(new BorderLayout());
    getStyle().setBgColor(0x000000,false);

    try
    {
    back=new Command("back");
    addCommandListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    closePlayerCompletely();
    parentForm.show();
    System.out.println(Runtime.getRuntime().freeMemory());
    }
    });

    if(!this.returnWhenFinished)
    {
    addCommand(back);
    isBackAdded=true;
    }

    if(this.viewControls)
    addControls();

    startPlayer();
    } catch (Exception ex)
    {
    closePlayer();
    this.parentForm.show();
    }

    }

    private void startPlayer()
    {
    try
    {
    createMediaPlayer();
    if(this.viewControls)
    {
    pauseButton.setText("pause");
    increaseVolume.setEnabled(true);
    decreaseVolume.setEnabled(true);
    vc=(VolumeControl) player.getControl("VolumeControl");
    }

    } catch (Exception ex)
    {
    closePlayer();
    parentForm.show();
    }
    }

    private void closePlayerCompletely()
    {
    try
    {
    if (player != null)
    {
    player.removePlayerListener(this);
    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;
    if(viewControls&&vc!=null)
    vc = null;
    if(this.getContentPane().contains(mediaComponent))
    this.getContentPane().removeComponent(mediaComponent);
    if(mediaComponent!=null)
    mediaComponent=null;

    } catch (Exception ex)
    {
    ex.printStackTrace();
    parentForm.show();
    }
    }
    public void playerUpdate(Player player, String event, Object eventData)
    {
    try
    {
    if(event.equals(PlayerListener.END_OF_MEDIA) && returnWhenFinished)
    {
    closePlayerCompletely();
    parentForm.show();
    }

    else if(event.equals(PlayerListener.END_OF_MEDIA) && this.viewControls)
    pauseButton.setText("start");
    } catch (Exception e)
    {
    closePlayerCompletely();
    parentForm.show();
    }

    }
    To be continued in next thread

  2. #2
    Registered User
    Join Date
    Jun 2010
    Location
    Palestine/Ramallah
    Posts
    76

    Re: OutOfMemorException when playing 800KB file

    private void createMediaPlayer()
    {
    try
    {
    InputStream is=getClass().getResourceAsStream("/videos/"+mediaFileName+".3gp");
    player = Manager.createPlayer(is, "video/3gpp");
    player.addPlayerListener(this);
    player.realize();
    player.prefetch();
    mediaComponent=new MediaComponent(player);
    addComponent(BorderLayout.CENTER,mediaComponent);

    if(returnWhenFinished)
    mediaComponent.setFullScreen(true);

    } catch (MediaException ex)
    {
    this.parentForm.show();
    }
    catch(IOException ioe)
    {
    this.parentForm.show();
    }
    }

    public void setMediaFileName(String mediaFileName)
    {
    this.mediaFileName=mediaFileName;
    }
    public String getMediaFileName()
    {
    return this.mediaFileName;
    }
    public void setViewControls(boolean viewControls)
    {
    this.viewControls=viewControls;
    if(viewControls)
    {
    if (!this.getContentPane().contains(controls))
    addControls();
    }
    else
    if (this.getContentPane().contains(controls))
    this.getContentPane().removeComponent(controls);
    }
    public boolean isViewControls()
    {
    return this.viewControls;
    }
    public void setReturnWhenFinished(boolean returnWhenFinished)
    {
    this.returnWhenFinished=returnWhenFinished;
    if(returnWhenFinished)
    {
    if(this.isBackAdded)
    this.removeCommand(back);
    }
    else
    if(!this.isBackAdded)
    addCommand(back);
    }
    public boolean isReturnWhenFinished()
    {
    return this.returnWhenFinished;
    }
    public void setParentForm(Form parentForm)
    {
    this.parentForm=parentForm;
    }
    public Form getParentForm()
    {
    return this.parentForm;
    }

    /**
    * To add volume and playing controls
    * to increase or decrease media volume
    * and to stop or play the media
    */
    private void addControls()
    {
    try {
    controls=new Container(new GridLayout(1, 3));

    pauseButton=new Button("pause");
    pauseButton.setAlignment(Button.CENTER);
    pauseButton.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    try {

    if(player.getState()==player.STARTED)
    {
    mediaComponent.stop();
    player.stop();
    pauseButton.setText("start");
    }
    else if(player.getState()==player.PREFETCHED)
    {

    mediaComponent.start();
    player.start();
    pauseButton.setText("pause");
    }
    } catch (MediaException ex)
    {
    closePlayer();
    parentForm.show();
    }
    }
    });
    controls.addComponent(pauseButton);



    increaseVolume=new Button("V++");
    decreaseVolume=new Button("V--");

    increaseVolume.setAlignment(Button.CENTER);
    increaseVolume.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    if(vc.getLevel()<=90)
    vc.setLevel(vc.getLevel()+10);
    if(vc.getLevel()==100)
    increaseVolume.setEnabled(false);
    else
    increaseVolume.setEnabled(true);

    if(!decreaseVolume.isEnabled())
    decreaseVolume.setEnabled(true);
    }
    });
    controls.addComponent(increaseVolume);




    decreaseVolume.setAlignment(Button.CENTER);
    decreaseVolume.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    VolumeControl vc=(VolumeControl) player.getControl("VolumeControl");
    if(vc.getLevel()>=10)
    vc.setLevel(vc.getLevel()-10);
    if(vc.getLevel()==0)
    decreaseVolume.setEnabled(false);
    else
    decreaseVolume.setEnabled(true);
    if(!increaseVolume.isEnabled())
    increaseVolume.setEnabled(true);
    }
    });
    controls.addComponent(decreaseVolume);
    addComponent(BorderLayout.SOUTH,controls);

    } catch (Exception e)
    {
    closePlayer();
    parentForm.show();
    }
    //----------------------------------

    }

    protected void onShowCompleted() {
    if(!mediaComponent.isVisible())
    mediaComponent.setVisible(true);
    mediaComponent.start();
    super.onShowCompleted();
    }


    }

    Please Help me to Solve This Problem

  3. #3
    Registered User
    Join Date
    Jun 2010
    Location
    Palestine/Ramallah
    Posts
    76

    Re: OutOfMemorException when playing 800KB file

    The problem solved by calling
    is.close();
    when i close the player

  4. #4
    Registered User
    Join Date
    Jun 2010
    Location
    Palestine/Ramallah
    Posts
    76

    Thumbs up Re: OutOfMemorException when playing 800KB file

    This is the complete solution,

    PHP Code:
    package com.alameenTech.directories.PalestineDirectory;

    import com.sun.lwuit.Button;
    import com.sun.lwuit.Command;
    import com.sun.lwuit.Container;
    import com.sun.lwuit.Form;
    import com.sun.lwuit.MediaComponent;
    import com.sun.lwuit.events.ActionEvent;
    import com.sun.lwuit.events.ActionListener;
    import com.sun.lwuit.layouts.BorderLayout;
    import com.sun.lwuit.layouts.GridLayout;
    import java.io.IOException;
    import java.io.InputStream;
    import javax.microedition.media.Manager;
    import javax.microedition.media.MediaException;
    import javax.microedition.media.Player;
    import javax.microedition.media.PlayerListener;
    import javax.microedition.media.control.VolumeControl;
    /**
     * This class to create a player to play .3gp video format
     * @author Mohammad
     */
    public class MediaPlayer extends Form implements PlayerListener
    {
        private 
    MediaComponent mediaComponent=null;
        private 
    Player player=null;
        private 
    Form parentForm=this;
        private 
    Button pauseButton=null /* To stop playing the video file */
                      
    ,increaseVolume=null /*To increase the volume of the speakers voice*/
                      
    ,decreaseVolume=null;/*To decrease the volume of the speakers voice*/
        
    private boolean returnWhenFinished=true,/*to stop close this form and showing the parent when the player reaches the limit*/
                        
    viewControls=false,  /*To view the video controls or not*/
                        
    isBackAdded=false/* if the back command added */
        
    private String videoFileName=null;
        private 
    Container controls=null;/*container to holds control*/
        
    private Command back=null;/*to be shown if the returnWhenFinished flag setted to false*/
        
    private VolumeControl vc=null;
        private 
    InputStream is;
    /**
     * @param title : the title of the video form
     * @param parentForm : the parent form which will be displayed after playing finished
     * @param videoFileName : the name of the video file
     * @param returnWhenFinished : flag to stop showing this form and showing the parent form when the video finished
     * @param viewControls : flag to view or not video controls
     */
        
    public MediaPlayerString title,final Form parentForm,String videoFileName,boolean returnWhenFinishedboolean viewControls)
        {
            
    super(title);
            
    this.parentForm=parentForm;
            
    this.returnWhenFinished=returnWhenFinished;
            
    this.viewControls=viewControls;
            
    this.videoFileName=videoFileName;

            
    setLayout(new BorderLayout());
            
    getStyle().setBgColor(0x000000,false);

            try
            {
                
    back=new Command("back");
                
    addCommandListener(new ActionListener()
                {
                    public 
    void actionPerformed(ActionEvent ae)
                    {
                        
    closePlayer();
                        
    System.gc();
                        
    Runtime.getRuntime().gc();
                        
    parentForm.show();
                    }
                });

                if(!
    this.returnWhenFinished)
                {
                    
    addCommand(back);
                    
    isBackAdded=true;
                }

                if(
    this.viewControls)
                    
    addControls();

                
    createMediaPlayer();
                
            } catch (
    Exception ex)
            {
                
    closePlayer();
                
    this.parentForm.show();
            }

        }
    /**
     * To create the player object
     */
       
    private void createMediaPlayer()
        {
            try
            {
                
    is=getClass().getResourceAsStream("/"+videoFileName+".3gp");
                
    player Manager.createPlayer(is"video/3gpp");
                
    player.addPlayerListener(this);
                
    player.realize();
                
    player.prefetch();

                
    mediaComponent=new MediaComponent(player);
                
    addComponent(BorderLayout.CENTER,mediaComponent);
                if(
    returnWhenFinished)
                    
    mediaComponent.setFullScreen(true);

                if(
    this.viewControls)
                {
                    
    pauseButton.setText("pause");
                    
    increaseVolume.setEnabled(true);
                    
    decreaseVolume.setEnabled(true);
                    
    vc=(VolumeControlplayer.getControl("VolumeControl");
                }

            } catch (
    MediaException ex)
            {
                
    this.parentForm.show();
            }
            catch(
    IOException ioe)
            {
                
    this.parentForm.show();
            }
        }

     
    /**
     * This method must be called after creating the object of MediaPlayer Class
     * it is to start playing the video
     */
       
    public void startPlayer()
        {
           if(!
    mediaComponent.isVisible())
               
    mediaComponent.setVisible(true);

           try {
               if(
    player!=null)
               {
                   if (
    player.getState() == player.UNREALIZED)
                       
    player.realize();

                   if(
    player.getState() != player.PREFETCHED)
                       
    player.prefetch();

                   
    player.start();
               }
               else
               {
                   
    System.gc();
                   
    Runtime.getRuntime().gc();

                   
    parentForm.show();
                }
            } catch (
    Exception ex) {
            }
        }

    /**
     * To close the player and the streaming
     * and to remove the controls and mediaComponent 
     */
       
    private void  closePlayer()
       {
           if (
    player != null)
           {
               
    player.removePlayerListener(this);
               try
               {
                   
    player.stop();
                   
    is.close();
                   
    is=null;
                   
    player.deallocate();
                   
    player.deallocate();
                   
    player.close();
               } catch (
    Exception ex
               {
                   
    ex.printStackTrace();
               }
           }
           
    player null;
           if(
    viewControls&&vc!=null)
               
    vc null;

           if(
    this.getContentPane().contains(mediaComponent))
               
    this.getContentPane().removeComponent(mediaComponent);

           if(
    mediaComponent!=null)
               
    mediaComponent=null;
       }
       
    /**
     * It is an implemented method from PlayerListener interface
     * @param player : my player
     * @param event  : player status like END_OF_MEDIA,...
     * @param eventData
     */
       
    public void playerUpdate(Player playerString eventObject eventData)
        {
           try
           {
               if(
    event.equals(PlayerListener.END_OF_MEDIA) && returnWhenFinished)
               {
                   
    closePlayer();
                   
    parentForm.show();
               }
               else if(
    event.equals(PlayerListener.END_OF_MEDIA) && this.viewControls)
                   
    pauseButton.setText("start");

           } catch (
    Exception e)
           {
               
    closePlayer();
               
    parentForm.show();
           }
       }

    /**
     * To add volume and playing controls
     * to increase or decrease media volume
     * and to stop or play the media
     */
       
    private void addControls()
       {
           try {
               
    controls=new Container(new GridLayout(13));
               
    pauseButton=new Button("pause");
               
    pauseButton.setAlignment(Button.CENTER);
               
    pauseButton.addActionListener(new ActionListener()
               {
                   public 
    void actionPerformed(ActionEvent ae)
                   {
                       try {
                           if(
    player.getState()==player.STARTED)
                           {
                               
    player.stop();
                               
    pauseButton.setText("start");
                           }
                           else if(
    player.getState()==player.PREFETCHED)
                           {
                               
    player.start();
                               
    pauseButton.setText("pause");
                           }
                       } catch (
    MediaException ex)
                       {
                           
    closePlayer();
                           
    parentForm.show();
                       }
                   }
                });

                
    controls.addComponent(pauseButton);

                
    increaseVolume=new Button("V++");
                
    decreaseVolume=new Button("V--");

                
    increaseVolume.setAlignment(Button.CENTER);
                
    increaseVolume.addActionListener(new ActionListener()
                {
                    public 
    void actionPerformed(ActionEvent ae)
                    {
                        if(
    vc.getLevel()<=90)
                            
    vc.setLevel(vc.getLevel()+10);

                        if(
    vc.getLevel()==100)
                            
    increaseVolume.setEnabled(false);
                        else
                            
    increaseVolume.setEnabled(true);

                        if(!
    decreaseVolume.isEnabled())
                            
    decreaseVolume.setEnabled(true);
                    }
                });

                
    controls.addComponent(increaseVolume);

                
    decreaseVolume.setAlignment(Button.CENTER);
                
    decreaseVolume.addActionListener(new ActionListener()

                {
                    public 
    void actionPerformed(ActionEvent ae)
                    {
                        
    VolumeControl vc=(VolumeControlplayer.getControl("VolumeControl");
                        if(
    vc.getLevel()>=10)
                            
    vc.setLevel(vc.getLevel()-10);
                        if(
    vc.getLevel()==0)
                            
    decreaseVolume.setEnabled(false);
                        else
                            
    decreaseVolume.setEnabled(true);
                        if(!
    increaseVolume.isEnabled())
                            
    increaseVolume.setEnabled(true);
                    }
                });

                
    controls.addComponent(decreaseVolume);
                
    addComponent(BorderLayout.SOUTH,controls);

           } catch (
    Exception e)
           {
               
    closePlayer();
               
    parentForm.show();
           }
           
    //----------------------------------
        
    }




Similar Threads

  1. Problem playing tones after playing a sound file
    By mattbrenner in forum Mobile Java Media (Graphics & Sounds)
    Replies: 4
    Last Post: 2010-08-18, 05:35
  2. S60 3rd Edition SDK wont accept above 800KB
    By vijay.chaudhari in forum Mobile Java Tools & SDKs
    Replies: 5
    Last Post: 2009-07-14, 14:41
  3. playing a GSM6.10 Compressed wave file and PCM file
    By new2symb in forum Symbian Media (Closed)
    Replies: 1
    Last Post: 2007-06-15, 17:03
  4. Replies: 0
    Last Post: 2007-06-09, 10:06
  5. playing a GSM6.10 Copressed wave file and PCM file
    By new2symb in forum Open C/C++
    Replies: 2
    Last Post: 2007-06-09, 09:54

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •