我正在尝试构建一个极其简单的广播流媒体应用程序,它存储一个网络广播 URL 列表,可以选择这些 URL 来流式传输音频;使用服务允许在应用未激活时继续播放 + 从通知控制。
我需要的控件非常简单:播放/暂停和停止,它们应该终止服务并在清除通知或按下应用内的停止按钮时触发。
对于大量代码,我深表歉意,但这就是我现在的位置:
public class StreamingService extends Service
implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener {
// .. snipped out fields
private AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener =
new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
// set mCurrentAudioFocusState field
}
if (mMediaPlayer != null)
configurePlayerState();
}
};
private int mCurrentAudioFocusState = AUDIO_NO_FOCUS_NO_DUCK;
private final IntentFilter mAudioNoisyIntentFilter =
new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
private BroadcastReceiver mNoisyReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Pause when headphones unplugged
mMediaPlayer.pause();
}
};
private boolean mAudioNoisyReceiverRegistered = false;
@Override
public void onCreate() {
super.onCreate();
AudioManager mAudioManager = (AudioManager)
getSystemService(Context.AUDIO_SERVICE);
int result = mAudioManager.requestAudioFocus(
mOnAudioFocusChangeListener,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN
);
if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
stopSelf();
} else {
mCurrentAudioFocusState = AUDIO_FOCUSED;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnErrorListener(this);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
WifiManager.WifiLock wifiLock =
((WifiManager) Objects.requireNonNull(
getApplicationContext().getSystemService(Context.WIFI_SERVICE)))
.createWifiLock(WifiManager.WIFI_MODE_FULL, "wifi_lock");
wifiLock.acquire();
try {
mMediaPlayer.setDataSource(intent.getStringExtra(STREAM_URI));
} catch (IOException e) {
e.printStackTrace();
}
mMediaPlayer.prepareAsync();
onStartIntent = intent;
return Service.START_STICKY;
}
@Override
public void onDestroy() {
mMediaPlayer.release();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
mMediaPlayer.reset();
return true;
}
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
handleIntent(onStartIntent);
}
private void handleIntent(Intent intent) {
String action = intent.getAction();
String command = intent.getStringExtra(CMD_NAME);
if (ACTION_CMD.equals(action)) {
switch (command) {
case CMD_PLAY:
registerAudioNoisyReceiver();
mMediaPlayer.start();
startForeground(NOTIFICATION_ID, buildNotification());
case CMD_PAUSE:
unregisterAudioNoisyReceiver();
mMediaPlayer.pause();
startForeground(NOTIFICATION_ID, buildNotification());
case CMD_STOP:
unregisterAudioNoisyReceiver();
mMediaPlayer.stop();
stopSelf();
}
}
}
private Notification buildNotification() {
createNotificationChannel();
NotificationCompat.Builder builder =
new NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CHANNEL);
builder
.setContentTitle(onStartIntent.getStringExtra(STREAM_TITLE))
.setContentIntent(PendingIntent.getActivity(
this,
0,
new Intent(getApplicationContext(), MainActivity.class),
0))
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setDeleteIntent(getActionIntent(CMD_STOP));
builder
.setSmallIcon(android.R.drawable.ic_media_play)
.setColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
builder
.addAction(new NotificationCompat.Action(
android.R.drawable.ic_media_pause, getString(R.string.pause),
getActionIntent(CMD_PAUSE)));
builder
.setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0)
.setShowCancelButton(true)
.setCancelButtonIntent(
getActionIntent(CMD_STOP)));
return builder.build();
}
private PendingIntent getActionIntent(String action) {
Intent s = new Intent(getApplicationContext(), StreamingService.class);
s.putExtra(
STREAM_TITLE,
onStartIntent.getStringExtra(STREAM_TITLE)
);
s.putExtra(
STREAM_URI,
onStartIntent.getStringExtra(STREAM_URI)
);
s.setAction(ACTION_CMD);
s.putExtra(
CMD_NAME,
action
);
s.setPackage(getApplicationContext().getPackageName());
return PendingIntent.getService(
getApplicationContext(), 0, s, 0);
}
// snipped methods to register and unregister noisy receiver
private void configurePlayerState() {
switch(mCurrentAudioFocusState) {
case AUDIO_NO_FOCUS_CAN_DUCK:
registerAudioNoisyReceiver();
mMediaPlayer.setVolume(VOLUME_DUCK, VOLUME_DUCK);
case AUDIO_NO_FOCUS_LOST:
unregisterAudioNoisyReceiver();
mMediaPlayer.stop();
case AUDIO_NO_FOCUS_NO_DUCK:
unregisterAudioNoisyReceiver();
mMediaPlayer.pause();
case AUDIO_FOCUSED:
registerAudioNoisyReceiver();
mMediaPlayer.setVolume(VOLUME_NORMAL, VOLUME_NORMAL);
}
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel =
new NotificationChannel(NOTIFICATION_CHANNEL, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
assert notificationManager != null;
notificationManager.createNotificationChannel(channel);
}
}
}
这是根据使用 Google 关于媒体播放的讲座、Android 文档以及 UAMP 和其他在线示例等示例应用的想法整理而成。
目前的代码:启动,似乎设置了音频,但随后似乎暂停、停止和销毁,同时销毁了通知。应用程序内不会出现通知,也不会播放音频。这是一个日志:
05-06 12:41:21.407 1903 1994 I ActivityManager: Displayed com.ojm.pinstream/.activities.MainActivity: +727ms
05-06 12:41:23.955 1903 2517 D AudioService: Stream muted, skip playback
05-06 12:41:23.962 1903 3205 I ActivityManager: START u0 {cmp=com.ojm.pinstream/.activities.PlayActivity} from uid 10191
05-06 12:41:23.979 12786 12786 W AudioManager: Use of stream types is deprecated for operations other than volume control
05-06 12:41:23.979 12786 12786 W AudioManager: See the documentation of requestAudioFocus() for what to use instead with android.media.AudioAttributes to qualify your playback use case
05-06 12:41:23.980 1903 3205 I MediaFocusControl: requestAudioFocus() from uid/pid 10191/12786 clientId=android.media.AudioManager@6badb4bcom.ojm.pinstream.services.StreamingService$1@3626928 callingPack=com.ojm.pinstream req=1 flags=0x0 sdk=27
05-06 12:41:23.986 12786 12786 W MediaPlayer: Use of stream types is deprecated for operations other than volume control
05-06 12:41:23.986 12786 12786 W MediaPlayer: See the documentation of setAudioStreamType() for what to use instead with android.media.AudioAttributes to qualify your playback use case
05-06 12:41:23.990 12786 12786 V MediaHTTPService: MediaHTTPService(android.media.MediaHTTPService@9e12641): Cookies: null
05-06 12:41:23.992 1808 25066 D NuPlayerDriver: NuPlayerDriver(0xe8513800) created, clientPid(12786)
05-06 12:41:23.996 12786 12808 V MediaHTTPService: makeHTTPConnection: CookieManager created: java.net.CookieManager@5cb47e6
05-06 12:41:23.997 12786 12808 V MediaHTTPService: makeHTTPConnection(android.media.MediaHTTPService@9e12641): cookieHandler: java.net.CookieManager@5cb47e6 Cookies: null
05-06 12:41:24.005 12786 12808 D NetworkSecurityConfig: No Network Security Config specified, using platform default
05-06 12:41:24.053 1903 4685 E NotificationService: Suppressing notification from package by user request.
05-06 12:41:24.056 1903 1966 E NotificationService: Suppressing notification from package by user request.
05-06 12:41:24.076 12786 12791 I zygote64: Do partial code cache collection, code=60KB, data=45KB
05-06 12:41:24.076 12786 12791 I zygote64: After code cache collection, code=60KB, data=45KB
05-06 12:41:24.078 12786 12791 I zygote64: Increasing code cache capacity to 256KB
05-06 12:41:24.203 1903 1994 I ActivityManager: Displayed com.ojm.pinstream/.activities.PlayActivity: +203ms
05-06 12:41:24.227 12786 12807 D OpenGLRenderer: endAllActiveAnimators on 0x7bd8b64c00 (ListView) with handle 0x7be64b8340
05-06 12:41:27.025 1903 8861 E NotificationService: Suppressing notification from package by user request.
05-06 12:41:27.031 1903 1966 E NotificationService: Suppressing notification from package by user request.
05-06 12:41:28.257 5051 5051 V ApiRequest: Performing request to https://127.0.0.1:8384/rest/events?since=0&limit=1
05-06 12:41:28.322 5051 5051 D EventProcessor: Reading events starting with id 1675
05-06 12:41:28.322 5051 5051 V ApiRequest: Performing request to https://127.0.0.1:8384/rest/events?since=1675&limit=0
05-06 12:41:28.733 1903 8861 D WificondControl: Scan result ready event
05-06 12:41:29.020 1808 12827 D GenericSource: stopBufferingIfNecessary_l, mBuffering=0
05-06 12:41:29.020 1808 12818 D NuPlayerDriver: notifyListener_l(0xe8513800), (1, 0, 0, -1), loop setting(0, 0)
05-06 12:41:29.039 1903 3205 V MediaRouterService: restoreBluetoothA2dp(false)
05-06 12:41:29.039 1711 6225 D AudioPolicyManagerCustom: setForceUse() usage 1, config 10, mPhoneState 0
05-06 12:41:29.040 1808 2811 D NuPlayerDriver: start(0xe8513800), state is 4, eos is 0
05-06 12:41:29.041 1808 12818 I GenericSource: start
05-06 12:41:29.061 1808 12834 I OMXClient: Treble IOmx obtained
05-06 12:41:29.061 1812 1902 I OMXMaster: makeComponentInstance(OMX.google.mp3.decoder) in omx@1.0-service process
05-06 12:41:29.067 1812 1902 E OMXNodeInstance: setConfig(0xf362a720:google.mp3.decoder, ConfigPriority(0x6f800002)) ERROR: Undefined(0x80001001)
05-06 12:41:29.068 1808 12834 I ACodec : codec does not support config priority (err -2147483648)
05-06 12:41:29.068 1812 6179 E OMXNodeInstance: getConfig(0xf362a720:google.mp3.decoder, ConfigAndroidVendorExtension(0x6f100004)) ERROR: Undefined(0x80001001)
05-06 12:41:29.069 1808 12834 I MediaCodec: MediaCodec will operate in async mode
05-06 12:41:29.081 1808 2811 D NuPlayerDriver: pause(0xe8513800)
05-06 12:41:29.081 1808 2811 D NuPlayerDriver: notifyListener_l(0xe8513800), (7, 0, 0, -1), loop setting(0, 0)
05-06 12:41:29.082 1903 1966 E NotificationService: Suppressing notification from package by user request.
05-06 12:41:29.082 1903 8861 V MediaRouterService: restoreBluetoothA2dp(false)
05-06 12:41:29.084 1711 6225 D AudioPolicyManagerCustom: setForceUse() usage 1, config 10, mPhoneState 0
05-06 12:41:29.097 1808 2811 D NuPlayerDriver: stop(0xe8513800)
05-06 12:41:29.097 1808 2811 D NuPlayerDriver: notifyListener_l(0xe8513800), (8, 0, 0, -1), loop setting(0, 0)
05-06 12:41:29.101 12786 12786 V MediaPlayer: resetDrmState: mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
05-06 12:41:29.102 12786 12786 V MediaPlayer: cleanDrmObj: mDrmObj=null mDrmSessionId=null
05-06 12:41:29.102 1808 2811 D NuPlayerDriver: reset(0xe8513800) at state 8
05-06 12:41:29.103 1903 1903 I NotificationService: Cannot find enqueued record for key: 0|com.ojm.pinstream|576|null|10191
05-06 12:41:29.108 1808 12826 I NuCachedSource2: caching reached eos.
05-06 12:41:29.108 1903 1966 E NotificationService: Suppressing notification from package by user request.
05-06 12:41:29.117 1903 3205 E NotificationService: Suppressing notification from package by user request.
05-06 12:41:29.117 1808 12818 D NuPlayerDriver: notifyResetComplete(0xe8513800)
05-06 12:41:29.121 1903 1966 E NotificationService: Suppressing notification from package by user request.
05-06 12:41:29.123 2663 2663 W StatusBar: removeNotification for unknown key: 0|com.ojm.pinstream|576|null|10191
我在 Android 开发方面经验不足。如果有人可以提供任何帮助,我们将不胜感激。
最佳答案
这是在您的代码中调用的方法链:
startForeground(NOTIFICATION_ID, buildNotification()) 方法被调用buildNotification() 方法通过 getActionIntent(CMD_PAUSE)getActionIntent()方法调用方法PendingIntent.getService(getApplicationContext(), 0, s, 0)问题是,根据文档,您是通过一种立即启动服务的方法获取 PendingIntent - PendingIntent.getService():
Retrieve a PendingIntent that will start a service, like calling {@link Context#startService Context.startService()}. The start arguments given to the service will come from the extras of the Intent.
当您的音频开始播放时,它会创建通知,获取 CMD_PAUSE 操作的挂起 Intent ,这个挂起的 Intent 启动服务,handleIntent() 方法通过待定 Intent ,然后您的音频将暂停...
根据个人经验,您应该使用以下方法进行调查:
MediaButtonReceiver.buildMediaButtonPendingIntent(context,
PlaybackStateCompat.ACTION_PLAY)
参见 MediaButtonReceiver documentation了解更多详情。
onStartCommand() 在您的媒体服务发生媒体事件(例如按下暂停按钮)时被调用 - 因此您应该实现一个简单的回调来处理 Intent :
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
MediaButtonReceiver.handleIntent(mSession, intent)
return super.onStartCommand(intent, flags, startId)
}
您需要找到一种不同的方法来将新的 URI 传递给您的服务,例如使用 MediaBrowser - 或者更简单的方法,绑定(bind)到服务并调用一种方法来从您的 Activity 中刷新 URI。您不应该从触发 onStartCommand() 的 Activity 中调用 startService()。
关于java - 如何为带有交互式通知的网络广播流创建 Android 前台服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50189004/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我刚刚为fedora安装了emacs。我想用emacs编写ruby。为ruby提供代码提示、代码完成类型功能所需的工具、扩展是什么? 最佳答案 ruby-mode已经包含在Emacs23之后的版本中。不过,它也可以通过ELPA获得。您可能感兴趣的其他一些事情是集成RVM、feature-mode(Cucumber)、rspec-mode、ruby-electric、inf-ruby、rinari(用于Rails)等。这是我当前用于Ruby开发的Emacs配置:https://github.com/citizen428/emacs
如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
这可能是个愚蠢的问题。但是,我是一个新手......你怎么能在交互式rubyshell中有多行代码?好像你只能有一条长线。按回车键运行代码。无论如何我可以在不运行代码的情况下跳到下一行吗?再次抱歉,如果这是一个愚蠢的问题。谢谢。 最佳答案 这是一个例子:2.1.2:053>a=1=>12.1.2:054>b=2=>22.1.2:055>a+b=>32.1.2:056>ifa>b#Thecode‘if..."startsthedefinitionoftheconditionalstatement.2.1.2:057?>puts"f