×

Discussion Board

Results 1 to 14 of 14
  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    ahmedabad
    Posts
    32

    J2ME Audio Player from FILE CONNECTION

    I want to build up an AUDIO player which scans all mp3 or wav files from the memory card. and same will be added in to the playlist.

    I have tried it to implement but each time when file is getting play it is asking for security check. I am storing path in the recordstore. Please let me know how to implement.

    Thanks
    jigar rahtod

  2. #2
    Regular Contributor
    Join Date
    Dec 2011
    Posts
    174

    Re: J2ME Audio Player from FILE CONNECTION

    Hi rathodjigar,

    You need to sign your application to avoid security questions. And then set read user data setting to "always allowed".

    Link to signing a MIDlet: http://library.developer.nokia.com/i...1141E036C.html

    -tiviinik

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    ahmedabad
    Posts
    32

    Re: J2ME Audio Player from FILE CONNECTION

    Hi Tiviinik,
    See what i want to do is.. Just want to have one option from where user can access a memory card. then he will select one wav file and then it should play each an after 5 min or so.
    public class file_browse extends MIDlet implements CommandListener, PlayerListener {
    private String currDirName;
    private final Command exitCommand = new Command("Exit", Command.EXIT, 1);
    private Command view = new Command("View", Command.ITEM, 1);
    private Command back = new Command("Back", Command.BACK, 2);
    private Command exit = new Command("Exit", Command.EXIT, 3);
    private final Command selectCommand = new Command("Select", Command.ITEM, 1);
    private final Command playlistCommand = new Command("playlist", Command.ITEM, 1);
    private final static String UP_DIRECTORY = "/";
    private final static String MEGA_ROOT = "/";
    private final static String SEP_STR = "/";
    private final static char SEP = '/';
    private List list;
    private int count = 0;
    private Vector mediaFiles;
    private Player p,player;
    private MediaCanvas canvas;
    private Display display;

    public file_browse()
    {

    Display.getDisplay(this).setCurrent(list);
    System.out.println("here1");
    currDirName = MEGA_ROOT;
    }

    public void startApp()
    {
    boolean isAPIAvailable = false;
    if (System.getProperty("microedition.io.file.FileConnection.version") != null)
    {
    isAPIAvailable = true;
    System.out.println("hi");
    try
    {
    showCurrDir();
    System.out.println("h5");
    }
    catch (SecurityException e)
    {
    System.out.println(e);
    }
    catch (Exception e) {System.out.println(e);}
    }
    else
    {
    StringBuffer splashText = new StringBuffer
    (getAppProperty("MIDlet-Name")).append("\n").append(
    getAppProperty("MIDlet-Vendor")).
    append(isAPIAvailable?"":"\nFileConnection API not available");
    Alert splashScreen = new Alert(null,splashText.toString(),
    null,AlertType.INFO);
    splashScreen.setTimeout(3000);
    Display.getDisplay(this).setCurrent(splashScreen);
    }

    }


    public void destroyApp(boolean cond)
    {
    notifyDestroyed();
    }

    public void commandAction(Command c, Displayable d)
    {
    System.out.println("updir:"+UP_DIRECTORY);
    if (c == view)
    {
    List curr = (List)d;
    String path= null;
    final String currFile = curr.getString(curr.getSelectedIndex());
    path = path + currFile;
    System.out.println("currFile:"+currFile);

    new Thread(new Runnable()
    {
    public void run()
    {
    if (currFile.endsWith(SEP_STR) || currFile.equals(UP_DIRECTORY))
    {
    System.out.println("FIRST");

    traverseDirectory(currFile);
    }
    else
    {


    try {

    String abc="";
    abc = "file:///"+currDirName+currFile;
    System.out.print("COMPLETE PATH FOR THE APPLICATION "+ abc);
    FileConnection connection = (FileConnection) Connector.open(abc);
    InputStream inStream = null;
    inStream = connection.openInputStream();
    String patht = inStream.toString();

    // InputStream ins = getClass().getResourceAsStream("/loading.gif");
    // System.out.print("THIS IS FIRST LINE"+PlayerListener.END_OF_MEDIA);
    player = Manager.createPlayer(inStream, "audio/x-wav");

    player.prefetch();
    // player.addPlayerListener(null);

    player.setLoopCount(5);
    try
    {
    Thread.sleep(4000);
    }catch(Exception e){

    }
    player.prefetch();

    player.start();
    if(PlayerListener.END_OF_MEDIA.equals("endOfMedia"))
    {
    System.out.print("INSIDE WAIT TIMING");
    try
    {
    Thread.sleep(1000);
    }catch(Exception e){

    }
    }
    player.realize();

    if(PlayerListener.END_OF_MEDIA.equals("endOfMedia"))
    {
    System.out.print("INSIDE WAIT TIMINGS");
    try
    {
    Thread.sleep(1000);
    }catch(Exception e){

    }
    }

    } catch (IOException ex) {
    ex.printStackTrace();
    } catch (MediaException ex) {
    ex.printStackTrace();
    }

    }
    }
    } ).start();


    }
    else if (c == back)
    {
    showCurrDir();
    }
    else if (c == exit)
    {
    destroyApp(false);
    }
    }

    void showCurrDir()
    {
    Enumeration e;
    FileConnection currDir = null;
    List browser;
    try
    {
    System.out.println("In showCurrDir");
    System.out.println("mega_root:"+MEGA_ROOT+"cur_dir:"+currDirName);
    if (MEGA_ROOT.equals(currDirName))
    {
    e = FileSystemRegistry.listRoots();
    browser = new List(currDirName, List.IMPLICIT);
    System.out.println("here");
    }
    else
    {
    System.out.println("connector");
    currDir = (FileConnection)Connector.open("file://localhost/" + currDirName);
    System.out.println("curr_dir:"+currDir);

    e = currDir.list();
    browser = new List( currDirName, List.IMPLICIT);
    browser.append(UP_DIRECTORY,null);
    }
    while (e.hasMoreElements())
    {
    System.out.println("h2");
    String fileName = (String)e.nextElement();
    System.out.println("fileName:"+fileName+" char_at:"+fileName.charAt(fileName.length()-1));

    if (fileName.charAt(fileName.length()-1) == SEP)
    {
    browser.append(fileName,null);

    }

    else
    {
    System.out.println("h4");

    browser.append(fileName,null);


    //form.append(image);
    }
    }
    browser.setSelectCommand(view);
    browser.addCommand(exit);
    browser.setCommandListener(this);
    if (currDir != null)
    {
    currDir.close();
    }
    Display.getDisplay(this).setCurrent(browser);
    }
    catch (IOException ioe)
    {
    System.out.println(ioe);
    }
    }
    void traverseDirectory(String fileName)
    {
    System.out.println("Jigar fileName:"+fileName+"Jigar cur_dir:"+currDirName+"Jigar mega_root:"+MEGA_ROOT);

    if (currDirName.equals(MEGA_ROOT))
    {
    if (fileName.equals(UP_DIRECTORY))
    {
    // can not go up from MEGA_ROOT
    return;
    }
    currDirName = fileName;
    }
    else if (fileName.equals(UP_DIRECTORY))
    {
    System.out.println("up");
    // Go up one directory
    // TODO use setFileConnection when implemented
    int i = currDirName.lastIndexOf(SEP, currDirName.length()-2);
    if (i != -1)
    {
    currDirName = currDirName.substring(0, i+1);
    }
    else
    {
    currDirName = MEGA_ROOT;
    }
    }
    else
    {
    currDirName = currDirName + fileName;
    }
    showCurrDir();
    }

    }
    This is what my current code.
    p;lease let me know how can i do the same.
    thanks

  4. #4
    Regular Contributor
    Join Date
    Dec 2011
    Posts
    174

    Re: J2ME Audio Player from FILE CONNECTION

    Hi rathodjigar,

    Could you clarify a bit more what your problem is. You mentioned that you get a security check when trying to play an audio file. What kind of security check do you mean?

    -tiviinik

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    ahmedabad
    Posts
    32

    Re: J2ME Audio Player from FILE CONNECTION

    Hi ,

    See i want to build a simple demo in which i am able to browse through the file. and am using below code for playing a file. But i my requirment is i want to play the selected file first time then want a delay of 2 min and then again it should automatically start playing the same again. Now for this i am getting file permission from user again and again.

    void showFile(String fileName) {
    Form creator = new Form("Media Player");
    try {
    FileConnection fc =
    (FileConnection)Connector.open("file://localhost/" + currDirName + fileName);

    if (!fc.exists()) {
    throw new IOException("File does not exists");
    }
    Vector v = new Vector();

    String totalpath = currDirName + fileName;
    System.out.print("FINAL PATH FOR THE FILE "+ totalpath);

    v.addElement(totalpath);
    v.addElement(totalpath);
    InputStream fis = fc.openInputStream();
    // String finalcall = fis.toString();


    for (int i = 0 ; i < v.size() ; i ++) {
    String address = (String) v.elementAt(i);
    //play here the audio file
    player = Manager.createPlayer(fis, "audio/x-wav");
    // player.prefetch();
    //player.setLoopCount(5);
    player.setLoopCount(1);
    player.prefetch();
    long i2 = (long) player.getDuration();
    long TimeInMilliseconds;
    //Code here to get TimeInMilliseconds

    int MM, SS;
    MM = (int)((i2 + 500) / 60000);
    SS = (int)((i2 + 500) / 1000) % 60;
    System.out.println("MINUTES" + MM + "SECONDS"+ SS);
    System.out.println("Player opened. Playing requested sound." + player.getState());
    player.start();
    System.out.println("Player opened. Playing requested sound." + player.getState());
    int i1 = player.getState();
    // int i2 = (int) player.getMediaTime();
    // System.out.println("REALIZED" + i2);
    System.out.println("Player opened. Playing requested sound." + player.getState());
    if(player.getState() == Player.TIME_UNKNOWN)
    {
    System.out.println("REALIZED" + i1);
    }

    //You can use here thread.wait() and Thread.notify to handle the event of beggining of an audio file and the ending of it. Do not play them all in the same time. You have to wait for the audio file to end before playing the other one
    }

    player.realize();

    if(PlayerListener.END_OF_MEDIA.equals("endOfMedia"))
    {
    System.out.print("INSIDE WAIT TIMINGS");
    try
    {
    Thread.sleep(2000);

    }catch(Exception e){

    }
    }


    creator.addCommand(back);
    creator.addCommand(exit);
    creator.setCommandListener(this);

    Display.getDisplay(this).setCurrent(creator);

    // player.realize();
    // fis.close();
    // fc.close();
    // TextBox viewer =
    // new TextBox("View File: " + fileName, null, 1024,
    // TextField.ANY | TextField.UNEDITABLE);


    // viewer.addCommand(back);
    // viewer.addCommand(exit);
    // viewer.setCommandListener(this);


    //Display.getDisplay(this).setCurrent(viewer);
    } catch (Exception e) {
    Alert alert =
    new Alert("Error!",
    "Can not access file " + fileName + " in directory " + currDirName +
    "\nException: " + e.getMessage(), null, AlertType.ERROR);
    alert.setTimeout(Alert.FOREVER);
    Display.getDisplay(this).setCurrent(alert);
    }
    }

  6. #6
    Super Contributor
    Join Date
    Nov 2003
    Location
    Bangalore , India
    Posts
    4,429

    Re: J2ME Audio Player from FILE CONNECTION

    As stated in the previous post, you need to Sign your app to avoid the permission pop up messages.

    Have a look at Java ME signing for dummies

    Hope this helps ?

    Regards
    Gopal

  7. #7
    Regular Contributor
    Join Date
    Dec 2011
    Posts
    174

    Re: J2ME Audio Player from FILE CONNECTION

    Hi rathodjigar,

    You could set the application access option to 'ask first time' only, so that it wont keep asking it again and again after the first prompt.

    Go to application options and select 'application access->data access->read user data' and set it to 'ask first time'.

    -tiviinik

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    ahmedabad
    Posts
    32

    Re: J2ME Audio Player from FILE CONNECTION

    Hi Gopal,

    I have gone through the above link. That is correct. But after accessing the file i want to play it continuously with some specific interval.

    For example if i am selecting and playing any song then there is a method which will continues play the song file after each and every one minute. Its working fine in the emulator using THREAD.SLEEP for specific interval. But after installing the same in the device is creating the problem. Its not at all getting stop.

    Device which i am using is NOKIA C2 .

    Can you please let me know the way to have another option than thread.

    Thank
    Jigar

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    ahmedabad
    Posts
    32

    Re: J2ME Audio Player from FILE CONNECTION

    HI tiviinik,

    From where i can set this option . Is this in the device or in SDK?

    Thansk
    Jigar

  10. #10
    Regular Contributor
    Join Date
    Dec 2011
    Posts
    174

    Re: J2ME Audio Player from FILE CONNECTION

    You can set the option from the device.

    -tiviinik

  11. #11
    Registered User
    Join Date
    Nov 2011
    Location
    ahmedabad
    Posts
    32

    Re: J2ME Audio Player from FILE CONNECTION

    Hi tiviinik,

    As i am using S40 series mobile its not available. I am using Nokia C2.

    Thanks
    Jigar Rathod

  12. #12
    Regular Contributor
    Join Date
    Dec 2011
    Posts
    174

    Re: J2ME Audio Player from FILE CONNECTION

    It's available for Series 40 devices from Series 40 6th Edition, Feature Pack 1 and onwards.
    http://library.developer.nokia.com/i...EBE86A60D.html

    -tiviinik

  13. #13
    Registered User
    Join Date
    Nov 2011
    Location
    ahmedabad
    Posts
    32

    Re: J2ME Audio Player from FILE CONNECTION

    hi tiviinik,

    Thanks for your prompt reply.

    Accessing from the memory card a particular wav file and playing it. This programming is done. But i after playing it first time i want a delay of 1 min. Now if i use loopcount as -1 then it wont be useful . It will be continuously playing in the player.

    My problem is when i am using Thread.Sleep(5000); Then in the emulator it is getting stop for 5 secs and started repeat it again. Which is fine n correct. But when i am generating the jar and installing it , its not happening. Can you please give me a idea to implement this.

    thanks
    Jigar

  14. #14
    Regular Contributor
    Join Date
    Dec 2011
    Posts
    174

    Re: J2ME Audio Player from FILE CONNECTION

    Hi Jigar,

    Have you tried playing the sound once and when the End of Media in the playerlistener comes restart the thread and make it wait for 1 min and then restart the player to play the sound.

    -tiviinik

Similar Threads

  1. How pause the player while playing the MP3 audio file?
    By bhakki in forum Mobile Java General
    Replies: 1
    Last Post: 2011-08-22, 14:09
  2. Audio Player in J2ME
    By Hassan Ashraf in forum Mobile Java Media (Graphics & Sounds)
    Replies: 3
    Last Post: 2011-02-18, 10:54
  3. add multiple file in default audio player.
    By ashutosh12 in forum Symbian
    Replies: 3
    Last Post: 2008-11-03, 13:03
  4. J2me mobile audio player
    By Onesizefitsall in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2007-10-15, 04:48

Posting Permissions

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