BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 06-13-2007, 08:22 AM   #1 (permalink)
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
Post Thanks: 20
Thanked 77 Times in 68 Posts
Default Playing Ringtones with 4.2

Please Login to Remove!

I did a lot of poking around to figure out how to do this. Since what I mostly found was other people asking how to do this I thought I would post some code snippets. These use APIs only available in 4.2, but for what its worth:

Getting a list of ringtone files:
Code:
    
import java.lang.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;

    /**
     * Search selected directories where ring tunes are kept.
     * @return A Vector of String objects.
     */
    public static Vector RingTuneList()
    {
        String  dirs[] = {
            "file:///store/samples/ringtones/",         // Included ringtones
            "file:///store/home/user/ringtones/",        // User added ringtones
            // "file:///SDCard/BlackBerry/ringtones/"   // Micro SD Card, probably not wise
        };
        Vector  tmp = new Vector();
        
        for (int i = 0; i < dirs.length; i++)
        {
            try
            {
                FileConnection _dir = (FileConnection)Connector.open(dirs[i], Connector.READ);
                if( _dir.exists())
                {
                    if (_dir.isDirectory())
                    {
                        Enumeration list = _dir.list();
                        
                        while (list.hasMoreElements())
                        {
                            String s = (String)list.nextElement();
                            tmp.addElement(new String(dirs[i] + s));
                        }
                    }
                }
            }
            catch(IOException ioe)
            {
            }
        }
        return tmp;
    }
Playing a ringtone from a file:

Code:
import javax.microedition.media.*;


    private Player  p;
    private int     volumeSave, volumeLevel, someNumberOfRepeats;
    private VolumeControl   vc;
    private String someFileFromAbove;


    try
    {
        /**
            * Create the player from the stage tune file.
            * Set the loop count, add the Listener, realize and prefetch.
            */
        p = javax.microedition.media.Manager.createPlayer(someFileFromAbove);
        p.setLoopCount(someNumberOfRepeats);
        p.addPlayerListener(this); // If you are interested in when the tune finishes
        p.realize();
        p.prefetch();
        
        /**
            * Iterate through player controls looking for an AudioPathControl. Setting
            * the path to AUDIO_PATH_HANDSFREE seems to ensure the tune goes to the
            * speaker even when there is something in the headset jack.
            * 
            * Slightly modified from RIM sample code.
            */
        Control c[] = p.getControls();
        for (int i = c.length-1; i >= 0; i--)
        {
            if (c[i] instanceof net.rim.device.api.media.control.AudioPathControl)
            {
                net.rim.device.api.media.control.AudioPathControl apc = (net.rim.device.api.media.control.AudioPathControl)c[i];
                try
                {
                    apc.setAudioPath(net.rim.device.api.media.control.AudioPathControl.AUDIO_PATH_HANDSFREE);
                }
                catch (Exception e)
                {
                    System.out.println(e.toString());
                }
            }
        }
        
        /**
            * Get the volume control by name.
            */
        vc = (VolumeControl)p.getControl("VolumeControl");
        
        int volumeSave = vc.getLevel();
        
        vc.setLevel(someVolumeLevel);
        
        p.start();
    }
    catch (Exception e)
    {
        System.err.println(e.toString());
    }
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.