android多媒體和相機詳解四

來源:互聯網
上載者:User

處理AUDIO_BECOMING_NOISYIntent
  很多良好的音頻播放的應用都會在那些導致聲音變為噪音(通過外部擴音器輸出)的事件發生時自動停止播放.例如,這可能發生在當一個使用者用耳機聽音樂時忽然斷開了耳機串連.音頻從擴音器播放可能不是使用者期望的.

 

 

  你可以通過處理ACTION_AUDIO_BECOMING_NOISYintent 來保證你的應用在此情況下停止播放音樂,你可以把如下代碼添加到你的manifest來註冊一個receiver:

[java] <receiver android:name=".MusicIntentReceiver"> 
   <intent-filter> 
      <action android:name="android.media.AUDIO_BECOMING_NOISY" /> 
   </intent-filter> 
</receiver> 
<receiver android:name=".MusicIntentReceiver">
   <intent-filter>
      <action android:name="android.media.AUDIO_BECOMING_NOISY" />
   </intent-filter>
</receiver>


此段把MusicIntentReceiver類作為這個intent的一個廣播接收器(broadcastreceiver)進行註冊,下面就是要實現這個類:

[java] public class MusicIntentReceiver implements android.content.BroadcastReceiver { 
   @Override 
   public void onReceive(Context ctx, Intent intent) { 
      if (intent.getAction().equals( 
                    android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY)) { 
          // 通知你的service停止播放  
          // (比如通過一個Intent)  
      } 
   } 

public class MusicIntentReceiver implements android.content.BroadcastReceiver {
   @Override
   public void onReceive(Context ctx, Intent intent) {
      if (intent.getAction().equals(
                    android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
          // 通知你的service停止播放
          // (比如通過一個Intent)
      }
   }
}

從一個ContentResolver擷取媒體


  媒體播放應用的是另一個有用的特性是檢索使用者存放在裝置上的音樂.你可以通過從ContentResolver查詢媒體來做到:

[java] ContentResolver contentResolver = getContentResolver(); 
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
Cursor cursor = contentResolver.query(uri, null, null, null, null); 
if (cursor == null) { 
    // 查詢失敗,處理錯誤  
} else if (!cursor.moveToFirst()) { 
    // 裝置上沒有媒體  
} else { 
    int titleColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE); 
    int idColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID); 
    do { 
       long thisId = cursor.getLong(idColumn); 
       String thisTitle = cursor.getString(titleColumn); 
       // ...process entry...  
    } while (cursor.moveToNext()); 

ContentResolver contentResolver = getContentResolver();
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor == null) {
    // 查詢失敗,處理錯誤 www.2cto.com
} else if (!cursor.moveToFirst()) {
    // 裝置上沒有媒體
} else {
    int titleColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
    int idColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
    do {
       long thisId = cursor.getLong(idColumn);
       String thisTitle = cursor.getString(titleColumn);
       // ...process entry...
    } while (cursor.moveToNext());
}

 

要在MediaPlayer中播放擷取到的媒體,你可以這樣做:

[java] long id = /* 上面擷取到的某個條目的id */; 
Uri contentUri = ContentUris.withAppendedId( 
        android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id); 
 
mMediaPlayer = new MediaPlayer(); 
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
mMediaPlayer.setDataSource(getApplicationContext(), contentUri); 
 
// ...prepare and start... 
long id = /* 上面擷取到的某個條目的id */;
Uri contentUri = ContentUris.withAppendedId(
        android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);

mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(getApplicationContext(), contentUri);

// ...prepare and start...

 


JetPlayer
  Android平台包含一個JET引擎,用它可以添加互動式播放的JET音頻內容到你的應用.你可以使用SDK所帶的JetCreator應用來建立JET內容.要播放和管理JET內容,使用JetPlayer類.

 


播放JET內容
  本節教給你如何寫出,配置和播放JET內容.JET的概念,介紹以及如何使用JetCreator工具建立JET內容,請看JetCreator使用者手冊,這裡不叨叨.這個工具可以在Windows,OS X, 和Linux平台(Linux上不能像Windows和OSX上那樣對匯入的資產進行試聽)上使用.


下面是如何對儲存在SD卡上的一個.jet檔案配置JET回放:

[java] JetPlayer jetPlayer = JetPlayer.getJetPlayer(); 
jetPlayer.loadJetFile("/sdcard/level1.jet"); 
byte segmentId = 0; 
 
// queue segment 5, repeat once, use General MIDI, transpose by -1 octave  
jetPlayer.queueJetSegment(5, -1, 1, -1, 0, segmentId++); 
// queue segment 2  
jetPlayer.queueJetSegment(2, -1, 0, 0, 0, segmentId++); 
 
jetPlayer.play(); 
JetPlayer jetPlayer = JetPlayer.getJetPlayer();
jetPlayer.loadJetFile("/sdcard/level1.jet");
byte segmentId = 0;

// queue segment 5, repeat once, use General MIDI, transpose by -1 octave
jetPlayer.queueJetSegment(5, -1, 1, -1, 0, segmentId++);
// queue segment 2
jetPlayer.queueJetSegment(2, -1, 0, 0, 0, segmentId++);

jetPlayer.play();


SDK包含一個例子— JetBoy —它向你示範了如何使用JetPlayer在你的遊戲中建立一個互動式音軌.它也示範了如何使用JET事件來同步音樂和遊戲邏輯.

 

 

摘自 nkmnkm的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.