Julian
Julian

Reputation: 3859

How to play audio in codenameone

I am developing an application based on codenameone. Now I try to play sounds in the background. Unfortunatly I didn't found a code sample for that.

The only thing I have I the following, which didn't work:

InputStream uri = getClass().getResourceAsStream("startsound.mp3");
    try {
        findMediaPlayer().setDataSource(uri,"audio/mpeg",null);
    } catch (IOException ex) {

    }
    findMediaPlayer().run();

Does anyone have a sample code for that problem?

Upvotes: 1

Views: 636

Answers (2)

Shai Almog
Shai Almog

Reputation: 52760

Check out the developer guide for code samples of this including a small sample of an audio recording/playback app:

Form hi = new Form("Capture", BoxLayout.y());
hi.setToolbar(new Toolbar());
Style s = UIManager.getInstance().getComponentStyle("Title");
FontImage icon = FontImage.createMaterial(FontImage.MATERIAL_MIC, s);

FileSystemStorage fs = FileSystemStorage.getInstance();
String recordingsDir = fs.getAppHomePath() + "recordings/";
fs.mkdir(recordingsDir);
try {
    for(String file : fs.listFiles(recordingsDir)) {
        MultiButton mb = new MultiButton(file.substring(file.lastIndexOf("/") + 1));
        mb.addActionListener((e) -> {
            try {
                Media m = MediaManager.createMedia(recordingsDir + file, false);
                m.play();
            } catch(IOException err) {
                Log.e(err);
            }
        });
        hi.add(mb);
    }

    hi.getToolbar().addCommandToRightBar("", icon, (ev) -> {
        try {
            String file = Capture.captureAudio();
            if(file != null) {
                SimpleDateFormat sd = new SimpleDateFormat("yyyy-MMM-dd-kk-mm");
                String fileName =sd.format(new Date());
                String filePath = recordingsDir + fileName;
                Util.copy(fs.openInputStream(file), fs.openOutputStream(filePath));
                MultiButton mb = new MultiButton(fileName);
                mb.addActionListener((e) -> {
                    try {
                        Media m = MediaManager.createMedia(filePath, false);
                        m.play();
                    } catch(IOException err) {
                        Log.e(err);
                    }
                });
                hi.add(mb);
                hi.revalidate();
            }
        } catch(IOException err) {
            Log.e(err);
        }
    });
} catch(IOException err) {
    Log.e(err);
}
hi.show();

enter image description here

Upvotes: 0

Aswin Arshad
Aswin Arshad

Reputation: 90

Not Sure whether this can imply in the case of codenameone. But u can try :-

MediaPlayer  player = MediaPlayer.create(this,  R.raw.music);
player.setLooping(true); // Set looping
player.setVolume(100,100);

public int onStartCommand(Intent intent, int flags, int startId) {
    player.start();
    return 1;
}

@Override
public void onDestroy() {
    player.stop();
    player.release();
}

public void onStart(Intent intent, int startId) 
{
    // TODO
}

Or u can refer to the discussion here : Play Background Sound in android applications

Upvotes: 1

Related Questions