[英]How do I implement a popup media player in Android for an mp3 file?
I have made an app that has 20 or so mp3 files in the res\\raw folder, and when a button is pressed, the media is played, the button can then be pressed again and the media is stopped. 我制作了一个在res \\ raw文件夹中具有20个左右mp3文件的应用程序,当按下按钮时,将播放媒体,然后可以再次按下该按钮并停止播放媒体。
This works well, but it does not look very nice, ideally, what I would like is a little popup player that appears in the centre of the screen with a seek bar, a pause button, and the name of the song that plays. 效果很好,但看起来不太好,理想情况下,我想要一个小的弹出播放器,出现在屏幕中央,带有搜索栏,暂停按钮和所播放歌曲的名称。 (Very much like the Google Play Music app works when opening an mp3 file from a file browser). (非常类似于从文件浏览器打开mp3文件时,Google Play音乐应用可以正常工作)。 I know I could just get the button to open an intent to use Google Play Music, but I would like my app to have it's own colour scheme of a very similar looking version of the same popup. 我知道我可以按一下按钮来打开使用Google Play音乐的意图,但是我希望我的应用具有自己的配色方案,具有与同一弹出窗口非常相似的外观。
The current Java code that I have for one of the songs is as follows: 我为其中一首歌曲使用的当前Java代码如下:
package com.lmarshall1995.scoutsongs;
import com.lmarshall1995.scoutsongs.R;
import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
public class Song_AliceTheCamel_Activity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.song_alicethecamel);
setupNavigationButton();
Toast toast = Toast.makeText(this, "To be sung in the tune of \n . . .", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
final Button back = (Button) findViewById(R.id.back_button);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
final Button previous = (Button) findViewById(R.id.previous_button);
previous.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent previousIntent = new Intent(Song_AliceTheCamel_Activity.this, Song_ZipADeeDooDah_Activity.class);
Song_AliceTheCamel_Activity.this.startActivity(previousIntent);
finish();
}
});
final Button next = (Button) findViewById(R.id.next_button);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent nextIntent = new Intent(Song_AliceTheCamel_Activity.this, Song_Bingo_Activity.class);
Song_AliceTheCamel_Activity.this.startActivity(nextIntent);
finish();
}
});
final ImageButton play_tune = (ImageButton) findViewById(R.id.play_tune);
play_tune.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ImageButton play_tune = (ImageButton) findViewById(R.id.play_tune);
play_tune.setOnClickListener(new View.OnClickListener() {
MediaPlayer mp = MediaPlayer.create(Song_AliceTheCamel_Activity.this, R.raw.tune_alicethecamel);
public void onClick(View arg0) {
if (mp.isPlaying()) {
mp.stop();
mp.prepareAsync();
mp.seekTo(0);
} else {
mp.start();
}
}
});
}
});
}
private void setupNavigationButton() {}
}
And the current xml code that I have for one of the songs is as follows: 我为其中一首歌曲使用的当前xml代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/song_alicethecamel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ScoutSongs" >
<TextView
android:id="@+id/Song_AliceTheCamel_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="@string/Song_AliceTheCamel_Title"
android:textSize="20sp"
android:textColor="#FFFFFF" />
<ImageButton
android:id="@+id/play_tune"
android:src="@drawable/play_tune"
android:contentDescription="@string/play_tune"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/Song_AliceTheCamel_Title"
android:layout_alignTop="@+id/Song_AliceTheCamel_Title" />
<ScrollView
android:id="@+id/Song_AliceTheCamel_Lyrics"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/back_button"
android:layout_alignLeft="@+id/Song_AliceTheCamel_Title"
android:layout_alignRight="@+id/play_tune"
android:layout_below="@+id/play_tune">
<TextView
android:id="@+id/Song_A_Lyrics1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/Song_AliceTheCamel_Lyrics"
android:textColor="#FFFFFF" />
</ScrollView>
<Button
android:id="@+id/back_button"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/Song_AliceTheCamel_Lyrics"
android:text="@string/back"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/previous_button"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/back_button"
android:layout_alignBottom="@+id/back_button"
android:layout_alignLeft="@+id/Song_AliceTheCamel_Lyrics"
android:text="@string/previous"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/next_button"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/previous_button"
android:layout_alignBottom="@+id/previous_button"
android:layout_toRightOf="@+id/previous_button"
android:text="@string/next"
android:textColor="#FFFFFF" />
</RelativeLayout>
but I would rather have it looking more like this: 但我希望它看起来更像这样:
Any help would be much appreciated as I know the source code for this is obviously going to be protected, At the moment, all I know is I need to make an xml file for the layout, and I think that would be a good start to ask for guidance. 任何帮助将不胜感激,因为我知道这样做的源代码显然将受到保护。目前,我所知道的是我需要为布局制作一个xml文件,我认为这将是一个不错的开始寻求指导。
Once I have this information, I would like to change the background to black rather than white, the seek bar to purple rather than orange, and change what Text is displayed to the song Title, rather than the file name. 掌握了这些信息后,我想将背景更改为黑色而不是白色,将搜索栏更改为紫色而不是橙色,并将文本显示的内容更改为歌曲标题,而不是文件名。
Implement the audio player as a fragment which communicates with a background service through a messaging tool like the LocalBroadcastReceiver or Otto. 将音频播放器实现为一个片段,该片段通过诸如LocalBroadcastReceiver或Otto之类的消息传递工具与后台服务进行通信。 Then in the application when the user wants to play a song they can select it and you can create a DialogFragment with the new Fragment you created. 然后,在应用程序中,当用户想要播放歌曲时,他们可以选择它,然后可以使用创建的新片段创建DialogFragment。 Thats how I've done this before. 那就是我以前做过的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.