[英]Sound(MediaPlayer) working on emulator but not on a real android device
The Emulator I am using is Galaxy Nexus API 22, and my background music(mp3) works perfectly on it and running as a service. 我使用的模拟器是Galaxy Nexus API 22,我的背景音乐(mp3)可以完美运行并作为服务运行。 However, it does not work on my SUMSUNG Galaxy Note, which has API 16, I don't think it is a API-related problem though.
但是,它在具有API 16的SUMSUNG Galaxy Note上不起作用,但是我认为这不是与API相关的问题。
Am i need to write some <uses-permission .../>
in Manifest? 我需要在清单中写一些
<uses-permission .../>
吗? or other solutions. 或其他解决方案。
Thank you in advance!! 先感谢您!!
Here is the problem 这是问题所在
Context context = getApplicationContext();
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
if (!taskInfo.isEmpty()) {
ComponentName topActivity = taskInfo.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
SoundService.pauseSound();
Toast.makeText(WelcomePage.this, "YOU LEFT YOUR APP", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(WelcomePage.this, "YOU SWITCHED ACTIVITIES WITHIN YOUR APP", Toast.LENGTH_SHORT).show();
}
}
I used these code in the onPause()
in WelcomePage
Activity (which is the 'MainActivity' in my App) in order to pause the background music when I press the HOME key or whatever.But I found the Toast sometimes immediately shows "YOU LEFT YOUR APP"
when this Activity shows up without do anything, so I think these piece of code is where the problem is. 我在
WelcomePage
Activity(在我的应用程序中为“ MainActivity” onPause()
的onPause()
中使用了这些代码,以便在按HOME键或其他功能时暂停背景音乐。但是我发现Toast有时会立即显示"YOU LEFT YOUR APP"
在此活动出现时却没有执行任何操作,因此我认为这些代码就是问题所在。 Can anyone help me? 谁能帮我? Thank you!!
谢谢!!
Try this code, its works here: 试试这个代码,它的工作在这里:
In activity, Declare the media player session before the oncreate like this; 在活动中,像这样在oncreate之前声明媒体播放器会话;
MediaPlayer mp = new MediaPlayer();
Put the mp3 file on res/raw folder! 将mp3文件放在res / raw文件夹中!
Finaly put this code on your java! 最后,将此代码放到您的Java上! (If you want this play automatic put on onCreate, or you can put this in a button if you want).
(如果您希望此播放自动放在onCreate上,或者您可以将其放在按钮中)。
mp.stop(); // Stop this if run in another place
mp = MediaPlayer.create(this, R.raw.nameofyourfile);//Load your file from folder
mp.setLooping(false); // Set loop is off, if you want it's on change (true)
mp.start(); // start playing your sound
I hope to help you! 希望对您有所帮助!
Whats the error, can you copy here the log. 错误是什么,您可以在此处复制日志吗? Maybe its a problem with the path to the mp3.
也许这是mp3路径的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.