gpt4 book ai didi

android - 如何使用播放/停止按钮显示通知?

转载 作者:行者123 更新时间:2023-11-29 23:38:08 27 4
gpt4 key购买 nike

我需要一些帮助,因为我是 android 的新手。在我正在开发的 android 应用程序中,我实现了 MediaPlayer 服务类 (PlayService.java),它将播放来自远程源的实时广播音频流。我的 MediaPlayer 服务类工作代码是:

public class PlayService extends Service implements MediaPlayer.OnCompletionListener,MediaPlayer.OnPreparedListener,
MediaPlayer.OnErrorListener,MediaPlayer.OnSeekCompleteListener,MediaPlayer.OnInfoListener,
MediaPlayer.OnBufferingUpdateListener,AudioManager.OnAudioFocusChangeListener {

private AudioManager audioManager;
private MediaPlayer mediaPlayer = new MediaPlayer();
private String sentAudioLink;

@Override
public void onCreate() {
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnSeekCompleteListener(this);
mediaPlayer.setOnInfoListener(this);
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.reset();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
sentAudioLink = intent.getExtras().getString("sentAudioLink");
mediaPlayer.reset();
//setup the media player data source using the strAudioLink value
if (!mediaPlayer.isPlaying()) {
try {
mediaPlayer.setDataSource(sentAudioLink);
//prepare media player
mediaPlayer.prepareAsync();
} catch (IllegalStateException e) {
Log.e("workingerror1", e.toString());
stopSelf();
} catch (IllegalArgumentException e) {
Log.e("workingerror2", e.toString());
stopSelf();
} catch (IOException e) {
Log.e("workingerror3", e.toString());
stopSelf();
}

//buildNotification(PlaybackStatus.PLAYING);
}

return START_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {

}

@Override
public void onCompletion(MediaPlayer mp) {
stopMedia();
stopSelf();
}

public void stopMedia() {
if (mediaPlayer.isPlaying())
mediaPlayer.stop();
}

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
//Invoked when there has been an error during an asynchronous operation
return false;
}

@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
return false;
}

@Override
public void onPrepared(MediaPlayer mp) {
playMedia();
}

public void playMedia() {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}

@Override
public void onSeekComplete(MediaPlayer mp) {

}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

我面临的唯一问题是我只希望音乐播放器在播放音频流时生成自定义通知。为此,我创建了 custom_notification.xml 类。我不知道我应该如何从服务创建此自定义通知并触发播放和停止操作按钮的调用监听器。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp">
<ImageView
android:layout_alignParentLeft="true"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/uow_logo"
android:id="@+id/imgAppIc" />
<RelativeLayout
android:layout_toRightOf="@id/imgAppIc"
android:singleLine="true"
android:id="@+id/textView1"
android:ellipsize="marquee"
android:layout_marginLeft="7dp"
android:layout_marginTop="5dp"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/selected_track_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title Text"
android:textColor="#f7212121"
android:textSize="15dp"/>
<TextView
android:id="@+id/selected_track_title2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FM Radio 100"
android:textColor="#767676"
android:layout_below="@+id/selected_track_title"
android:layout_marginTop="1dp"
android:textSize="15dp" />
</RelativeLayout>
<ImageButton
android:id="@+id/play_pause"
android:layout_width="35dp"
android:layout_height="35dp"
android:gravity="right"
android:layout_alignParentRight="true"
android:layout_marginTop="3dp" />
</RelativeLayout>

最佳答案

这个答案可能对您有帮助...

引用:How to create a Custom Notification Layout in android?

To define a custom notification layout, start by instantiating a RemoteViews object that inflates an XML layout file. Then, instead of calling methods such as setContentTitle(), call setContent(). To set content details in the custom notification, use the methods in RemoteViews to set the values of the view's children:

Create an XML layout for the notification in a separate file. You can use any file name you wish, but you must use the extension .xml In your app, use RemoteViews methods to define your notification's icons and text. Put this RemoteViews object into your NotificationCompat.Builder by calling setContent(). Avoid setting a background Drawable on your RemoteViews object, because your text color may become unreadable.

custom_push.xml 有我的自定义 View R.id.image,R.id.text,R.id.title

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="64dp"
android:padding="10dp" >
<ImageView
android:src="@mipmap/ic_launcher"
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp" />
<TextView
android:textSize="13dp"
android:textColor="#000"
android:text="Testing"
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
/>
<TextView
android:textSize="13dp"
android:textColor="#000"
android:text="Testing is awecome"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
android:layout_below="@id/title"
/>
</RelativeLayout>

实例化一个 RemoteViews 对象并设置它,

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_push);
contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
contentView.setTextViewText(R.id.title, "Custom notification");
contentView.setTextViewText(R.id.text, "This is a custom layout");

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContent(contentView);

Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(1, notification);

Notification Views Like

检查:https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ApplyStyle

关于android - 如何使用播放/停止按钮显示通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52082512/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com