Mobile Media API(MMAPI)

來源:互聯網
上載者:User
MMAPI是JSR 135: Mobile Media API提供的一套規範的播放和錄製音頻或視頻介面。

MMAPI架構:(from: developers.sun.com)

Manager類(javax.microedition.media.Manager),用於建立各種不同類型的Players,獲得各種支援協議和內容格式,播放簡單的曲調等。

Manager根據createPlayer函數的參數建立一個Data Source對象,由該對象完成對媒體資料的傳輸工作,並從資料中擷取該媒體的資料內容類型,然後根據這個媒體資料類型建立相應的Player對象,如果Manager無法確定DataSource的內容類型,它將拋出一個MediaException異常:

引用文字

// public static Player Manager.createPlayer(String locator) throws IOException, MediaException
// public static Player Manager.createPlayer(DataSource source) throws IOException, MediaException
// public static Player Manager.createPlayer(InputStream stream, String type) throws IOException, MediaException
try {
    Player p = Manager.createPlayer("http://webserver/music.mp3");
    p.setLoopCount(5);
    p.start();
} catch (IOException ioe) {
} catch (MediaException me) {
}

擷取手機支援的協議和媒體類型:

引用文字

// static java.lang.String[] Manager.getSupportedProtocols(java.lang.String content_type)
// static java.lang.String[] Manager.getSupportedContentTypes(java.lang.String protocol)

播放簡單聲調:

引用文字

// static void playTone(int note, int duration, int volume)
try {
    Manager.playTone(ToneControl.C4, 5000 /* millisec */, 100 /* max vol */);
    // ToneControl.C4 中音C
    // ToneControl.SILENCE 不發聲
} catch (MediaException e) {
}

MMAPI使用Player來處理媒體資料內容。Player是javax.microedition.media.Player介面的實現執行個體,它從Data Source中讀取媒體資料、解析和解碼資料以及識別媒體輸出裝置和傳送媒體資料到輸出裝置等。

Player提供了一套方法去控制媒體的重放和同步:
Player.start():重放媒體流。
Player.stop():停止媒體流。
Player.setMediaTime(long now):設定媒體時間。
Player.close():關閉媒體流並釋放資源。
Player.getState():擷取Player的目前狀態:

詳細重放控制:

引用文字

static final long SECS_TO_MICROSECS = 1000000L;
Player p;
VolumeControl vc;
try {
    p = Manager.createPlayer("http://webserver/music.mp3");
    p.realize();
    // Set a listener.
    p.addPlayerListener(new Listener());
    // Grab volume control for the player.
    // Set Volume to max.
    vc = (VolumeControl)p.getControl("VolumeControl");
    if (vc != null) {
        vc.setLevel(100);
    }
    // Set a start time.
    p.setMediaTime(5 * SECS_TO_MICROSECS);
    // Guarantee that the player can start with the smallest latency.
    p.prefetch();
    // Non-blocking start
    p.start();
} catch (IOException ioe) {
} catch (MediaException me) {
}

class Listener implements PlayerListener {
    public void playerUpdate(Player p, String event, Object eventData) {
        if (event == END_OF_MEDIA || event == STOP_AT_TIME) {
            System.out.println("Done processing");
            try {
                p.setMediaTime(5 * SECS_TO_MICROSECS);
                p.start();
            } catch (MediaException me) {
            }
        }
    }
}

播放RMS記憶體儲的資料:

引用文字

RecordStore rs;
int recordID;
// code to set up the record store.
try {
    InputStream is = new
    ByteArrayInputStream(rs.getRecord(recordID));
    Player p = Manager.createPlayer(is, "audio/X-wav");
    p.start();
} catch (IOException ioe) {
} catch (MediaException me) {
}

播放Jar檔案中儲存的媒體:

引用文字

/** Notice that in MIDP 2.0, the wav format is mandatory only */
/** in the case that the device supports sampled audio.       */
try {
    InputStream is = getClass().getResourceAsStream("audio.wav");
    Player p = Manager.createPlayer(is, "audio/X-wav");
    p.start();
} catch (IOException ioe) {
} catch (MediaException me) {
}

不同Player的同步:

引用文字

Player p1, p2;

try {
    p1 = Manager.createPlayer("http://webserver/tune.mid");
    p1.realize();
    p2 = Manager.createPlayer("http://webserver/movie.mpg");
    p2.realize();
    p2.setTimeBase(p1.getTimeBase());
    p1.prefetch();
    p2.prefetch();
    p1.start();
    p2.start();
} catch (IOException ioe) {
} catch (MediaException me) {
}

MMAPI還提供了一個或多個Controls來調整player的行為,可以在player從媒體轉換資料的時候從player執行個體取得並且使用Controls。我們可以通過Player中提供的一些特殊的Controls訪問一些特殊的媒體類型。Controls由javax.microedition.media.Control介面實現。

實現MIDI重放控制:

引用文字

Player p;
TempoControl tc;

try {
    p = Manager.createPlayer("http://webserver/tune.mid");
    p.realize();
    // Grab the tempo control.
    tc = (TempoControl)p.getControl("TempoControl");
    tc.setTempo(120000); // 120 beats/min
    p.start();
} catch (IOException ioe) {
} catch (MediaException me) {
}

視頻重放功能實現:

引用文字

Player p;
VideoControl vc;

try {
    p = Manager.createPlayer("http://webserver/movie.mpg");
    p.realize();
    // Grab the video control and set it to the current display.
    vc = (VideoControl)p.getControl("VideoControl");
    if (vc != null) {
        Form form = new Form("video");
        form.append((Item)vc.initDisplayMode(vc.USE_GUI_PRIMITIVE, null));
        Display.getDisplay(midlet).setCurrent(form);
    }
    p.start();
} catch (IOException ioe) {
} catch (MediaException me) {
}

產生單音序列:

引用文字

byte tempo = 30; // set tempo to 120 bpm
byte d = 8;      // eighth-note

byte C4 = ToneControl.C4;;
byte D4 = (byte)(C4 + 2); // a whole step
byte E4 = (byte)(C4 + 4); // a major third
byte G4 = (byte)(C4 + 7); // a fifth
byte rest = ToneControl.SILENCE; // rest

byte[] mySequence = {
    ToneControl.VERSION, 1,   // version 1
    ToneControl.TEMPO, tempo, // set tempo
    ToneControl.BLOCK_START, 0,   // start define "A" section
    E4,d, D4,d, C4,d, E4,d,       // content of "A" section
    E4,d, E4,d, E4,d, rest,d,          
    ToneControl.BLOCK_END, 0,     // end define "A" section
    ToneControl.PLAY_BLOCK, 0,    // play "A" section
    D4,d, D4,d, D4,d, rest,d,     // play "B" section
    E4,d, G4,d, G4,d, rest,d,
    ToneControl.PLAY_BLOCK, 0,    // repeat "A" section
    D4,d, D4,d, E4,d, D4,d, C4,d  // play "C" section
};

try{
    Player p = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
    p.realize();
    ToneControl c = (ToneControl)p.getControl("ToneControl");
    c.setSequence(mySequence);
    p.start();
} catch (IOException ioe) {
} catch (MediaException me) {
}

語音捕獲和錄音功能的實現:

引用文字

try {
    // Create a DataSource that captures live audio.
    Player p = Manager.createPlayer("capture://audio");
    p.realize();
    // Get the RecordControl, set the record location, and
    // start the Player and record for 5 seconds.
    RecordControl rc = (RecordControl)p.getControl("RecordControl");
    rc.setRecordLocation("file:/tmp/audio.wav");
    rc.startRecord();
    p.start();
    Thread.currentThread().sleep(5000);
    p.stop();
    rc.stopRecord();
    rc.commit();
} catch (IOException ioe) {
} catch (MediaException me) {
} catch (InterruptedException e) {
}

實現攝像功能:

引用文字

Player p;
VideoControl vc;

// initialize camera
try {
    p = Manager.createPlayer("capture://video");
    p.realize();
    //Grab the video control and set it to the current display.
    vc = (VideoControl)(p.getControl("VideoControl"));
    if(videoControl != null) {
        vc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
        // show camera
        p.start();
        vc.setVisible(true);
        // now take a picture
       byte[] pngImage = vc.getSnapshot(null);
       Image image = Image.createImage(pngImage, 0, pngImage.length);
    }
} catch(IOException ioe) {
} catch(MediaException me) {
} catch(SecurityException se) {
} finally {
    if(player != null) {
        player.close();
        player = null;
    }
    videoControl = null;
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.