我正在开发一个媒体播放器的应用程序。
问题:如何在不关闭屏幕的情况下使媒体播放器(应用程序)正常工作? 问题: loadInBackground()返回uri,但屏幕关闭时未调用onLoadFinished。
很难用词来解释麻烦:
媒体播放器包含加载歌曲的Loader
和加载相关建议的另一个Loader
。我还将方法实现到play_next()
,它依赖于完成的媒体播放器的监听器(右上角的按钮)。
媒体播放器已在我制作的服务类中初始化,因此用户可以搜索新歌曲,并使用按钮准备next_song()
(并且播放继续,因为我每次都连接到服务{{ 1}}已加载,我从服务媒体播放器返回,因此我可以附加Activity
方法的监听器。
如果我打开屏幕,我会按照以下方式开展活动:
小粉红点是进度条。所以活动试图刷新自己?
在onFinish
方法中,我调用onCreate()
初始化并使用start_loader
执行操作。
我已经看过一些电源管理器工具并且看到了关于它的坏评论,这意味着电池使用情况但是我确实尝试了它并且从log cat它再次进入空闲状态(如果它很重要)。
Loader
活动和onPause()
?此外,我从loadInBackground()获取消息,这是来自歌曲的uri,从那里冻结不会继续。
答案 0 :(得分:2)
当你播放歌曲时,你需要为在Background.so上运行的内容创建Service
,如果你屏幕关闭,请不要停止播放。Service in Android
上层链接完美地描述了服务。
服务示例......
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.widget.Toast;
import com.example.divyesh.musiclite.Pojos.SongsList;
import java.io.File;
import java.io.IOException;
/**
* Created by Divyesh on 11/18/2017.
*/
public class MediaSongServiece extends Service {
SongsList s;
private static Boolean destroy = false;
private String TAG = "HELLO";
private MusicIntentReceiver reciever;
private SharedPreferences prefrence;
private static MediaPlayer player;
private int thisStartId = 1;
private String ss[];
SharedPreferences.Editor editor;
public IBinder onBind(Intent arg0) {
return null;
}
public static void requestPlayMedia() {
player.start();
}
public void requestPauseMedia() {
player.pause();
}
@Override
public void onCreate() {
super.onCreate();
reciever = new MusicIntentReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
registerReceiver(reciever, filter);
Log.d("service", "onCreate");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
stopSelf();
}
@Override
public boolean onUnbind(Intent intent) {
stopSelf(thisStartId);
return super.onUnbind(intent);
}
public void onStart(Intent intent, int startId) {
if (intent.equals(null)) {
stopSelf();
}
if (destroy == false) {
thisStartId = startId;
ss = intent.getExtras().getStringArray("getArray");
Log.e(TAG, "onStart: " + ss[0] + ss[1] + " path" + ss[5]);
s = new SongsList();
s.setAll(ss);
if (player != null) {
player.stop();
player.reset();
try {
player.setDataSource(getApplicationContext(), Uri.fromFile(new File(s.getPath())));
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
player.setLooping(true);
player.setVolume(100, 100);
player.start();
Log.e(TAG, "onStart: m= is not null" + player.isPlaying());
} else {
player = MediaPlayer.create(getApplicationContext(), Uri.fromFile(new File(s.getPath())));
player.setLooping(true);
player.setVolume(100, 100);
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
Log.e(TAG, "onStart: m= is null WE created new player");
}
} else {
Log.e(TAG, "onelse destroy ");
recover();
}
}
private void recover() {
destroy = false;
prefrence = getSharedPreferences("SongPrefrence", Context.MODE_PRIVATE);
for (int i = 0; i <= 5; i++) {
ss[i] = prefrence.getString("" + i, "");
}
String currentPose = prefrence.getString("current_pos", "");
Log.e(TAG, "recover: Shared Daata is" + ss[5] + "_______" + currentPose);
}
@Override
public void onDestroy() {
unregisterReceiver(reciever);
player.stop();
player.release();
stopSelf(thisStartId);
}
@Override
public void onLowMemory() {
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
prefrence = getSharedPreferences("SongPrefrence", Context.MODE_PRIVATE);
editor = prefrence.edit();
destroy = true;
}
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
prefrence = getSharedPreferences("SongPrefrence", Context.MODE_PRIVATE);
editor = prefrence.edit();
destroy = true;
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void saveData() {
player.pause();
for (int i = 0; i < ss.length; i++) {
editor.putString("" + i, ss[i]);
}
editor.putString("current_pos", "" + player.getCurrentPosition());
editor.commit();
}
public class MusicIntentReceiver extends BroadcastReceiver {
public String TAG = "ss";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
Log.d(TAG, "Headset is unplugged");
Toast.makeText(context, " Headset is unpluged ", Toast.LENGTH_SHORT).show();
Log.e(TAG, "onReceive: " + " is play song " + player.isPlaying());
break;
case 1:
Log.d(TAG, "Headset is plugged");
break;
default:
Log.d(TAG, "I have no idea what the headset state is");
}
}
}
}
}