android中怎么样能获取正在播放的音乐名?
2个回答
展开全部
如果我们在此基础上更深入一步,控制音乐播放的同时,我们还需要得到正在播放的音乐名称,这又该怎么实现呢?Android Framework 提供了非常棒的 android.media,凡音乐播放都离不开 MediaPlayer,该类定义了七个内部类,其中 MediaPlayer.OnPreparedListener 是这么描述的:Interface definition for a callback to be invoked when the media source is ready for playback.媒体文件准备结束,准备播放时,Android 就会调用这个这个内部类,在 MediaPlaybackService 则是 notifyChange(String what) 方法进行处理:private void notifyChange(String what) { Intent i = new Intent(what); i.putExtra(”id”, Integer.valueOf(getAudioId())); i.putExtra(”artist”, getArtistName()); i.putExtra(”album”,getAlbumName()); i.putExtra(”track”, getTrackName()); sendBroadcast(i); // …… }那么在我们的应用当中,则需要创建 一个BroadcastReceiver,过滤对应的 action name,也就是常量 META_CHANGED 对应的值即可达到目标。 查看原帖>>
展开全部
android获取正在播放的音乐文件信息的方法为:
1、就是判断当前是否有音乐正在播放。
[java]
/** Checks whether any music is active. */
isMusicActive()
通过这个接口就可以判断当前系统是否有音乐在播放了。
2、还有一个问题,如果在音乐一开始就已经播放的时候,就知道这个事件,以便进行特殊的处理。
3、再进一步看一下 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();
1、就是判断当前是否有音乐正在播放。
[java]
/** Checks whether any music is active. */
isMusicActive()
通过这个接口就可以判断当前系统是否有音乐在播放了。
2、还有一个问题,如果在音乐一开始就已经播放的时候,就知道这个事件,以便进行特殊的处理。
3、再进一步看一下 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();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询