我需要一些帮助,因为我在android中有点新。在我正在使用的android应用程序中,我实现了MediaPlayer服务类(PlayService.java),它将播放来自远程源的实时广播音频流。我的MediaPlayer Service类的工作代码是:
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>
答案 0 :(得分:1)
此答案可能会帮助您...
参考:How to create a Custom Notification Layout in android?
要定义自定义通知布局,请首先实例化一个可扩展XML布局文件的RemoteViews对象。然后,代替 调用setContentTitle()之类的方法,然后调用setContent()。设置 自定义通知中的内容详细信息,请使用 RemoteViews设置视图子级的值:
在单独的文件中为通知创建XML布局。您可以 使用所需的任何文件名,但必须使用扩展名.xml 您的应用,请使用RemoteViews方法来定义通知的图标 和文字。将此RemoteViews对象放入您的 通过调用setContent()的NotificationCompat.Builder。避免设置 在您的RemoteViews对象上可绘制背景,因为您的文本 颜色可能变得不可读。
custom_push.xml具有我的自定义视图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);
检查:https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ApplyStyle