使用安卓媒体播放器播放音频文件 [英] Playing audio file using android media player
问题描述
在我的应用程序中,我使用媒体播放器播放单个音频文件 (12 mb).该应用程序包含三个活动 ManiActivity、MWlyrics、About.音频正在 MainActivity 中播放,问题是当我在这些活动之间切换时..音频停止播放.
公共类 MainActivity 扩展 AppCompatActivity {私人媒体播放器 mp;@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mp = MediaPlayer.create(this, R.raw.abc);getSupportActionBar().setElevation(0);ImageView imageView = (ImageView)findViewById(R.id.main_img_lm);Glide.with(this).load(R.drawable.lm).thumbnail(0.5f).into(imageView);}//播放歌曲方法public void playSong (View v){mp.start();}//暂停歌曲方法public void pauseSong (View v){mp.暂停();}//停止歌曲方法public void stopSong (View v){mp.stop();mp = MediaPlayer.create(this, R.raw.abc);}//主菜单@覆盖公共布尔 onCreateOptionsMenu(菜单菜单){getMenuInflater().inflate(R.menu.menur, menu);menu.add(0, 1, 1, menuIconWithText(getResources().getDrawable(R.drawable.ic_rate), getResources().getString(R.string.rate_us)));menu.add(0, 2, 2, menuIconWithText(getResources().getDrawable(R.drawable.ic_apps), getResources().getString(R.string.more_apps)));menu.add(0, 3, 3, menuIconWithText(getResources().getDrawable(R.drawable.ic_share), getResources().getString(R.string.share_app)));menu.add(0, 4, 4, menuIconWithText(getResources().getDrawable(R.drawable.ic_info), getResources().getString(R.string.about)));menu.add(0, 5, 5, menuIconWithText(getResources().getDrawable(R.drawable.ic_exit), getResources().getString(R.string.exit)));返回真;}@覆盖公共布尔 onOptionsItemSelected(MenuItem item) {开关(item.getItemId()){情况1:Toast.makeText(MainActivity.this, "重定向给我们评分和评论页面", Toast.LENGTH_SHORT).show();返回真;案例2:Toast.makeText(MainActivity.this, "感谢您使用", Toast.LENGTH_SHORT).show();返回真;案例3:Toast.makeText(MainActivity.this, "共享应用..", Toast.LENGTH_SHORT).show();返回真;案例4:Toast.makeText(MainActivity.this, "关于这个应用程序", Toast.LENGTH_SHORT).show();Intent j = new Intent(this,About.class);开始活动(j);返回真;案例5:结束();mp.release();返回真;案例R.id.mdwtid:Intent i = new Intent(this,MWLyrics.class);开始活动(i);返回真;}返回 super.onOptionsItemSelected(item);}私人 CharSequence menuIconWithText(Drawable r, String title) {r.setBounds(0, 0, r.getIntrinsicWidth(), r.getIntrinsicHeight());SpannableString sb = new SpannableString(" " + title);ImageSpan imageSpan = new ImageSpan(r, ImageSpan.ALIGN_BOTTOM);sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);返回某人;}}
应用详情..
MainActiviy - 显示专辑图像和 [播放、暂停和停止] 图像.
Mwlyrics - 显示歌词.
关于 - 显示应用信息.
如何解决这个问题..
<块引用>如何在不中断的情况下在其他活动之间切换的同时继续播放音频.
如何在应用程序中有效地使用这个音频文件......考虑到内存.
在我的手机上测试应用程序(moto one power,android pie)时,我注意到频率(有时运行 40%)和最大使用 170 mb.它到底是什么意思.我是 Android 开发的新手,我需要一些关于此的建议.
您需要为此使用服务.您可以查看以下代码
class BackgroundAudioService 扩展服务实现 OnCompletionListener {媒体播放器媒体播放器;@覆盖公共IBinder onBind(意图意图){返回空;}@覆盖公共无效 onCreate() {mediaPlayer = MediaPlayer.create(this, R.raw.s);//raw/s.mp3mediaPlayer.setOnCompletionListener(this);}@覆盖public int onStartCommand(意图意图,int标志,int startId){如果 (!mediaPlayer.isPlaying()) {mediaPlayer.start();}返回 START_STICKY;}公共无效 onDestroy() {如果(mediaPlayer.isPlaying()){mediaPlayer.stop();}mediaPlayer.release();}公共无效onCompletion(MediaPlayer _mediaPlayer){停止自我();}}
你可以这样启动服务
public class Test extends Activity 实现 OnClickListener {按钮开始播放按钮,停止播放按钮;意图回放ServiceIntent;@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);startPlaybackButton = (Button) this.findViewById(R.id.StartPlaybackButton);stopPlaybackButton = (Button) this.findViewById(R.id.StopPlaybackButton);startPlaybackButton.setOnClickListener(this);stopPlaybackButton.setOnClickListener(this);playbackServiceIntent = new Intent(this, BackgroundAudioService.class);}public void onClick(View v) {if (v == startPlaybackButton) {开始服务(播放服务意图);结束();} else if (v == stopPlaybackButton) {停止服务(播放服务意图);结束();}}}
In my app, i'm using media player to play a single audio file (12 mb). The app contains three activities ManiActivity, MWlyrics, About. Audio is playing in MainActivity, the problem is when i switch between these activities..the audio stops playing.
public class MainActivity extends AppCompatActivity {
private MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp = MediaPlayer.create(this, R.raw.abc);
getSupportActionBar().setElevation(0);
ImageView imageView = (ImageView)findViewById(R.id.main_img_lm);
Glide.with(this).load(R.drawable.lm)
.thumbnail(0.5f)
.into(imageView);
}
//play song method
public void playSong (View v){
mp.start();
}
//pause song method
public void pauseSong (View v){
mp.pause();
}
//stop song method
public void stopSong (View v){
mp.stop();
mp = MediaPlayer.create(this, R.raw.abc);
}
// main menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menur, menu);
menu.add(0, 1, 1, menuIconWithText(getResources().getDrawable(R.drawable.ic_rate), getResources().getString(R.string.rate_us)));
menu.add(0, 2, 2, menuIconWithText(getResources().getDrawable(R.drawable.ic_apps), getResources().getString(R.string.more_apps)));
menu.add(0, 3, 3, menuIconWithText(getResources().getDrawable(R.drawable.ic_share), getResources().getString(R.string.share_app)));
menu.add(0, 4, 4, menuIconWithText(getResources().getDrawable(R.drawable.ic_info), getResources().getString(R.string.about)));
menu.add(0, 5, 5, menuIconWithText(getResources().getDrawable(R.drawable.ic_exit), getResources().getString(R.string.exit)));
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
Toast.makeText(MainActivity.this, "redirecting to rate us and review page page", Toast.LENGTH_SHORT).show();
return true;
case 2:
Toast.makeText(MainActivity.this, "thank you for using", Toast.LENGTH_SHORT).show();
return true;
case 3:
Toast.makeText(MainActivity.this, "sharing app..", Toast.LENGTH_SHORT).show();
return true;
case 4:
Toast.makeText(MainActivity.this, "about this app", Toast.LENGTH_SHORT).show();
Intent j = new Intent(this,About.class);
startActivity(j);
return true;
case 5:
finish();
mp.release();
return true;
case R.id.mdwtid:
Intent i = new Intent(this,MWLyrics.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
private CharSequence menuIconWithText(Drawable r, String title) {
r.setBounds(0, 0, r.getIntrinsicWidth(), r.getIntrinsicHeight());
SpannableString sb = new SpannableString(" " + title);
ImageSpan imageSpan = new ImageSpan(r, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
}
}
App Details..
MainActiviy - displays album image and [play, pause , and stop] images.
Mwlyrics - displays song lyrics.
About - display app info.
How to resolve this..
How to continue playing audio while switching between other activities without any interruption.
How to use this audio file efficiently in app .. considering memory.
when testing app on my mobile (moto one power, android pie) , i noticed that the Frequency (sometimes running 40 % ), and Maximum Usage 170 mb. What does it exactly mean. I'm new to android development and i need some suggestions about this.
You need to use service for this. You can check below code
class BackgroundAudioService extends Service implements OnCompletionListener {
MediaPlayer mediaPlayer;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
mediaPlayer = MediaPlayer.create(this, R.raw.s);// raw/s.mp3
mediaPlayer.setOnCompletionListener(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
return START_STICKY;
}
public void onDestroy() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
}
public void onCompletion(MediaPlayer _mediaPlayer) {
stopSelf();
}
}
You can start service like this
public class Test extends Activity implements OnClickListener {
Button startPlaybackButton, stopPlaybackButton;
Intent playbackServiceIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startPlaybackButton = (Button) this.findViewById(R.id.StartPlaybackButton);
stopPlaybackButton = (Button) this.findViewById(R.id.StopPlaybackButton);
startPlaybackButton.setOnClickListener(this);
stopPlaybackButton.setOnClickListener(this);
playbackServiceIntent = new Intent(this, BackgroundAudioService.class);
}
public void onClick(View v) {
if (v == startPlaybackButton) {
startService(playbackServiceIntent);
finish();
} else if (v == stopPlaybackButton) {
stopService(playbackServiceIntent);
finish();
}
}
}
这篇关于使用安卓媒体播放器播放音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!