当前位置:操作系统 > 安卓/Android >>

Android如何判断当前手机是否正在播放音乐,并获取到正在播放的音乐的信息

我想实现如下的场景,判断当前Android手机上是否正在播放音乐,如果是,通过某个特定的手势,
或者点击某个按键,将当前我正在听的音乐共享出去。
第一步,就是判断当前是否有音乐正在播放。
最开始我想得有点复杂,以为要深入framework或更下层去做手脚才行,找了一下资料,发现AudioManager对外暴露了接口。
[java]  
/** Checks whether any music is active. */  
isMusicActive()  
通过这个接口就可以判断当前系统是否有音乐在播放了。
 
还有一个问题,如果我想在音乐一开始就已经播放的时候,就知道这个事件,以便进行特殊的处理。
再进一步看一下 AudioManager 的源码,发现其中有如下方法:
[java] 
    /** 
     *  Request audio focus. 
     *  Send a request to obtain the audio focus 
     *  @param l the listener to be notified of audio focus changes 
     *  @param streamType the main audio stream type affected by the focus request 
     *  @param durationHint use {@link #AUDIOFOCUS_GAIN_TRANSIENT} to indicate this focus request 
     *      is temporary, and focus will be abandonned shortly. Examples of transient requests are 
     *      for the playback of driving directions, or notifications sounds. 
     *      Use {@link #AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK} to indicate also that it's ok for 
     *      the previous focus owner to keep playing if it ducks its audio output. 
     *      Use {@link #AUDIOFOCUS_GAIN} for a focus request of unknown duration such 
     *      as the playback of a song or a video. 
     *  @return {@link #AUDIOFOCUS_REQUEST_FAILED} or {@link #AUDIOFOCUS_REQUEST_GRANTED} 
     */  
    public int requestAudioFocus(OnAudioFocusChangeListener l, int streamType, int durationHint)  
 
从字面意思来看:请求音频焦点,再看这个函数的返回值:
[java] 
    /** 
     * A failed focus change request. 
     */  
    public static final int AUDIOFOCUS_REQUEST_FAILED = 0;  
  
  
    /** 
     * A successful focus change request. 
     */  
    public static final int AUDIOFOCUS_REQUEST_GRANTED = 1;  
 
这个函数可能对我有帮助,进一步查一下Google官方的帮助:http://developer.android.com/training/managing-audio/audio-focus.html
Managing Audio Focus
With multiple apps potentially playing audio it's important to think about how they should interact. To avoid every music app playing at the same time, Android uses audio focus to moderate audio playback—only apps that hold the audio focus should play audio.
Before your app starts playing audio it should request—and receive—the audio focus.  Likewise, it should know how to listen for a loss of audio focus and respond appropriately when that happens.
 
简单地翻译一下:
管理音频焦点
多个应用都在播放音频的可能性,所以考虑应用间如何交互非常重要。为避免每个音乐应用同时播放,Android使用音频焦点来协调音频的播放----只有获取到音频焦点的应用可以播放音频。
在你的应用开始播放音频之前,它应该先请求--并接收音频焦点。同样,它也应该知道当监听到失去音频焦点后如何合理地进行响应。
 
沿着这个路应该是对的,写了下面的测试代码进行验证。这个主要是Service的实现,你还需要实现一个Activity去启动Service、结束Service:
[java]  
package com.example.servicetest;  
  
import android.app.Service;  
import android.content.Context;  
import android.content.Intent;  
import android.media.AudioManager;  
import android.media.AudioManager.OnAudioFocusChangeListener;  
import android.media.MediaPlayer;  
import android.os.IBinder;  
import android.util.Log;  
import android.widget.Toast;  
  
public class MainService extends Service  
{  
    private static final String TAG = "MainService";  
      
    private MediaPlayer player;  
      
    private AudioManager mAm;  
      
    private MyOnAudioFocusChangeListener mListener;  
  
      
    @Override  
    public void onCreate()  
    {  
        Log.i(TAG, "onCreate");  
          
        player = MediaPlayer.create(this, R.raw.test);  // 在res目录下新建raw目录,复制一个test.mp3文件到此目录下。  
        player.setLooping(false);  
          
        mAm = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);  
        mListener = new MyOnAudioFocusChangeListener();  
    }  
      
  
    @Override  
    public IBinder onBind(Intent intent)  
    {  
        return null;  
    }  
      
  
    @Override  
    public void onStart(Intent intent, int startid)  
    {  
        Toast.makeText(this, "My Service Start", Toast.LENGTH_LONG).show();  
        Log.i(TAG, "onStart");  
          
        // Request audio focus for playback  
        int result = mAm.requestAudioFocus(mListener,  
                AudioManager.STREAM_MUSIC,  
                AudioManager.AUDIOFOCUS_GAIN);  
          
        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED)  
        {  
            Log.i(TAG, "requestAudioFocus successfully.");  
              
            // Start playback.  
            player.start();  
        }  
        else  
        {  
          &n
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,