gpt4 book ai didi

android - 使用Android Media Player播放音频文件

转载 作者:行者123 更新时间:2023-12-03 00:12:52 25 4
gpt4 key购买 nike

在我的应用中,我正在使用媒体播放器播放单个音频文件(12 mb)。该应用程序包含三个 Activity ManiActivity,MWlyrics,关于。
音频正在MainActivity中播放,问题是当我在这些 Activity 之间切换时。音频停止播放。

 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;
}
}

应用详情

MainActiviy-显示专辑图像和[播放,暂停和停止]图像。

Mwlyrics-显示歌曲歌词。

关于-显示应用信息。

如何解决这个问题。

  1. How to continue playing audio while switching between other activities without any interruption.

  2. How to use this audio file efficiently in app .. considering memory.



当在我的移动设备(摩托车一种电源,Android Pie)上测试应用程序时,我注意到该频率(有时运行40%)和最大使用量为170 mb。这到底是什么意思。我是android开发的新手,我需要一些建议。

最佳答案

您需要为此使用服务。您可以检查以下代码

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();
}

}

您可以像这样开始服务
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();
}
}
}

关于android - 使用Android Media Player播放音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53533702/

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