首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> J2ME

MMAPI 多媒體播放全攻略:從單音到視訊捕獲的 10 大實戰場景

     編輯:J2ME

MMAPI (JSR-135) 核心優勢

Mobile Media API 提供統一的 Manager.createPlayer() 工廠方法,支援音頻/視頻播放、錄製、MIDI 控制、音調合成、多媒體同步等豐富功能。核心介面:Player、PlayerListener、各類 Control (VolumeControl, VideoControl, TempoControl, RecordControl...)。

十大經典使用場景代碼詳解

1. 簡單單音播放

try {
    Manager.playTone(ToneControl.C4, 5000, 100); // 頻率、持續時間(ms)、音量(0-100)
} catch (MediaException e) { /* 處理 */ }

2. 基礎媒體播放 (HTTP 串流)

try {
    Player p = Manager.createPlayer("https://server/music.mp3");
    p.setLoopCount(5); // 循環 5 次
    p.start();
} catch (IOException | MediaException e) { /* 處理 */ }

3. 精細播放控制 (定位、音量、事件監聽)

static final long SECS_TO_MICROS = 1000000L;
Player p; VolumeControl vc;

try {
    p = Manager.createPlayer("https://server/music.mp3");
    p.realize(); // 獲取資源,準備 Control
    
    p.addPlayerListener(new PlayerListener() {
        public void playerUpdate(Player player, String event, Object data) {
            if (PlayerListener.END_OF_MEDIA.equals(event) || 
                PlayerListener.STOP_AT_TIME.equals(event)) {
                try {
                    p.setMediaTime(5 * SECS_TO_MICROS); // 從 5 秒重播
                    p.start();
                } catch (MediaException ignored) {}
            }
        }
    });
    
    vc = (VolumeControl) p.getControl("VolumeControl");
    if (vc != null) vc.setLevel(100); // 最大音量
    
    p.setMediaTime(5 * SECS_TO_MICROS); // 起始位置
    p.prefetch(); // 預載數據,降低啟動延遲
    p.start();
} catch (IOException | MediaException e) { /* 處理 */ }

4. MIDI 速度控制

try {
    Player p = Manager.createPlayer("https://server/tune.mid");
    p.realize();
    TempoControl tc = (TempoControl) p.getControl("TempoControl");
    if (tc != null) tc.setTempo(120000); // 120 BPM (微分音/分鐘)
    p.start();
} catch (IOException | MediaException e) { /* 處理 */ }

5. 視頻播放與 GUI 整合

try {
    Player p = Manager.createPlayer("https://server/movie.mpg");
    p.realize();
    VideoControl vc = (VideoControl) p.getControl("VideoControl");
    if (vc != null) {
        Form form = new Form("Video");
        form.append((Item) vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null));
        Display.getDisplay(midlet).setCurrent(form);
    }
    p.start();
} catch (IOException | MediaException e) { /* 處理 */ }

6. RMS 記憶體資料播放

try {
    byte[] record = recordStore.getRecord(recordId);
    InputStream is = new ByteArrayInputStream(record);
    Player p = Manager.createPlayer(is, "audio/X-wav"); // 需指定 MIME
    p.start();
} catch (IOException | MediaException | RecordStoreException e) { /* 處理 */ }

7. JAR 內嵌媒體資源播放

try {
    InputStream is = getClass().getResourceAsStream("/audio.wav");
    Player p = Manager.createPlayer(is, "audio/X-wav");
    p.start();
} catch (IOException | MediaException e) { /* 處理 */ }

8. 多 Player 同步播放 (時間基準對齊)

try {
    Player p1 = Manager.createPlayer("https://server/tune.mid");
    Player p2 = Manager.createPlayer("https://server/movie.mpg");
    p1.realize(); p2.realize();
    p2.setTimeBase(p1.getTimeBase()); // p2 追隨 p1 時間基準
    p1.prefetch(); p2.prefetch();
    p1.start(); p2.start(); // 同步啟動
} catch (IOException | MediaException e) { /* 處理 */ }

9. 程式化音調序列合成

byte[] sequence = {
    ToneControl.VERSION, 1,
    ToneControl.TEMPO, 30,           // 120 BPM
    ToneControl.BLOCK_START, 0,      // 定義區塊 A
    ToneControl.C4 + 4, 8,           // E4 八分音符
    ToneControl.C4 + 2, 8,           // D4
    ToneControl.C4, 8,               // C4
    ToneControl.C4 + 4, 8,           // E4
    ToneControl.BLOCK_END, 0,
    ToneControl.PLAY_BLOCK, 0,       // 播放區塊 A
    ToneControl.C4 + 2, 8, ToneControl.C4 + 2, 8, ToneControl.C4 + 2, 8, ToneControl.SILENCE, 8, // B 區
    ToneControl.C4 + 4, 8, ToneControl.C4 + 7, 8, ToneControl.C4 + 7, 8, ToneControl.SILENCE, 8,
    ToneControl.PLAY_BLOCK, 0,       // 重複 A
    ToneControl.C4 + 2, 8, ToneControl.C4 + 2, 8, ToneControl.C4 + 4, 8, ToneControl.C4 + 2, 8, ToneControl.C4, 8 // C 區
};

try {
    Player p = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
    p.realize();
    ToneControl tc = (ToneControl) p.getControl("ToneControl");
    tc.setSequence(sequence);
    p.start();
} catch (IOException | MediaException e) { /* 處理 */ }

10. 語音錄製與拍照/攝影

// 語音錄製 (AMR/WAV)
try {
    Player p = Manager.createPlayer("capture://audio");
    p.realize();
    RecordControl rc = (RecordControl) p.getControl("RecordControl");
    rc.setRecordLocation("file:///tmp/audio.wav");
    rc.startRecord();
    p.start();
    Thread.sleep(5000); // 錄製 5 秒
    p.stop(); rc.stopRecord(); rc.commit();
} catch (Exception e) { /* 處理 */ }

// 攝影預覽與拍照
try {
    Player p = Manager.createPlayer("capture://video");
    p.realize();
    VideoControl vc = (VideoControl) p.getControl("VideoControl");
    if (vc != null) {
        Form form = new Form("Camera");
        form.append((Item) vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null));
        Display.getDisplay(midlet).setCurrent(form);
    }
    p.start();
    // 拍照瞬間
    byte[] png = vc.getSnapshot(null); // null 使用預設 PNG
    Image img = Image.createImage(png, 0, png.length);
} catch (IOException | MediaException e) { /* 處理 */ }

關鍵最佳實踐

  • 生命週期管理:realize() → prefetch() → start() 三階段獲取資源,stop() → deallocate() → close() 釋放
  • Control 型別安全:永遠檢查 getControl() 返回非 null 才轉型
  • 錯誤恢復:網路串流播放需處理 CONNECTION_ERROR、BUFFERING_STARTED/STOPPED 事件
  • 資源競爭:同一時間僅能有一個 Player 使用攝影機/麥克風,需妥善協調

MMAPI 的靈活性在於統一抽象了多種媒體型別,掌握上述模式即可應對絕大多數 Java ME 多媒體開發需求。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved